blob: b113c602cf2c44926518dca2c219203eeb5c3950 [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. Adve70bc4b52001-07-21 12:41:50 +000045 MachineOperand::MachineOperandType operandType,
Ruchira Sasanka45c171e2001-08-07 20:16:52 +000046 Value* _val, bool isdef=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000047{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000048 assert(i < operands.size());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000049 operands[i].Initialize(operandType, _val);
Vikram S. Adve149977b2001-08-13 16:32:45 +000050 operands[i].isDef = isdef ||
Vikram S. Adve6e447182001-09-18 12:56:28 +000051 TargetInstrDescriptors[opCode].resultPos == (int) i;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000052}
53
54void
Vikram S. Advec356e562002-03-18 03:35:24 +000055MachineInstr::SetMachineOperandConst(unsigned int i,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000056 MachineOperand::MachineOperandType operandType,
Vikram S. Advec356e562002-03-18 03:35:24 +000057 int64_t intValue)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000058{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000059 assert(i < operands.size());
Vikram S. Advec356e562002-03-18 03:35:24 +000060 assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
61 "immed. constant cannot be defined");
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000062 operands[i].InitializeConst(operandType, intValue);
Vikram S. Advec356e562002-03-18 03:35:24 +000063 operands[i].isDef = false;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000064}
65
66void
Vikram S. Advec356e562002-03-18 03:35:24 +000067MachineInstr::SetMachineOperandReg(unsigned int i,
68 int regNum,
69 bool isdef=false,
70 bool isCCReg=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000071{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000072 assert(i < operands.size());
Vikram S. Advec356e562002-03-18 03:35:24 +000073 operands[i].InitializeReg(regNum, isCCReg);
Vikram S. Adve149977b2001-08-13 16:32:45 +000074 operands[i].isDef = isdef ||
Vikram S. Adve6e447182001-09-18 12:56:28 +000075 TargetInstrDescriptors[opCode].resultPos == (int) i;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000076}
77
78void
Ruchira Sasanka0b03c6a2001-08-07 21:01:23 +000079MachineInstr::dump(unsigned int indent) const
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000080{
81 for (unsigned i=0; i < indent; i++)
Chris Lattner697954c2002-01-20 22:54:45 +000082 cerr << " ";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000083
Chris Lattner697954c2002-01-20 22:54:45 +000084 cerr << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000085}
86
Vikram S. Adve93240fe2002-04-25 04:31:18 +000087static inline std::ostream &OutputValue(std::ostream &os,
88 const Value* val)
89{
90 os << "(val ";
91 if (val && val->hasName())
92 return os << val->getName();
93 else
94 return os << (void*) val; // print address only
95 os << ")";
96}
97
Chris Lattner697954c2002-01-20 22:54:45 +000098std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000099{
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000100 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000101
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000102 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000103 os << "\t" << minstr.getOperand(i);
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000104 if( minstr.getOperand(i).opIsDef() )
105 os << "*";
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000106 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000107
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000108 // code for printing implict references
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000109 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
110 if( NumOfImpRefs > 0 ) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000111 os << "\tImplicit: ";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000112 for(unsigned z=0; z < NumOfImpRefs; z++) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000113 OutputValue(os, minstr.getImplicitRef(z));
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000114 if( minstr.implicitRefIsDefined(z)) os << "*";
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000115 os << "\t";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000116 }
117 }
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000118
Chris Lattner697954c2002-01-20 22:54:45 +0000119 return os << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000120}
121
Chris Lattner697954c2002-01-20 22:54:45 +0000122static inline std::ostream &OutputOperand(std::ostream &os,
123 const MachineOperand &mop)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000124{
Vikram S. Adved9beb972001-11-12 14:19:47 +0000125 Value* val;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000126 switch (mop.getOperandType())
127 {
128 case MachineOperand::MO_CCRegister:
129 case MachineOperand::MO_VirtualRegister:
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000130 return OutputValue(os, mop.getVRegValue());
Vikram S. Adve6e447182001-09-18 12:56:28 +0000131 case MachineOperand::MO_MachineRegister:
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000132 return os << "(" << mop.getMachineRegNum() << ")";
Vikram S. Adve6e447182001-09-18 12:56:28 +0000133 default:
134 assert(0 && "Unknown operand type");
135 return os;
136 }
Chris Lattnere6fdb112001-09-09 22:26:29 +0000137}
138
139
Chris Lattner697954c2002-01-20 22:54:45 +0000140std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000141{
142 switch(mop.opType)
143 {
144 case MachineOperand::MO_VirtualRegister:
145 case MachineOperand::MO_MachineRegister:
146 os << "%reg";
147 return OutputOperand(os, mop);
148 case MachineOperand::MO_CCRegister:
149 os << "%ccreg";
150 return OutputOperand(os, mop);
151 case MachineOperand::MO_SignExtendedImmed:
Chris Lattner697954c2002-01-20 22:54:45 +0000152 return os << (long)mop.immedVal;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000153 case MachineOperand::MO_UnextendedImmed:
Chris Lattner697954c2002-01-20 22:54:45 +0000154 return os << (long)mop.immedVal;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000155 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee949da52001-09-30 23:44:19 +0000156 {
157 const Value* opVal = mop.getVRegValue();
Chris Lattner4d669b52002-04-08 22:01:15 +0000158 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
Vikram S. Adved9beb972001-11-12 14:19:47 +0000159 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
160 if (opVal->hasName())
Chris Lattner697954c2002-01-20 22:54:45 +0000161 os << opVal->getName();
Vikram S. Adved9beb972001-11-12 14:19:47 +0000162 else
163 os << opVal;
164 return os << ")";
Vikram S. Advee949da52001-09-30 23:44:19 +0000165 }
Vikram S. Adve6e447182001-09-18 12:56:28 +0000166 default:
167 assert(0 && "Unrecognized operand type");
168 break;
169 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000170
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000171 return os;
172}