Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 1 | //===-- MachineInstr.cpp --------------------------------------------------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Brian Gaeke | 21326fc | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 9 | // |
| 10 | // Methods common to all machine instructions. |
| 11 | // |
| 12 | // FIXME: Now that MachineInstrs have parent pointers, they should always |
| 13 | // print themselves using their MachineFunction's TargetMachine. |
| 14 | // |
Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 15 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 16 | |
Chris Lattner | 822b4fb | 2001-09-07 17:18:30 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | 8517e1f | 2004-02-19 16:17:08 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 3801f6d | 2002-02-03 07:46:01 +0000 | [diff] [blame] | 19 | #include "llvm/Value.h" |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | 2a79a09 | 2002-10-30 00:58:19 +0000 | [diff] [blame] | 22 | #include "llvm/Target/MRegisterInfo.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/Support/LeakDetector.h" |
Reid Spencer | 954da37 | 2004-07-04 12:19:56 +0000 | [diff] [blame] | 24 | #include <iostream> |
| 25 | |
Chris Lattner | 0742b59 | 2004-02-23 18:38:20 +0000 | [diff] [blame] | 26 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 27 | |
Chris Lattner | f1757c4 | 2002-10-29 17:40:30 +0000 | [diff] [blame] | 28 | // Global variable holding an array of descriptors for machine instructions. |
| 29 | // The actual object needs to be created separately for each target machine. |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 30 | // This variable is initialized and reset by class TargetInstrInfo. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 31 | // |
Chris Lattner | f1757c4 | 2002-10-29 17:40:30 +0000 | [diff] [blame] | 32 | // FIXME: This should be a property of the target so that more than one target |
| 33 | // at a time can be active... |
| 34 | // |
Chris Lattner | 11d1f21 | 2004-02-23 18:40:08 +0000 | [diff] [blame] | 35 | namespace llvm { |
Chris Lattner | 0742b59 | 2004-02-23 18:38:20 +0000 | [diff] [blame] | 36 | extern const TargetInstrDescriptor *TargetInstrDescriptors; |
| 37 | } |
Ruchira Sasanka | 69917e2 | 2001-10-18 22:40:02 +0000 | [diff] [blame] | 38 | |
Vikram S. Adve | 1885da4 | 2001-07-31 21:49:28 +0000 | [diff] [blame] | 39 | // Constructor for instructions with variable #operands |
Alkis Evlogimenos | ab8672c | 2004-02-12 18:49:07 +0000 | [diff] [blame] | 40 | MachineInstr::MachineInstr(short opcode, unsigned numOperands) |
| 41 | : Opcode(opcode), |
Alkis Evlogimenos | ab8672c | 2004-02-12 18:49:07 +0000 | [diff] [blame] | 42 | operands(numOperands, MachineOperand()), |
| 43 | parent(0) { |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 44 | // Make sure that we get added to a machine basicblock |
| 45 | LeakDetector::addGarbageObject(this); |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 48 | /// MachineInstr ctor - This constructor only does a _reserve_ of the operands, |
| 49 | /// not a resize for them. It is expected that if you use this that you call |
| 50 | /// add* methods below to fill up the operands, instead of the Set methods. |
| 51 | /// Eventually, the "resizing" ctors will be phased out. |
| 52 | /// |
Brian Gaeke | 21326fc | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 53 | MachineInstr::MachineInstr(short opcode, unsigned numOperands, bool XX, bool YY) |
Chris Lattner | 02e5f8d | 2006-04-20 18:08:53 +0000 | [diff] [blame^] | 54 | : Opcode(opcode), parent(0) { |
Chris Lattner | 7279122 | 2002-10-28 20:59:49 +0000 | [diff] [blame] | 55 | operands.reserve(numOperands); |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 56 | // Make sure that we get added to a machine basicblock |
| 57 | LeakDetector::addGarbageObject(this); |
Chris Lattner | 7279122 | 2002-10-28 20:59:49 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 60 | /// MachineInstr ctor - Work exactly the same as the ctor above, except that the |
| 61 | /// MachineInstr is created and added to the end of the specified basic block. |
| 62 | /// |
Alkis Evlogimenos | ab8672c | 2004-02-12 18:49:07 +0000 | [diff] [blame] | 63 | MachineInstr::MachineInstr(MachineBasicBlock *MBB, short opcode, |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 64 | unsigned numOperands) |
Chris Lattner | 02e5f8d | 2006-04-20 18:08:53 +0000 | [diff] [blame^] | 65 | : Opcode(opcode), parent(0) { |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 66 | assert(MBB && "Cannot use inserting ctor with null basic block!"); |
| 67 | operands.reserve(numOperands); |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 68 | // Make sure that we get added to a machine basicblock |
| 69 | LeakDetector::addGarbageObject(this); |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 70 | MBB->push_back(this); // Add instruction to end of basic block! |
| 71 | } |
| 72 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 73 | /// MachineInstr ctor - Copies MachineInstr arg exactly |
| 74 | /// |
Tanya Lattner | 466b534 | 2004-05-23 19:35:12 +0000 | [diff] [blame] | 75 | MachineInstr::MachineInstr(const MachineInstr &MI) { |
| 76 | Opcode = MI.getOpcode(); |
Tanya Lattner | b5159ed | 2004-05-23 20:58:02 +0000 | [diff] [blame] | 77 | operands.reserve(MI.getNumOperands()); |
| 78 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 79 | // Add operands |
| 80 | for (unsigned i = 0; i < MI.getNumOperands(); ++i) |
Tanya Lattner | 466b534 | 2004-05-23 19:35:12 +0000 | [diff] [blame] | 81 | operands.push_back(MachineOperand(MI.getOperand(i))); |
Tanya Lattner | 0c63e03 | 2004-05-24 03:14:18 +0000 | [diff] [blame] | 82 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 83 | // Set parent, next, and prev to null |
Tanya Lattner | 0c63e03 | 2004-05-24 03:14:18 +0000 | [diff] [blame] | 84 | parent = 0; |
| 85 | prev = 0; |
| 86 | next = 0; |
Tanya Lattner | 466b534 | 2004-05-23 19:35:12 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 90 | MachineInstr::~MachineInstr() { |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 91 | LeakDetector::removeGarbageObject(this); |
| 92 | } |
| 93 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 94 | /// clone - Create a copy of 'this' instruction that is identical in all ways |
| 95 | /// except the following: the new instruction has no parent and it has no name |
| 96 | /// |
Tanya Lattner | 0c63e03 | 2004-05-24 03:14:18 +0000 | [diff] [blame] | 97 | MachineInstr* MachineInstr::clone() const { |
Tanya Lattner | b5159ed | 2004-05-23 20:58:02 +0000 | [diff] [blame] | 98 | return new MachineInstr(*this); |
Tanya Lattner | 466b534 | 2004-05-23 19:35:12 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Chris Lattner | 48d7c06 | 2006-04-17 21:35:41 +0000 | [diff] [blame] | 101 | /// removeFromParent - This method unlinks 'this' from the containing basic |
| 102 | /// block, and returns it, but does not delete it. |
| 103 | MachineInstr *MachineInstr::removeFromParent() { |
| 104 | assert(getParent() && "Not embedded in a basic block!"); |
| 105 | getParent()->remove(this); |
| 106 | return this; |
| 107 | } |
| 108 | |
| 109 | |
Brian Gaeke | 21326fc | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 110 | /// OperandComplete - Return true if it's illegal to add a new operand |
| 111 | /// |
Chris Lattner | 2a90ba6 | 2004-02-12 16:09:53 +0000 | [diff] [blame] | 112 | bool MachineInstr::OperandsComplete() const { |
| 113 | int NumOperands = TargetInstrDescriptors[Opcode].numOperands; |
Vikram S. Adve | a2bae30 | 2002-10-29 19:41:18 +0000 | [diff] [blame] | 114 | if (NumOperands >= 0 && getNumOperands() >= (unsigned)NumOperands) |
Vikram S. Adve | 3497782 | 2003-05-31 07:39:06 +0000 | [diff] [blame] | 115 | return true; // Broken: we have all the operands of this instruction! |
Chris Lattner | 413746e | 2002-10-28 20:48:39 +0000 | [diff] [blame] | 116 | return false; |
| 117 | } |
| 118 | |
Chris Lattner | a2dd745 | 2003-08-05 16:58:46 +0000 | [diff] [blame] | 119 | void MachineInstr::SetMachineOperandVal(unsigned i, |
| 120 | MachineOperand::MachineOperandType opTy, |
| 121 | Value* V) { |
Vikram S. Adve | a2bae30 | 2002-10-29 19:41:18 +0000 | [diff] [blame] | 122 | assert(i < operands.size()); // may be explicit or implicit op |
Chris Lattner | a2dd745 | 2003-08-05 16:58:46 +0000 | [diff] [blame] | 123 | operands[i].opType = opTy; |
Brian Gaeke | c548395 | 2004-03-03 19:07:27 +0000 | [diff] [blame] | 124 | operands[i].contents.value = V; |
Chris Lattner | ca4f6eb | 2004-10-15 04:38:41 +0000 | [diff] [blame] | 125 | operands[i].extra.regNum = -1; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void |
Chris Lattner | 572f5c8 | 2002-10-28 04:24:49 +0000 | [diff] [blame] | 129 | MachineInstr::SetMachineOperandConst(unsigned i, |
Brian Gaeke | 21326fc | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 130 | MachineOperand::MachineOperandType opTy, |
Chris Lattner | 561c010 | 2004-02-29 05:07:02 +0000 | [diff] [blame] | 131 | int intValue) { |
Vikram S. Adve | a2bae30 | 2002-10-29 19:41:18 +0000 | [diff] [blame] | 132 | assert(i < getNumOperands()); // must be explicit op |
Chris Lattner | 2a90ba6 | 2004-02-12 16:09:53 +0000 | [diff] [blame] | 133 | assert(TargetInstrDescriptors[Opcode].resultPos != (int) i && |
Vikram S. Adve | c356e56 | 2002-03-18 03:35:24 +0000 | [diff] [blame] | 134 | "immed. constant cannot be defined"); |
Chris Lattner | 572f5c8 | 2002-10-28 04:24:49 +0000 | [diff] [blame] | 135 | |
Brian Gaeke | 21326fc | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 136 | operands[i].opType = opTy; |
Brian Gaeke | c548395 | 2004-03-03 19:07:27 +0000 | [diff] [blame] | 137 | operands[i].contents.value = NULL; |
| 138 | operands[i].contents.immedVal = intValue; |
Chris Lattner | ca4f6eb | 2004-10-15 04:38:41 +0000 | [diff] [blame] | 139 | operands[i].extra.regNum = -1; |
Chris Lattner | 572f5c8 | 2002-10-28 04:24:49 +0000 | [diff] [blame] | 140 | operands[i].flags = 0; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Chris Lattner | a2dd745 | 2003-08-05 16:58:46 +0000 | [diff] [blame] | 143 | void MachineInstr::SetMachineOperandReg(unsigned i, int regNum) { |
Vikram S. Adve | a2bae30 | 2002-10-29 19:41:18 +0000 | [diff] [blame] | 144 | assert(i < getNumOperands()); // must be explicit op |
Chris Lattner | 572f5c8 | 2002-10-28 04:24:49 +0000 | [diff] [blame] | 145 | |
Chris Lattner | 2f30598 | 2002-10-28 19:46:59 +0000 | [diff] [blame] | 146 | operands[i].opType = MachineOperand::MO_MachineRegister; |
Brian Gaeke | c548395 | 2004-03-03 19:07:27 +0000 | [diff] [blame] | 147 | operands[i].contents.value = NULL; |
Chris Lattner | ca4f6eb | 2004-10-15 04:38:41 +0000 | [diff] [blame] | 148 | operands[i].extra.regNum = regNum; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Brian Gaeke | 21326fc | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 151 | void MachineInstr::dump() const { |
Chris Lattner | 925b771 | 2003-08-03 20:24:29 +0000 | [diff] [blame] | 152 | std::cerr << " " << *this; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Brian Gaeke | 21326fc | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 155 | static inline std::ostream& OutputValue(std::ostream &os, const Value* val) { |
Vikram S. Adve | 93240fe | 2002-04-25 04:31:18 +0000 | [diff] [blame] | 156 | os << "(val "; |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 157 | os << (void*) val; // print address always |
Vikram S. Adve | 93240fe | 2002-04-25 04:31:18 +0000 | [diff] [blame] | 158 | if (val && val->hasName()) |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 159 | os << " " << val->getName(); // print name also, if available |
Brian Gaeke | 21326fc | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 160 | os << ")"; |
Vikram S. Adve | 627eb31 | 2003-07-10 19:45:07 +0000 | [diff] [blame] | 161 | return os; |
Vikram S. Adve | 93240fe | 2002-04-25 04:31:18 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Chris Lattner | 2a79a09 | 2002-10-30 00:58:19 +0000 | [diff] [blame] | 164 | static inline void OutputReg(std::ostream &os, unsigned RegNo, |
| 165 | const MRegisterInfo *MRI = 0) { |
Alkis Evlogimenos | ddcfd9e | 2004-02-27 01:52:34 +0000 | [diff] [blame] | 166 | if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) { |
Chris Lattner | 8517e1f | 2004-02-19 16:17:08 +0000 | [diff] [blame] | 167 | if (MRI) |
Chris Lattner | 2a79a09 | 2002-10-30 00:58:19 +0000 | [diff] [blame] | 168 | os << "%" << MRI->get(RegNo).Name; |
| 169 | else |
Chris Lattner | 8517e1f | 2004-02-19 16:17:08 +0000 | [diff] [blame] | 170 | os << "%mreg(" << RegNo << ")"; |
Chris Lattner | 2a79a09 | 2002-10-30 00:58:19 +0000 | [diff] [blame] | 171 | } else |
Chris Lattner | 8517e1f | 2004-02-19 16:17:08 +0000 | [diff] [blame] | 172 | os << "%reg" << RegNo; |
Vikram S. Adve | 8c6936a | 2002-09-16 15:18:53 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 175 | static void print(const MachineOperand &MO, std::ostream &OS, |
Tanya Lattner | b140762 | 2004-06-25 00:13:11 +0000 | [diff] [blame] | 176 | const TargetMachine *TM) { |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 177 | const MRegisterInfo *MRI = 0; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 178 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 179 | if (TM) MRI = TM->getRegisterInfo(); |
Tanya Lattner | b140762 | 2004-06-25 00:13:11 +0000 | [diff] [blame] | 180 | |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 181 | bool CloseParen = true; |
Alkis Evlogimenos | 4d7af65 | 2003-12-14 13:24:17 +0000 | [diff] [blame] | 182 | if (MO.isHiBits32()) |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 183 | OS << "%lm("; |
Alkis Evlogimenos | 4d7af65 | 2003-12-14 13:24:17 +0000 | [diff] [blame] | 184 | else if (MO.isLoBits32()) |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 185 | OS << "%lo("; |
Alkis Evlogimenos | 4d7af65 | 2003-12-14 13:24:17 +0000 | [diff] [blame] | 186 | else if (MO.isHiBits64()) |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 187 | OS << "%hh("; |
Alkis Evlogimenos | 4d7af65 | 2003-12-14 13:24:17 +0000 | [diff] [blame] | 188 | else if (MO.isLoBits64()) |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 189 | OS << "%hm("; |
| 190 | else |
| 191 | CloseParen = false; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 192 | |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 193 | switch (MO.getType()) { |
| 194 | case MachineOperand::MO_VirtualRegister: |
| 195 | if (MO.getVRegValue()) { |
| 196 | OS << "%reg"; |
| 197 | OutputValue(OS, MO.getVRegValue()); |
| 198 | if (MO.hasAllocatedReg()) |
| 199 | OS << "=="; |
| 200 | } |
| 201 | if (MO.hasAllocatedReg()) |
Alkis Evlogimenos | be766c7 | 2004-02-13 21:01:20 +0000 | [diff] [blame] | 202 | OutputReg(OS, MO.getReg(), MRI); |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 203 | break; |
| 204 | case MachineOperand::MO_CCRegister: |
| 205 | OS << "%ccreg"; |
| 206 | OutputValue(OS, MO.getVRegValue()); |
| 207 | if (MO.hasAllocatedReg()) { |
| 208 | OS << "=="; |
Alkis Evlogimenos | be766c7 | 2004-02-13 21:01:20 +0000 | [diff] [blame] | 209 | OutputReg(OS, MO.getReg(), MRI); |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 210 | } |
| 211 | break; |
| 212 | case MachineOperand::MO_MachineRegister: |
Chris Lattner | 2a79a09 | 2002-10-30 00:58:19 +0000 | [diff] [blame] | 213 | OutputReg(OS, MO.getMachineRegNum(), MRI); |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 214 | break; |
| 215 | case MachineOperand::MO_SignExtendedImmed: |
| 216 | OS << (long)MO.getImmedValue(); |
| 217 | break; |
| 218 | case MachineOperand::MO_UnextendedImmed: |
| 219 | OS << (long)MO.getImmedValue(); |
| 220 | break; |
| 221 | case MachineOperand::MO_PCRelativeDisp: { |
| 222 | const Value* opVal = MO.getVRegValue(); |
| 223 | bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal); |
| 224 | OS << "%disp(" << (isLabel? "label " : "addr-of-val "); |
| 225 | if (opVal->hasName()) |
| 226 | OS << opVal->getName(); |
| 227 | else |
| 228 | OS << (const void*) opVal; |
| 229 | OS << ")"; |
| 230 | break; |
| 231 | } |
Chris Lattner | 2109f50 | 2002-12-15 20:35:25 +0000 | [diff] [blame] | 232 | case MachineOperand::MO_MachineBasicBlock: |
Brian Gaeke | 988b7ba | 2004-06-17 22:26:53 +0000 | [diff] [blame] | 233 | OS << "mbb<" |
Chris Lattner | 2109f50 | 2002-12-15 20:35:25 +0000 | [diff] [blame] | 234 | << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName() |
Brian Gaeke | 988b7ba | 2004-06-17 22:26:53 +0000 | [diff] [blame] | 235 | << "," << (void*)MO.getMachineBasicBlock() << ">"; |
Chris Lattner | 2109f50 | 2002-12-15 20:35:25 +0000 | [diff] [blame] | 236 | break; |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 237 | case MachineOperand::MO_FrameIndex: |
| 238 | OS << "<fi#" << MO.getFrameIndex() << ">"; |
| 239 | break; |
Chris Lattner | 8d95ef4 | 2003-01-13 00:23:24 +0000 | [diff] [blame] | 240 | case MachineOperand::MO_ConstantPoolIndex: |
| 241 | OS << "<cp#" << MO.getConstantPoolIndex() << ">"; |
| 242 | break; |
| 243 | case MachineOperand::MO_GlobalAddress: |
Chris Lattner | ca4f6eb | 2004-10-15 04:38:41 +0000 | [diff] [blame] | 244 | OS << "<ga:" << ((Value*)MO.getGlobal())->getName(); |
| 245 | if (MO.getOffset()) OS << "+" << MO.getOffset(); |
| 246 | OS << ">"; |
Chris Lattner | 8d95ef4 | 2003-01-13 00:23:24 +0000 | [diff] [blame] | 247 | break; |
| 248 | case MachineOperand::MO_ExternalSymbol: |
Chris Lattner | ca4f6eb | 2004-10-15 04:38:41 +0000 | [diff] [blame] | 249 | OS << "<es:" << MO.getSymbolName(); |
| 250 | if (MO.getOffset()) OS << "+" << MO.getOffset(); |
| 251 | OS << ">"; |
Chris Lattner | 8d95ef4 | 2003-01-13 00:23:24 +0000 | [diff] [blame] | 252 | break; |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 253 | default: |
| 254 | assert(0 && "Unrecognized operand type"); |
| 255 | } |
| 256 | |
| 257 | if (CloseParen) |
| 258 | OS << ")"; |
| 259 | } |
| 260 | |
Tanya Lattner | b140762 | 2004-06-25 00:13:11 +0000 | [diff] [blame] | 261 | void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const { |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 262 | unsigned StartOp = 0; |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 263 | |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 264 | // Specialize printing if op#0 is definition |
Alkis Evlogimenos | 4d7af65 | 2003-12-14 13:24:17 +0000 | [diff] [blame] | 265 | if (getNumOperands() && getOperand(0).isDef() && !getOperand(0).isUse()) { |
Chris Lattner | 0742b59 | 2004-02-23 18:38:20 +0000 | [diff] [blame] | 266 | ::print(getOperand(0), OS, TM); |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 267 | OS << " = "; |
| 268 | ++StartOp; // Don't print this operand again! |
| 269 | } |
Tanya Lattner | b140762 | 2004-06-25 00:13:11 +0000 | [diff] [blame] | 270 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 271 | // Must check if Target machine is not null because machine BB could not |
| 272 | // be attached to a Machine function yet |
| 273 | if (TM) |
Tanya Lattner | b140762 | 2004-06-25 00:13:11 +0000 | [diff] [blame] | 274 | OS << TM->getInstrInfo()->getName(getOpcode()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 276 | for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) { |
Vikram S. Adve | 5f2180c | 2003-05-27 00:05:23 +0000 | [diff] [blame] | 277 | const MachineOperand& mop = getOperand(i); |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 278 | if (i != StartOp) |
| 279 | OS << ","; |
| 280 | OS << " "; |
Chris Lattner | 0742b59 | 2004-02-23 18:38:20 +0000 | [diff] [blame] | 281 | ::print(mop, OS, TM); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 282 | |
Alkis Evlogimenos | 4d7af65 | 2003-12-14 13:24:17 +0000 | [diff] [blame] | 283 | if (mop.isDef()) |
| 284 | if (mop.isUse()) |
| 285 | OS << "<def&use>"; |
| 286 | else |
| 287 | OS << "<def>"; |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 288 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 289 | |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 290 | OS << "\n"; |
| 291 | } |
| 292 | |
Chris Lattner | 11d1f21 | 2004-02-23 18:40:08 +0000 | [diff] [blame] | 293 | namespace llvm { |
Chris Lattner | 8517e1f | 2004-02-19 16:17:08 +0000 | [diff] [blame] | 294 | std::ostream &operator<<(std::ostream &os, const MachineInstr &MI) { |
| 295 | // If the instruction is embedded into a basic block, we can find the target |
| 296 | // info for the instruction. |
| 297 | if (const MachineBasicBlock *MBB = MI.getParent()) { |
| 298 | const MachineFunction *MF = MBB->getParent(); |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 299 | if (MF) |
Tanya Lattner | b140762 | 2004-06-25 00:13:11 +0000 | [diff] [blame] | 300 | MI.print(os, &MF->getTarget()); |
| 301 | else |
| 302 | MI.print(os, 0); |
Chris Lattner | 8517e1f | 2004-02-19 16:17:08 +0000 | [diff] [blame] | 303 | return os; |
| 304 | } |
| 305 | |
| 306 | // Otherwise, print it out in the "raw" format without symbolic register names |
| 307 | // and such. |
Chris Lattner | 2a90ba6 | 2004-02-12 16:09:53 +0000 | [diff] [blame] | 308 | os << TargetInstrDescriptors[MI.getOpcode()].Name; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 309 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 310 | for (unsigned i = 0, N = MI.getNumOperands(); i < N; i++) { |
Chris Lattner | 8d95ef4 | 2003-01-13 00:23:24 +0000 | [diff] [blame] | 311 | os << "\t" << MI.getOperand(i); |
Alkis Evlogimenos | 4d7af65 | 2003-12-14 13:24:17 +0000 | [diff] [blame] | 312 | if (MI.getOperand(i).isDef()) |
| 313 | if (MI.getOperand(i).isUse()) |
| 314 | os << "<d&u>"; |
| 315 | else |
| 316 | os << "<d>"; |
Ruchira Sasanka | 8d24337 | 2001-11-14 20:05:23 +0000 | [diff] [blame] | 317 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 318 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 319 | return os << "\n"; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Brian Gaeke | 21326fc | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 322 | std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) { |
Alkis Evlogimenos | 4d7af65 | 2003-12-14 13:24:17 +0000 | [diff] [blame] | 323 | if (MO.isHiBits32()) |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 324 | OS << "%lm("; |
Alkis Evlogimenos | 4d7af65 | 2003-12-14 13:24:17 +0000 | [diff] [blame] | 325 | else if (MO.isLoBits32()) |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 326 | OS << "%lo("; |
Alkis Evlogimenos | 4d7af65 | 2003-12-14 13:24:17 +0000 | [diff] [blame] | 327 | else if (MO.isHiBits64()) |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 328 | OS << "%hh("; |
Alkis Evlogimenos | 4d7af65 | 2003-12-14 13:24:17 +0000 | [diff] [blame] | 329 | else if (MO.isLoBits64()) |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 330 | OS << "%hm("; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 331 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 332 | switch (MO.getType()) { |
| 333 | case MachineOperand::MO_VirtualRegister: |
| 334 | if (MO.hasAllocatedReg()) |
| 335 | OutputReg(OS, MO.getReg()); |
Chris Lattner | 8d95ef4 | 2003-01-13 00:23:24 +0000 | [diff] [blame] | 336 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 337 | if (MO.getVRegValue()) { |
| 338 | if (MO.hasAllocatedReg()) OS << "=="; |
| 339 | OS << "%vreg"; |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 340 | OutputValue(OS, MO.getVRegValue()); |
Vikram S. Adve | 6e44718 | 2001-09-18 12:56:28 +0000 | [diff] [blame] | 341 | } |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 342 | break; |
| 343 | case MachineOperand::MO_CCRegister: |
| 344 | OS << "%ccreg"; |
| 345 | OutputValue(OS, MO.getVRegValue()); |
| 346 | if (MO.hasAllocatedReg()) { |
| 347 | OS << "=="; |
| 348 | OutputReg(OS, MO.getReg()); |
| 349 | } |
| 350 | break; |
| 351 | case MachineOperand::MO_MachineRegister: |
| 352 | OutputReg(OS, MO.getMachineRegNum()); |
| 353 | break; |
| 354 | case MachineOperand::MO_SignExtendedImmed: |
| 355 | OS << (long)MO.getImmedValue(); |
| 356 | break; |
| 357 | case MachineOperand::MO_UnextendedImmed: |
| 358 | OS << (long)MO.getImmedValue(); |
| 359 | break; |
| 360 | case MachineOperand::MO_PCRelativeDisp: { |
| 361 | const Value* opVal = MO.getVRegValue(); |
| 362 | bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal); |
| 363 | OS << "%disp(" << (isLabel? "label " : "addr-of-val "); |
| 364 | if (opVal->hasName()) |
| 365 | OS << opVal->getName(); |
| 366 | else |
| 367 | OS << (const void*) opVal; |
| 368 | OS << ")"; |
| 369 | break; |
| 370 | } |
| 371 | case MachineOperand::MO_MachineBasicBlock: |
| 372 | OS << "<mbb:" |
| 373 | << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName() |
| 374 | << "@" << (void*)MO.getMachineBasicBlock() << ">"; |
| 375 | break; |
| 376 | case MachineOperand::MO_FrameIndex: |
| 377 | OS << "<fi#" << MO.getFrameIndex() << ">"; |
| 378 | break; |
| 379 | case MachineOperand::MO_ConstantPoolIndex: |
| 380 | OS << "<cp#" << MO.getConstantPoolIndex() << ">"; |
| 381 | break; |
| 382 | case MachineOperand::MO_GlobalAddress: |
| 383 | OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">"; |
| 384 | break; |
| 385 | case MachineOperand::MO_ExternalSymbol: |
| 386 | OS << "<es:" << MO.getSymbolName() << ">"; |
| 387 | break; |
| 388 | default: |
| 389 | assert(0 && "Unrecognized operand type"); |
| 390 | break; |
| 391 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 392 | |
Chris Lattner | 0742b59 | 2004-02-23 18:38:20 +0000 | [diff] [blame] | 393 | if (MO.isHiBits32() || MO.isLoBits32() || MO.isHiBits64() || MO.isLoBits64()) |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 394 | OS << ")"; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 395 | |
Chris Lattner | 10cb79b | 2002-12-28 20:37:37 +0000 | [diff] [blame] | 396 | return OS; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 397 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 398 | |
Chris Lattner | 11d1f21 | 2004-02-23 18:40:08 +0000 | [diff] [blame] | 399 | } |