Chris Lattner | f7e7948 | 2002-04-07 22:31:46 +0000 | [diff] [blame] | 1 | //===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===// |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
| 10 | // This library implements the functionality defined in llvm/Assembly/Writer.h |
| 11 | // |
Chris Lattner | 189088e | 2002-04-12 18:21:53 +0000 | [diff] [blame] | 12 | // Note that these routines must be extremely tolerant of various errors in the |
Chris Lattner | f70da10 | 2003-05-08 02:44:12 +0000 | [diff] [blame] | 13 | // LLVM code, because it can be used for debugging transformations. |
Chris Lattner | 189088e | 2002-04-12 18:21:53 +0000 | [diff] [blame] | 14 | // |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 7db7958 | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 17 | #include "llvm/Assembly/CachedWriter.h" |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 18 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 7f8845a | 2002-07-23 18:07:49 +0000 | [diff] [blame] | 19 | #include "llvm/Assembly/PrintModulePass.h" |
Chris Lattner | 8339f7d | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 20 | #include "llvm/Assembly/AsmAnnotationWriter.h" |
Chris Lattner | c70b3f6 | 2004-01-20 19:50:34 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" |
Chris Lattner | 913d18f | 2002-04-29 18:46:50 +0000 | [diff] [blame] | 22 | #include "llvm/DerivedTypes.h" |
Vikram S. Adve | b952b54 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 23 | #include "llvm/Instruction.h" |
Misha Brukman | 2d3fa9e | 2004-07-29 16:53:53 +0000 | [diff] [blame] | 24 | #include "llvm/Instructions.h" |
Chris Lattner | c70b3f6 | 2004-01-20 19:50:34 +0000 | [diff] [blame] | 25 | #include "llvm/Module.h" |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 26 | #include "llvm/SymbolTable.h" |
Misha Brukman | 4685e26 | 2004-04-28 15:31:21 +0000 | [diff] [blame] | 27 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 58185f2 | 2002-10-02 19:38:55 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CFG.h" |
Chris Lattner | 5de2204 | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 29 | #include "Support/StringExtras.h" |
| 30 | #include "Support/STLExtras.h" |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 31 | #include <algorithm> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 32 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 60a7dd1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 34 | namespace llvm { |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 35 | |
| 36 | /// This class provides computation of slot numbers for LLVM Assembly writing. |
| 37 | /// @brief LLVM Assembly Writing Slot Computation. |
| 38 | class SlotMachine { |
| 39 | |
| 40 | /// @name Types |
| 41 | /// @{ |
| 42 | public: |
| 43 | |
| 44 | /// @brief A mapping of Values to slot numbers |
| 45 | typedef std::map<const Value*, unsigned> ValueMap; |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 46 | typedef std::map<const Type*, unsigned> TypeMap; |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 47 | |
| 48 | /// @brief A plane with next slot number and ValueMap |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 49 | struct ValuePlane { |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 50 | unsigned next_slot; ///< The next slot number to use |
| 51 | ValueMap map; ///< The map of Value* -> unsigned |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 52 | ValuePlane() { next_slot = 0; } ///< Make sure we start at 0 |
| 53 | }; |
| 54 | |
| 55 | struct TypePlane { |
| 56 | unsigned next_slot; |
| 57 | TypeMap map; |
| 58 | TypePlane() { next_slot = 0; } |
| 59 | void clear() { map.clear(); next_slot = 0; } |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | /// @brief The map of planes by Type |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 63 | typedef std::map<const Type*, ValuePlane> TypedPlanes; |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 64 | |
| 65 | /// @} |
| 66 | /// @name Constructors |
| 67 | /// @{ |
| 68 | public: |
| 69 | /// @brief Construct from a module |
| 70 | SlotMachine(const Module *M ); |
| 71 | |
| 72 | /// @brief Construct from a function, starting out in incorp state. |
| 73 | SlotMachine(const Function *F ); |
| 74 | |
| 75 | /// @} |
| 76 | /// @name Accessors |
| 77 | /// @{ |
| 78 | public: |
| 79 | /// Return the slot number of the specified value in it's type |
| 80 | /// plane. Its an error to ask for something not in the SlotMachine. |
| 81 | /// Its an error to ask for a Type* |
Chris Lattner | 757ee0b | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 82 | int getSlot(const Value *V); |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 83 | int getSlot(const Type*Ty); |
Reid Spencer | 8beac69 | 2004-06-09 15:26:53 +0000 | [diff] [blame] | 84 | |
| 85 | /// Determine if a Value has a slot or not |
| 86 | bool hasSlot(const Value* V); |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 87 | bool hasSlot(const Type* Ty); |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 88 | |
| 89 | /// @} |
| 90 | /// @name Mutators |
| 91 | /// @{ |
| 92 | public: |
| 93 | /// If you'd like to deal with a function instead of just a module, use |
| 94 | /// this method to get its data into the SlotMachine. |
Reid Spencer | b0ac8c4 | 2004-08-16 07:46:33 +0000 | [diff] [blame^] | 95 | void incorporateFunction(const Function *F) { |
| 96 | TheFunction = F; |
| 97 | FunctionProcessed = false; |
| 98 | } |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 99 | |
| 100 | /// After calling incorporateFunction, use this method to remove the |
| 101 | /// most recently incorporated function from the SlotMachine. This |
| 102 | /// will reset the state of the machine back to just the module contents. |
| 103 | void purgeFunction(); |
| 104 | |
| 105 | /// @} |
| 106 | /// @name Implementation Details |
| 107 | /// @{ |
| 108 | private: |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 109 | /// This function does the actual initialization. |
| 110 | inline void initialize(); |
| 111 | |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 112 | /// Values can be crammed into here at will. If they haven't |
| 113 | /// been inserted already, they get inserted, otherwise they are ignored. |
| 114 | /// Either way, the slot number for the Value* is returned. |
| 115 | unsigned createSlot(const Value *V); |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 116 | unsigned createSlot(const Type* Ty); |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 117 | |
| 118 | /// Insert a value into the value table. Return the slot number |
| 119 | /// that it now occupies. BadThings(TM) will happen if you insert a |
| 120 | /// Value that's already been inserted. |
| 121 | unsigned insertValue( const Value *V ); |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 122 | unsigned insertValue( const Type* Ty); |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 123 | |
| 124 | /// Add all of the module level global variables (and their initializers) |
| 125 | /// and function declarations, but not the contents of those functions. |
| 126 | void processModule(); |
| 127 | |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 128 | /// Add all of the functions arguments, basic blocks, and instructions |
| 129 | void processFunction(); |
| 130 | |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 131 | SlotMachine(const SlotMachine &); // DO NOT IMPLEMENT |
| 132 | void operator=(const SlotMachine &); // DO NOT IMPLEMENT |
| 133 | |
| 134 | /// @} |
| 135 | /// @name Data |
| 136 | /// @{ |
| 137 | public: |
| 138 | |
| 139 | /// @brief The module for which we are holding slot numbers |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 140 | const Module* TheModule; |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 141 | |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 142 | /// @brief The function for which we are holding slot numbers |
| 143 | const Function* TheFunction; |
Reid Spencer | b0ac8c4 | 2004-08-16 07:46:33 +0000 | [diff] [blame^] | 144 | bool FunctionProcessed; |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 145 | |
| 146 | /// @brief The TypePlanes map for the module level data |
| 147 | TypedPlanes mMap; |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 148 | TypePlane mTypes; |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 149 | |
| 150 | /// @brief The TypePlanes map for the function level data |
| 151 | TypedPlanes fMap; |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 152 | TypePlane fTypes; |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 153 | |
| 154 | /// @} |
| 155 | |
| 156 | }; |
| 157 | |
Chris Lattner | 60a7dd1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 158 | } // end namespace llvm |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 159 | |
Chris Lattner | c8b7092 | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 160 | static RegisterPass<PrintModulePass> |
| 161 | X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization); |
| 162 | static RegisterPass<PrintFunctionPass> |
| 163 | Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization); |
Chris Lattner | 7f8845a | 2002-07-23 18:07:49 +0000 | [diff] [blame] | 164 | |
Chris Lattner | ab7d1ab | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 165 | static void WriteAsOperandInternal(std::ostream &Out, const Value *V, |
| 166 | bool PrintName, |
| 167 | std::map<const Type *, std::string> &TypeTable, |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 168 | SlotMachine *Machine); |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 169 | |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 170 | static void WriteAsOperandInternal(std::ostream &Out, const Type *T, |
| 171 | bool PrintName, |
| 172 | std::map<const Type *, std::string> &TypeTable, |
| 173 | SlotMachine *Machine); |
| 174 | |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 175 | static const Module *getModuleFromVal(const Value *V) { |
Chris Lattner | f26a8ee | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 176 | if (const Argument *MA = dyn_cast<Argument>(V)) |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 177 | return MA->getParent() ? MA->getParent()->getParent() : 0; |
Chris Lattner | f26a8ee | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 178 | else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 179 | return BB->getParent() ? BB->getParent()->getParent() : 0; |
Chris Lattner | f26a8ee | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 180 | else if (const Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 181 | const Function *M = I->getParent() ? I->getParent()->getParent() : 0; |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 182 | return M ? M->getParent() : 0; |
Chris Lattner | f26a8ee | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 183 | } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 184 | return GV->getParent(); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 188 | static SlotMachine *createSlotMachine(const Value *V) { |
Chris Lattner | f26a8ee | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 189 | if (const Argument *FA = dyn_cast<Argument>(V)) { |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 190 | return new SlotMachine(FA->getParent()); |
Chris Lattner | f26a8ee | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 191 | } else if (const Instruction *I = dyn_cast<Instruction>(V)) { |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 192 | return new SlotMachine(I->getParent()->getParent()); |
Chris Lattner | f26a8ee | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 193 | } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) { |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 194 | return new SlotMachine(BB->getParent()); |
Chris Lattner | f26a8ee | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 195 | } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){ |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 196 | return new SlotMachine(GV->getParent()); |
Chris Lattner | f26a8ee | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 197 | } else if (const Function *Func = dyn_cast<Function>(V)) { |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 198 | return new SlotMachine(Func); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 199 | } |
| 200 | return 0; |
| 201 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 202 | |
Chris Lattner | 1c34304 | 2003-08-22 05:40:38 +0000 | [diff] [blame] | 203 | // getLLVMName - Turn the specified string into an 'LLVM name', which is either |
| 204 | // prefixed with % (if the string only contains simple characters) or is |
| 205 | // surrounded with ""'s (if it has special chars in it). |
| 206 | static std::string getLLVMName(const std::string &Name) { |
| 207 | assert(!Name.empty() && "Cannot get empty name!"); |
| 208 | |
| 209 | // First character cannot start with a number... |
| 210 | if (Name[0] >= '0' && Name[0] <= '9') |
| 211 | return "\"" + Name + "\""; |
| 212 | |
| 213 | // Scan to see if we have any characters that are not on the "white list" |
| 214 | for (unsigned i = 0, e = Name.size(); i != e; ++i) { |
| 215 | char C = Name[i]; |
| 216 | assert(C != '"' && "Illegal character in LLVM value name!"); |
| 217 | if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') && |
| 218 | C != '-' && C != '.' && C != '_') |
| 219 | return "\"" + Name + "\""; |
| 220 | } |
| 221 | |
| 222 | // If we get here, then the identifier is legal to use as a "VarID". |
| 223 | return "%"+Name; |
| 224 | } |
| 225 | |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 226 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 227 | /// fillTypeNameTable - If the module has a symbol table, take all global types |
| 228 | /// and stuff their names into the TypeNames map. |
| 229 | /// |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 230 | static void fillTypeNameTable(const Module *M, |
Chris Lattner | ab7d1ab | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 231 | std::map<const Type *, std::string> &TypeNames) { |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 232 | if (!M) return; |
| 233 | const SymbolTable &ST = M->getSymbolTable(); |
Reid Spencer | e7e9671 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 234 | SymbolTable::type_const_iterator TI = ST.type_begin(); |
| 235 | for (; TI != ST.type_end(); ++TI ) { |
| 236 | // As a heuristic, don't insert pointer to primitive types, because |
| 237 | // they are used too often to have a single useful name. |
| 238 | // |
| 239 | const Type *Ty = cast<Type>(TI->second); |
| 240 | if (!isa<PointerType>(Ty) || |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 241 | !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() || |
| 242 | isa<OpaqueType>(cast<PointerType>(Ty)->getElementType())) |
Reid Spencer | e7e9671 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 243 | TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first))); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
| 247 | |
| 248 | |
John Criswell | cd116ba | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 249 | static void calcTypeName(const Type *Ty, |
| 250 | std::vector<const Type *> &TypeStack, |
| 251 | std::map<const Type *, std::string> &TypeNames, |
| 252 | std::string & Result){ |
| 253 | if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)) { |
| 254 | Result += Ty->getDescription(); // Base case |
| 255 | return; |
| 256 | } |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 257 | |
| 258 | // Check to see if the type is named. |
Chris Lattner | ab7d1ab | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 259 | std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty); |
John Criswell | cd116ba | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 260 | if (I != TypeNames.end()) { |
| 261 | Result += I->second; |
| 262 | return; |
| 263 | } |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 264 | |
John Criswell | cd116ba | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 265 | if (isa<OpaqueType>(Ty)) { |
| 266 | Result += "opaque"; |
| 267 | return; |
| 268 | } |
Chris Lattner | f14ead9 | 2003-10-30 00:22:33 +0000 | [diff] [blame] | 269 | |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 270 | // Check to see if the Type is already on the stack... |
| 271 | unsigned Slot = 0, CurSize = TypeStack.size(); |
| 272 | while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type |
| 273 | |
| 274 | // This is another base case for the recursion. In this case, we know |
| 275 | // that we have looped back to a type that we have previously visited. |
| 276 | // Generate the appropriate upreference to handle this. |
John Criswell | cd116ba | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 277 | if (Slot < CurSize) { |
| 278 | Result += "\\" + utostr(CurSize-Slot); // Here's the upreference |
| 279 | return; |
| 280 | } |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 281 | |
| 282 | TypeStack.push_back(Ty); // Recursive case: Add us to the stack.. |
| 283 | |
Chris Lattner | 6b72759 | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 284 | switch (Ty->getTypeID()) { |
Chris Lattner | 91db582 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 285 | case Type::FunctionTyID: { |
Chris Lattner | f26a8ee | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 286 | const FunctionType *FTy = cast<FunctionType>(Ty); |
John Criswell | cd116ba | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 287 | calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result); |
| 288 | Result += " ("; |
Chris Lattner | fa829be | 2004-02-09 04:14:01 +0000 | [diff] [blame] | 289 | for (FunctionType::param_iterator I = FTy->param_begin(), |
| 290 | E = FTy->param_end(); I != E; ++I) { |
| 291 | if (I != FTy->param_begin()) |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 292 | Result += ", "; |
John Criswell | cd116ba | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 293 | calcTypeName(*I, TypeStack, TypeNames, Result); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 294 | } |
Chris Lattner | d816b53 | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 295 | if (FTy->isVarArg()) { |
Chris Lattner | fa829be | 2004-02-09 04:14:01 +0000 | [diff] [blame] | 296 | if (FTy->getNumParams()) Result += ", "; |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 297 | Result += "..."; |
| 298 | } |
| 299 | Result += ")"; |
| 300 | break; |
| 301 | } |
| 302 | case Type::StructTyID: { |
Chris Lattner | f26a8ee | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 303 | const StructType *STy = cast<StructType>(Ty); |
John Criswell | cd116ba | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 304 | Result += "{ "; |
Chris Lattner | ac6db75 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 305 | for (StructType::element_iterator I = STy->element_begin(), |
| 306 | E = STy->element_end(); I != E; ++I) { |
| 307 | if (I != STy->element_begin()) |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 308 | Result += ", "; |
John Criswell | cd116ba | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 309 | calcTypeName(*I, TypeStack, TypeNames, Result); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 310 | } |
| 311 | Result += " }"; |
| 312 | break; |
| 313 | } |
| 314 | case Type::PointerTyID: |
John Criswell | cd116ba | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 315 | calcTypeName(cast<PointerType>(Ty)->getElementType(), |
| 316 | TypeStack, TypeNames, Result); |
| 317 | Result += "*"; |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 318 | break; |
| 319 | case Type::ArrayTyID: { |
Chris Lattner | f26a8ee | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 320 | const ArrayType *ATy = cast<ArrayType>(Ty); |
John Criswell | cd116ba | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 321 | Result += "[" + utostr(ATy->getNumElements()) + " x "; |
| 322 | calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result); |
| 323 | Result += "]"; |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 324 | break; |
| 325 | } |
Chris Lattner | 15285ab | 2003-05-14 17:50:47 +0000 | [diff] [blame] | 326 | case Type::OpaqueTyID: |
John Criswell | cd116ba | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 327 | Result += "opaque"; |
Chris Lattner | 15285ab | 2003-05-14 17:50:47 +0000 | [diff] [blame] | 328 | break; |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 329 | default: |
John Criswell | cd116ba | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 330 | Result += "<unrecognized-type>"; |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | TypeStack.pop_back(); // Remove self from stack... |
John Criswell | cd116ba | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 334 | return; |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | |
Misha Brukman | b22d09c | 2004-03-01 19:48:13 +0000 | [diff] [blame] | 338 | /// printTypeInt - The internal guts of printing out a type that has a |
| 339 | /// potentially named portion. |
| 340 | /// |
Chris Lattner | ab7d1ab | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 341 | static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty, |
| 342 | std::map<const Type *, std::string> &TypeNames) { |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 343 | // Primitive types always print out their description, regardless of whether |
| 344 | // they have been named or not. |
| 345 | // |
Chris Lattner | 92d6053 | 2003-10-30 00:12:51 +0000 | [diff] [blame] | 346 | if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)) |
| 347 | return Out << Ty->getDescription(); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 348 | |
| 349 | // Check to see if the type is named. |
Chris Lattner | ab7d1ab | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 350 | std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 351 | if (I != TypeNames.end()) return Out << I->second; |
| 352 | |
| 353 | // Otherwise we have a type that has not been named but is a derived type. |
| 354 | // Carefully recurse the type hierarchy to print out any contained symbolic |
| 355 | // names. |
| 356 | // |
Chris Lattner | ab7d1ab | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 357 | std::vector<const Type *> TypeStack; |
John Criswell | cd116ba | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 358 | std::string TypeName; |
| 359 | calcTypeName(Ty, TypeStack, TypeNames, TypeName); |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 360 | TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use |
John Criswell | cd116ba | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 361 | return (Out << TypeName); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Chris Lattner | 34b9518 | 2001-10-31 04:33:19 +0000 | [diff] [blame] | 364 | |
Misha Brukman | b22d09c | 2004-03-01 19:48:13 +0000 | [diff] [blame] | 365 | /// WriteTypeSymbolic - This attempts to write the specified type as a symbolic |
| 366 | /// type, iff there is an entry in the modules symbol table for the specified |
| 367 | /// type or one of it's component types. This is slower than a simple x << Type |
| 368 | /// |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 369 | std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty, |
| 370 | const Module *M) { |
Misha Brukman | 21bbdb9 | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 371 | Out << ' '; |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 372 | |
| 373 | // If they want us to print out a type, attempt to make it symbolic if there |
| 374 | // is a symbol table in the module... |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 375 | if (M) { |
Chris Lattner | ab7d1ab | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 376 | std::map<const Type *, std::string> TypeNames; |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 377 | fillTypeNameTable(M, TypeNames); |
| 378 | |
Chris Lattner | 72f866e | 2001-10-29 16:40:32 +0000 | [diff] [blame] | 379 | return printTypeInt(Out, Ty, TypeNames); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 380 | } else { |
Chris Lattner | 72f866e | 2001-10-29 16:40:32 +0000 | [diff] [blame] | 381 | return Out << Ty->getDescription(); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 385 | /// @brief Internal constant writer. |
Chris Lattner | ab7d1ab | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 386 | static void WriteConstantInt(std::ostream &Out, const Constant *CV, |
| 387 | bool PrintName, |
| 388 | std::map<const Type *, std::string> &TypeTable, |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 389 | SlotMachine *Machine) { |
Chris Lattner | 1e19468 | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 390 | if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) { |
| 391 | Out << (CB == ConstantBool::True ? "true" : "false"); |
| 392 | } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) { |
| 393 | Out << CI->getValue(); |
| 394 | } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) { |
| 395 | Out << CI->getValue(); |
| 396 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
| 397 | // We would like to output the FP constant value in exponential notation, |
| 398 | // but we cannot do this if doing so will lose precision. Check here to |
| 399 | // make sure that we only output it in exponential format if we can parse |
| 400 | // the value back and get the same value. |
| 401 | // |
| 402 | std::string StrVal = ftostr(CFP->getValue()); |
| 403 | |
| 404 | // Check to make sure that the stringized number is not some string like |
| 405 | // "Inf" or NaN, that atof will accept, but the lexer will not. Check that |
| 406 | // the string matches the "[-+]?[0-9]" regex. |
| 407 | // |
| 408 | if ((StrVal[0] >= '0' && StrVal[0] <= '9') || |
| 409 | ((StrVal[0] == '-' || StrVal[0] == '+') && |
Brian Gaeke | 87b4f07 | 2003-06-17 23:55:35 +0000 | [diff] [blame] | 410 | (StrVal[1] >= '0' && StrVal[1] <= '9'))) |
Chris Lattner | 1e19468 | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 411 | // Reparse stringized version! |
| 412 | if (atof(StrVal.c_str()) == CFP->getValue()) { |
| 413 | Out << StrVal; return; |
| 414 | } |
| 415 | |
| 416 | // Otherwise we could not reparse it to exactly the same value, so we must |
| 417 | // output the string in hexadecimal format! |
| 418 | // |
| 419 | // Behave nicely in the face of C TBAA rules... see: |
| 420 | // http://www.nullstone.com/htmls/category/aliastyp.htm |
| 421 | // |
| 422 | double Val = CFP->getValue(); |
| 423 | char *Ptr = (char*)&Val; |
| 424 | assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 && |
| 425 | "assuming that double is 64 bits!"); |
| 426 | Out << "0x" << utohexstr(*(uint64_t*)Ptr); |
| 427 | |
Chris Lattner | 76b2ff4 | 2004-02-15 05:55:15 +0000 | [diff] [blame] | 428 | } else if (isa<ConstantAggregateZero>(CV)) { |
| 429 | Out << "zeroinitializer"; |
Chris Lattner | 1e19468 | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 430 | } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) { |
| 431 | // As a special case, print the array as a string if it is an array of |
| 432 | // ubytes or an array of sbytes with positive values. |
| 433 | // |
| 434 | const Type *ETy = CA->getType()->getElementType(); |
| 435 | bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy); |
| 436 | |
| 437 | if (ETy == Type::SByteTy) |
| 438 | for (unsigned i = 0; i < CA->getNumOperands(); ++i) |
| 439 | if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) { |
| 440 | isString = false; |
| 441 | break; |
| 442 | } |
| 443 | |
| 444 | if (isString) { |
| 445 | Out << "c\""; |
| 446 | for (unsigned i = 0; i < CA->getNumOperands(); ++i) { |
Chris Lattner | aa6ff27 | 2004-06-04 23:53:20 +0000 | [diff] [blame] | 447 | unsigned char C = |
| 448 | (unsigned char)cast<ConstantInt>(CA->getOperand(i))->getRawValue(); |
Chris Lattner | 1e19468 | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 449 | |
Chris Lattner | e5fd386 | 2002-07-31 23:56:44 +0000 | [diff] [blame] | 450 | if (isprint(C) && C != '"' && C != '\\') { |
Chris Lattner | 1e19468 | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 451 | Out << C; |
| 452 | } else { |
| 453 | Out << '\\' |
| 454 | << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A')) |
| 455 | << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A')); |
| 456 | } |
| 457 | } |
| 458 | Out << "\""; |
| 459 | |
| 460 | } else { // Cannot output in string format... |
Misha Brukman | 21bbdb9 | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 461 | Out << '['; |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 462 | if (CA->getNumOperands()) { |
Misha Brukman | 21bbdb9 | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 463 | Out << ' '; |
Chris Lattner | 1e19468 | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 464 | printTypeInt(Out, ETy, TypeTable); |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 465 | WriteAsOperandInternal(Out, CA->getOperand(0), |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 466 | PrintName, TypeTable, Machine); |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 467 | for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) { |
| 468 | Out << ", "; |
Chris Lattner | 1e19468 | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 469 | printTypeInt(Out, ETy, TypeTable); |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 470 | WriteAsOperandInternal(Out, CA->getOperand(i), PrintName, |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 471 | TypeTable, Machine); |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | Out << " ]"; |
| 475 | } |
| 476 | } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) { |
Misha Brukman | 21bbdb9 | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 477 | Out << '{'; |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 478 | if (CS->getNumOperands()) { |
Misha Brukman | 21bbdb9 | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 479 | Out << ' '; |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 480 | printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable); |
| 481 | |
| 482 | WriteAsOperandInternal(Out, CS->getOperand(0), |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 483 | PrintName, TypeTable, Machine); |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 484 | |
| 485 | for (unsigned i = 1; i < CS->getNumOperands(); i++) { |
| 486 | Out << ", "; |
| 487 | printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable); |
| 488 | |
| 489 | WriteAsOperandInternal(Out, CS->getOperand(i), |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 490 | PrintName, TypeTable, Machine); |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
| 494 | Out << " }"; |
| 495 | } else if (isa<ConstantPointerNull>(CV)) { |
| 496 | Out << "null"; |
| 497 | |
Chris Lattner | 3cd8c56 | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 498 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
Chris Lattner | b825722 | 2003-03-06 23:23:32 +0000 | [diff] [blame] | 499 | Out << CE->getOpcodeName() << " ("; |
Vikram S. Adve | b952b54 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 500 | |
| 501 | for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) { |
| 502 | printTypeInt(Out, (*OI)->getType(), TypeTable); |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 503 | WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Machine); |
Vikram S. Adve | b952b54 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 504 | if (OI+1 != CE->op_end()) |
Chris Lattner | 3cd8c56 | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 505 | Out << ", "; |
Vikram S. Adve | b952b54 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Chris Lattner | cfe8f53 | 2002-08-16 21:17:11 +0000 | [diff] [blame] | 508 | if (CE->getOpcode() == Instruction::Cast) { |
Chris Lattner | 83b396b | 2002-08-15 19:37:43 +0000 | [diff] [blame] | 509 | Out << " to "; |
| 510 | printTypeInt(Out, CE->getType(), TypeTable); |
| 511 | } |
Misha Brukman | 21bbdb9 | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 512 | Out << ')'; |
Chris Lattner | 83b396b | 2002-08-15 19:37:43 +0000 | [diff] [blame] | 513 | |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 514 | } else { |
Vikram S. Adve | b952b54 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 515 | Out << "<placeholder or erroneous Constant>"; |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
| 519 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 520 | /// WriteAsOperand - Write the name of the specified value out to the specified |
| 521 | /// ostream. This can be useful when you just want to print int %reg126, not |
| 522 | /// the whole instruction that generated it. |
| 523 | /// |
Chris Lattner | ab7d1ab | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 524 | static void WriteAsOperandInternal(std::ostream &Out, const Value *V, |
| 525 | bool PrintName, |
| 526 | std::map<const Type*, std::string> &TypeTable, |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 527 | SlotMachine *Machine) { |
Misha Brukman | 21bbdb9 | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 528 | Out << ' '; |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 529 | if ((PrintName || isa<GlobalValue>(V)) && V->hasName()) |
Chris Lattner | 1c34304 | 2003-08-22 05:40:38 +0000 | [diff] [blame] | 530 | Out << getLLVMName(V->getName()); |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 531 | else { |
| 532 | const Constant *CV = dyn_cast<Constant>(V); |
| 533 | if (CV && !isa<GlobalValue>(CV)) |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 534 | WriteConstantInt(Out, CV, PrintName, TypeTable, Machine); |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 535 | else { |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 536 | int Slot; |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 537 | if (Machine) { |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 538 | Slot = Machine->getSlot(V); |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 539 | } else { |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 540 | Machine = createSlotMachine(V); |
Chris Lattner | 757ee0b | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 541 | if (Machine == 0) |
| 542 | Slot = Machine->getSlot(V); |
| 543 | else |
| 544 | Slot = -1; |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 545 | delete Machine; |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 546 | } |
Chris Lattner | 757ee0b | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 547 | if (Slot != -1) |
| 548 | Out << '%' << Slot; |
| 549 | else |
| 550 | Out << "<badref>"; |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
Misha Brukman | b22d09c | 2004-03-01 19:48:13 +0000 | [diff] [blame] | 555 | /// WriteAsOperand - Write the name of the specified value out to the specified |
| 556 | /// ostream. This can be useful when you just want to print int %reg126, not |
| 557 | /// the whole instruction that generated it. |
| 558 | /// |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 559 | std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V, |
Misha Brukman | b22d09c | 2004-03-01 19:48:13 +0000 | [diff] [blame] | 560 | bool PrintType, bool PrintName, |
| 561 | const Module *Context) { |
Chris Lattner | ab7d1ab | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 562 | std::map<const Type *, std::string> TypeNames; |
Chris Lattner | 5a9f63e | 2002-07-10 16:48:17 +0000 | [diff] [blame] | 563 | if (Context == 0) Context = getModuleFromVal(V); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 564 | |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 565 | if (Context) |
Chris Lattner | 5a9f63e | 2002-07-10 16:48:17 +0000 | [diff] [blame] | 566 | fillTypeNameTable(Context, TypeNames); |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 567 | |
| 568 | if (PrintType) |
| 569 | printTypeInt(Out, V->getType(), TypeNames); |
| 570 | |
Chris Lattner | 5a9f63e | 2002-07-10 16:48:17 +0000 | [diff] [blame] | 571 | WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0); |
Chris Lattner | 5e5abe3 | 2001-07-20 19:15:21 +0000 | [diff] [blame] | 572 | return Out; |
| 573 | } |
| 574 | |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 575 | /// WriteAsOperandInternal - Write the name of the specified value out to |
| 576 | /// the specified ostream. This can be useful when you just want to print |
| 577 | /// int %reg126, not the whole instruction that generated it. |
| 578 | /// |
| 579 | static void WriteAsOperandInternal(std::ostream &Out, const Type *T, |
| 580 | bool PrintName, |
| 581 | std::map<const Type*, std::string> &TypeTable, |
| 582 | SlotMachine *Machine) { |
| 583 | Out << ' '; |
| 584 | int Slot; |
| 585 | if (Machine) { |
| 586 | Slot = Machine->getSlot(T); |
| 587 | if (Slot != -1) |
| 588 | Out << '%' << Slot; |
| 589 | else |
| 590 | Out << "<badref>"; |
| 591 | } else { |
| 592 | Out << T->getDescription(); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | /// WriteAsOperand - Write the name of the specified value out to the specified |
| 597 | /// ostream. This can be useful when you just want to print int %reg126, not |
| 598 | /// the whole instruction that generated it. |
| 599 | /// |
| 600 | std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Type *Ty, |
| 601 | bool PrintType, bool PrintName, |
| 602 | const Module *Context) { |
| 603 | std::map<const Type *, std::string> TypeNames; |
| 604 | assert(Context != 0 && "Can't write types as operand without module context"); |
| 605 | |
| 606 | fillTypeNameTable(Context, TypeNames); |
| 607 | |
| 608 | // if (PrintType) |
| 609 | // printTypeInt(Out, V->getType(), TypeNames); |
| 610 | |
| 611 | printTypeInt(Out, Ty, TypeNames); |
| 612 | |
| 613 | WriteAsOperandInternal(Out, Ty, PrintName, TypeNames, 0); |
| 614 | return Out; |
| 615 | } |
| 616 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 617 | namespace llvm { |
Chris Lattner | 2e9fee4 | 2001-07-12 23:35:26 +0000 | [diff] [blame] | 618 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 619 | class AssemblyWriter { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 620 | std::ostream &Out; |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 621 | SlotMachine &Machine; |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 622 | const Module *TheModule; |
Chris Lattner | ab7d1ab | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 623 | std::map<const Type *, std::string> TypeNames; |
Chris Lattner | 8339f7d | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 624 | AssemblyAnnotationWriter *AnnotationWriter; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 625 | public: |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 626 | inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M, |
Chris Lattner | 8339f7d | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 627 | AssemblyAnnotationWriter *AAW) |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 628 | : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) { |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 629 | |
| 630 | // If the module has a symbol table, take all global types and stuff their |
| 631 | // names into the TypeNames map. |
| 632 | // |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 633 | fillTypeNameTable(M, TypeNames); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 636 | inline void write(const Module *M) { printModule(M); } |
| 637 | inline void write(const GlobalVariable *G) { printGlobal(G); } |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 638 | inline void write(const Function *F) { printFunction(F); } |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 639 | inline void write(const BasicBlock *BB) { printBasicBlock(BB); } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 640 | inline void write(const Instruction *I) { printInstruction(*I); } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 641 | inline void write(const Constant *CPV) { printConstant(CPV); } |
Chris Lattner | 7db7958 | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 642 | inline void write(const Type *Ty) { printType(Ty); } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 643 | |
Chris Lattner | 1e19468 | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 644 | void writeOperand(const Value *Op, bool PrintType, bool PrintName = true); |
| 645 | |
Misha Brukman | 4685e26 | 2004-04-28 15:31:21 +0000 | [diff] [blame] | 646 | const Module* getModule() { return TheModule; } |
| 647 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 648 | private : |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 649 | void printModule(const Module *M); |
| 650 | void printSymbolTable(const SymbolTable &ST); |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 651 | void printConstant(const Constant *CPV); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 652 | void printGlobal(const GlobalVariable *GV); |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 653 | void printFunction(const Function *F); |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 654 | void printArgument(const Argument *FA); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 655 | void printBasicBlock(const BasicBlock *BB); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 656 | void printInstruction(const Instruction &I); |
Chris Lattner | d816b53 | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 657 | |
| 658 | // printType - Go to extreme measures to attempt to print out a short, |
| 659 | // symbolic version of a type name. |
| 660 | // |
Chris Lattner | ab7d1ab | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 661 | std::ostream &printType(const Type *Ty) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 662 | return printTypeInt(Out, Ty, TypeNames); |
Chris Lattner | d816b53 | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | // printTypeAtLeastOneLevel - Print out one level of the possibly complex type |
| 666 | // without considering any symbolic types that we may have equal to it. |
| 667 | // |
Chris Lattner | ab7d1ab | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 668 | std::ostream &printTypeAtLeastOneLevel(const Type *Ty); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 669 | |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 670 | // printInfoComment - Print a little comment after the instruction indicating |
| 671 | // which slot it occupies. |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 672 | void printInfoComment(const Value &V); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 673 | }; |
Reid Spencer | f43ac62 | 2004-05-27 22:04:46 +0000 | [diff] [blame] | 674 | } // end of llvm namespace |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 675 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 676 | /// printTypeAtLeastOneLevel - Print out one level of the possibly complex type |
| 677 | /// without considering any symbolic types that we may have equal to it. |
| 678 | /// |
Chris Lattner | ab7d1ab | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 679 | std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 680 | if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) { |
Chris Lattner | d816b53 | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 681 | printType(FTy->getReturnType()) << " ("; |
Chris Lattner | fa829be | 2004-02-09 04:14:01 +0000 | [diff] [blame] | 682 | for (FunctionType::param_iterator I = FTy->param_begin(), |
| 683 | E = FTy->param_end(); I != E; ++I) { |
| 684 | if (I != FTy->param_begin()) |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 685 | Out << ", "; |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 686 | printType(*I); |
Chris Lattner | d816b53 | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 687 | } |
| 688 | if (FTy->isVarArg()) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 689 | if (FTy->getNumParams()) Out << ", "; |
| 690 | Out << "..."; |
Chris Lattner | d816b53 | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 691 | } |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 692 | Out << ')'; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 693 | } else if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 694 | Out << "{ "; |
Chris Lattner | ac6db75 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 695 | for (StructType::element_iterator I = STy->element_begin(), |
| 696 | E = STy->element_end(); I != E; ++I) { |
| 697 | if (I != STy->element_begin()) |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 698 | Out << ", "; |
Chris Lattner | d816b53 | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 699 | printType(*I); |
| 700 | } |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 701 | Out << " }"; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 702 | } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) { |
Misha Brukman | 21bbdb9 | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 703 | printType(PTy->getElementType()) << '*'; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 704 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 705 | Out << '[' << ATy->getNumElements() << " x "; |
Misha Brukman | 21bbdb9 | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 706 | printType(ATy->getElementType()) << ']'; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 707 | } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 708 | Out << "opaque"; |
Chris Lattner | d816b53 | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 709 | } else { |
Vikram S. Adve | b952b54 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 710 | if (!Ty->isPrimitiveType()) |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 711 | Out << "<unknown derived type>"; |
Chris Lattner | d816b53 | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 712 | printType(Ty); |
| 713 | } |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 714 | return Out; |
Chris Lattner | d816b53 | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 718 | void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType, |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 719 | bool PrintName) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 720 | if (PrintType) { Out << ' '; printType(Operand->getType()); } |
| 721 | WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Machine); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 724 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 725 | void AssemblyWriter::printModule(const Module *M) { |
Chris Lattner | 8068e0c | 2003-08-24 13:48:48 +0000 | [diff] [blame] | 726 | switch (M->getEndianness()) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 727 | case Module::LittleEndian: Out << "target endian = little\n"; break; |
| 728 | case Module::BigEndian: Out << "target endian = big\n"; break; |
Chris Lattner | 8068e0c | 2003-08-24 13:48:48 +0000 | [diff] [blame] | 729 | case Module::AnyEndianness: break; |
| 730 | } |
| 731 | switch (M->getPointerSize()) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 732 | case Module::Pointer32: Out << "target pointersize = 32\n"; break; |
| 733 | case Module::Pointer64: Out << "target pointersize = 64\n"; break; |
Chris Lattner | 8068e0c | 2003-08-24 13:48:48 +0000 | [diff] [blame] | 734 | case Module::AnyPointerSize: break; |
| 735 | } |
Reid Spencer | 48f98c8 | 2004-07-25 21:44:54 +0000 | [diff] [blame] | 736 | if (!M->getTargetTriple().empty()) |
Reid Spencer | ffec7df | 2004-07-25 21:29:43 +0000 | [diff] [blame] | 737 | Out << "target triple = \"" << M->getTargetTriple() << "\"\n"; |
Reid Spencer | cc5ff64 | 2004-07-25 18:08:18 +0000 | [diff] [blame] | 738 | |
| 739 | // Loop over the dependent libraries and emit them |
Reid Spencer | 48f98c8 | 2004-07-25 21:44:54 +0000 | [diff] [blame] | 740 | Module::lib_iterator LI= M->lib_begin(); |
| 741 | Module::lib_iterator LE= M->lib_end(); |
| 742 | if (LI != LE) { |
Reid Spencer | ffec7df | 2004-07-25 21:29:43 +0000 | [diff] [blame] | 743 | Out << "deplibs = [\n"; |
Reid Spencer | 48f98c8 | 2004-07-25 21:44:54 +0000 | [diff] [blame] | 744 | while ( LI != LE ) { |
Reid Spencer | ffec7df | 2004-07-25 21:29:43 +0000 | [diff] [blame] | 745 | Out << "\"" << *LI << "\""; |
| 746 | ++LI; |
| 747 | if ( LI != LE ) |
| 748 | Out << ",\n"; |
| 749 | } |
| 750 | Out << " ]\n"; |
Reid Spencer | cc5ff64 | 2004-07-25 18:08:18 +0000 | [diff] [blame] | 751 | } |
Chris Lattner | 8068e0c | 2003-08-24 13:48:48 +0000 | [diff] [blame] | 752 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 753 | // Loop over the symbol table, emitting all named constants... |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 754 | printSymbolTable(M->getSymbolTable()); |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 755 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 756 | for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I) |
| 757 | printGlobal(I); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 758 | |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 759 | Out << "\nimplementation ; Functions:\n"; |
Vikram S. Adve | 13ba19a | 2001-09-18 12:48:16 +0000 | [diff] [blame] | 760 | |
Chris Lattner | 6915f8f | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 761 | // Output all of the functions... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 762 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 763 | printFunction(I); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 766 | void AssemblyWriter::printGlobal(const GlobalVariable *GV) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 767 | if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = "; |
Chris Lattner | 3779864 | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 768 | |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 769 | if (!GV->hasInitializer()) |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 770 | Out << "external "; |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 771 | else |
| 772 | switch (GV->getLinkage()) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 773 | case GlobalValue::InternalLinkage: Out << "internal "; break; |
| 774 | case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break; |
| 775 | case GlobalValue::WeakLinkage: Out << "weak "; break; |
| 776 | case GlobalValue::AppendingLinkage: Out << "appending "; break; |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 777 | case GlobalValue::ExternalLinkage: break; |
| 778 | } |
Chris Lattner | 3779864 | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 779 | |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 780 | Out << (GV->isConstant() ? "constant " : "global "); |
Chris Lattner | 2413b16 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 781 | printType(GV->getType()->getElementType()); |
Chris Lattner | 3779864 | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 782 | |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 783 | if (GV->hasInitializer()) { |
| 784 | Constant* C = cast<Constant>(GV->getInitializer()); |
| 785 | assert(C && "GlobalVar initializer isn't constant?"); |
Reid Spencer | c9c90cf | 2004-07-18 01:04:19 +0000 | [diff] [blame] | 786 | writeOperand(GV->getInitializer(), false, isa<GlobalValue>(C)); |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 787 | } |
Chris Lattner | 3779864 | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 788 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 789 | printInfoComment(*GV); |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 790 | Out << "\n"; |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 793 | |
Reid Spencer | e7e9671 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 794 | // printSymbolTable - Run through symbol table looking for constants |
| 795 | // and types. Emit their declarations. |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 796 | void AssemblyWriter::printSymbolTable(const SymbolTable &ST) { |
Reid Spencer | e7e9671 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 797 | |
| 798 | // Print the types. |
| 799 | for (SymbolTable::type_const_iterator TI = ST.type_begin(); |
| 800 | TI != ST.type_end(); ++TI ) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 801 | Out << "\t" << getLLVMName(TI->first) << " = type "; |
Reid Spencer | e7e9671 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 802 | |
| 803 | // Make sure we print out at least one level of the type structure, so |
| 804 | // that we do not get %FILE = type %FILE |
| 805 | // |
| 806 | printTypeAtLeastOneLevel(TI->second) << "\n"; |
| 807 | } |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 808 | |
Reid Spencer | e7e9671 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 809 | // Print the constants, in type plane order. |
| 810 | for (SymbolTable::plane_const_iterator PI = ST.plane_begin(); |
| 811 | PI != ST.plane_end(); ++PI ) { |
| 812 | SymbolTable::value_const_iterator VI = ST.value_begin(PI->first); |
| 813 | SymbolTable::value_const_iterator VE = ST.value_end(PI->first); |
| 814 | |
| 815 | for (; VI != VE; ++VI) { |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 816 | const Value* V = VI->second; |
| 817 | const Constant *CPV = dyn_cast<Constant>(V) ; |
| 818 | if (CPV && !isa<GlobalValue>(V)) { |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 819 | printConstant(CPV); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 820 | } |
| 821 | } |
Chris Lattner | a7620d9 | 2001-07-15 06:35:59 +0000 | [diff] [blame] | 822 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 826 | /// printConstant - Print out a constant pool entry... |
| 827 | /// |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 828 | void AssemblyWriter::printConstant(const Constant *CPV) { |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 829 | // Don't print out unnamed constants, they will be inlined |
| 830 | if (!CPV->hasName()) return; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 831 | |
Chris Lattner | ee998be | 2001-07-26 16:29:38 +0000 | [diff] [blame] | 832 | // Print out name... |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 833 | Out << "\t" << getLLVMName(CPV->getName()) << " ="; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 834 | |
| 835 | // Write the value out now... |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 836 | writeOperand(CPV, true, false); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 837 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 838 | printInfoComment(*CPV); |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 839 | Out << "\n"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 842 | /// printFunction - Print all aspects of a function. |
| 843 | /// |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 844 | void AssemblyWriter::printFunction(const Function *F) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 845 | // Print out the return type and name... |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 846 | Out << "\n"; |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 847 | |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 848 | if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out); |
Chris Lattner | 8339f7d | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 849 | |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 850 | if (F->isExternal()) |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 851 | Out << "declare "; |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 852 | else |
| 853 | switch (F->getLinkage()) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 854 | case GlobalValue::InternalLinkage: Out << "internal "; break; |
| 855 | case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break; |
| 856 | case GlobalValue::WeakLinkage: Out << "weak "; break; |
| 857 | case GlobalValue::AppendingLinkage: Out << "appending "; break; |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 858 | case GlobalValue::ExternalLinkage: break; |
| 859 | } |
| 860 | |
Misha Brukman | 21bbdb9 | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 861 | printType(F->getReturnType()) << ' '; |
Chris Lattner | 5b33748 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 862 | if (!F->getName().empty()) |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 863 | Out << getLLVMName(F->getName()); |
Chris Lattner | 5b33748 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 864 | else |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 865 | Out << "\"\""; |
| 866 | Out << '('; |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 867 | Machine.incorporateFunction(F); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 868 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 869 | // Loop over the arguments, printing them... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 870 | const FunctionType *FT = F->getFunctionType(); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 871 | |
Chris Lattner | 149376d | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 872 | for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I) |
| 873 | printArgument(I); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 874 | |
| 875 | // Finish printing arguments... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 876 | if (FT->isVarArg()) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 877 | if (FT->getNumParams()) Out << ", "; |
| 878 | Out << "..."; // Output varargs portion of signature! |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 879 | } |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 880 | Out << ')'; |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 881 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 882 | if (F->isExternal()) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 883 | Out << "\n"; |
Chris Lattner | b2f02e5 | 2002-05-06 03:00:40 +0000 | [diff] [blame] | 884 | } else { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 885 | Out << " {"; |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 886 | |
Chris Lattner | 6915f8f | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 887 | // Output all of its basic blocks... for the function |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 888 | for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I) |
| 889 | printBasicBlock(I); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 890 | |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 891 | Out << "}\n"; |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 894 | Machine.purgeFunction(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 897 | /// printArgument - This member is called for every argument that is passed into |
| 898 | /// the function. Simply print it out |
| 899 | /// |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 900 | void AssemblyWriter::printArgument(const Argument *Arg) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 901 | // Insert commas as we go... the first arg doesn't get a comma |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 902 | if (Arg != &Arg->getParent()->afront()) Out << ", "; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 903 | |
| 904 | // Output type... |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 905 | printType(Arg->getType()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 906 | |
| 907 | // Output name, if available... |
| 908 | if (Arg->hasName()) |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 909 | Out << ' ' << getLLVMName(Arg->getName()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 912 | /// printBasicBlock - This member is called for each basic block in a method. |
| 913 | /// |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 914 | void AssemblyWriter::printBasicBlock(const BasicBlock *BB) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 915 | if (BB->hasName()) { // Print out the label if it exists... |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 916 | Out << "\n" << BB->getName() << ':'; |
Chris Lattner | 408dbdb | 2002-05-14 16:02:05 +0000 | [diff] [blame] | 917 | } else if (!BB->use_empty()) { // Don't print block # of no uses... |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 918 | Out << "\n; <label>:"; |
Chris Lattner | 757ee0b | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 919 | int Slot = Machine.getSlot(BB); |
| 920 | if (Slot != -1) |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 921 | Out << Slot; |
Chris Lattner | 757ee0b | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 922 | else |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 923 | Out << "<badref>"; |
Chris Lattner | 58185f2 | 2002-10-02 19:38:55 +0000 | [diff] [blame] | 924 | } |
Chris Lattner | 2447ef5 | 2003-11-20 00:09:43 +0000 | [diff] [blame] | 925 | |
| 926 | if (BB->getParent() == 0) |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 927 | Out << "\t\t; Error: Block without parent!"; |
Chris Lattner | 2447ef5 | 2003-11-20 00:09:43 +0000 | [diff] [blame] | 928 | else { |
| 929 | if (BB != &BB->getParent()->front()) { // Not the entry block? |
| 930 | // Output predecessors for the block... |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 931 | Out << "\t\t;"; |
Chris Lattner | 2447ef5 | 2003-11-20 00:09:43 +0000 | [diff] [blame] | 932 | pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB); |
| 933 | |
| 934 | if (PI == PE) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 935 | Out << " No predecessors!"; |
Chris Lattner | 2447ef5 | 2003-11-20 00:09:43 +0000 | [diff] [blame] | 936 | } else { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 937 | Out << " preds ="; |
Chris Lattner | 00211f1 | 2003-11-16 22:59:57 +0000 | [diff] [blame] | 938 | writeOperand(*PI, false, true); |
Chris Lattner | 2447ef5 | 2003-11-20 00:09:43 +0000 | [diff] [blame] | 939 | for (++PI; PI != PE; ++PI) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 940 | Out << ','; |
Chris Lattner | 2447ef5 | 2003-11-20 00:09:43 +0000 | [diff] [blame] | 941 | writeOperand(*PI, false, true); |
| 942 | } |
Chris Lattner | 00211f1 | 2003-11-16 22:59:57 +0000 | [diff] [blame] | 943 | } |
Chris Lattner | 58185f2 | 2002-10-02 19:38:55 +0000 | [diff] [blame] | 944 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 945 | } |
Chris Lattner | 408dbdb | 2002-05-14 16:02:05 +0000 | [diff] [blame] | 946 | |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 947 | Out << "\n"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 948 | |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 949 | if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out); |
Chris Lattner | 8339f7d | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 950 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 951 | // Output all of the instructions in the basic block... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 952 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 953 | printInstruction(*I); |
Chris Lattner | 96cdd27 | 2004-03-08 18:51:45 +0000 | [diff] [blame] | 954 | |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 955 | if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 956 | } |
| 957 | |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 958 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 959 | /// printInfoComment - Print a little comment after the instruction indicating |
| 960 | /// which slot it occupies. |
| 961 | /// |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 962 | void AssemblyWriter::printInfoComment(const Value &V) { |
| 963 | if (V.getType() != Type::VoidTy) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 964 | Out << "\t\t; <"; |
Misha Brukman | 21bbdb9 | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 965 | printType(V.getType()) << '>'; |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 966 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 967 | if (!V.hasName()) { |
Chris Lattner | 757ee0b | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 968 | int SlotNum = Machine.getSlot(&V); |
| 969 | if (SlotNum == -1) |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 970 | Out << ":<badref>"; |
Reid Spencer | 8beac69 | 2004-06-09 15:26:53 +0000 | [diff] [blame] | 971 | else |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 972 | Out << ':' << SlotNum; // Print out the def slot taken. |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 973 | } |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 974 | Out << " [#uses=" << V.use_size() << ']'; // Output # uses |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 975 | } |
| 976 | } |
| 977 | |
Reid Spencer | 8beac69 | 2004-06-09 15:26:53 +0000 | [diff] [blame] | 978 | /// printInstruction - This member is called for each Instruction in a function.. |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 979 | /// |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 980 | void AssemblyWriter::printInstruction(const Instruction &I) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 981 | if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out); |
Chris Lattner | 8339f7d | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 982 | |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 983 | Out << "\t"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 984 | |
| 985 | // Print out name if it exists... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 986 | if (I.hasName()) |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 987 | Out << getLLVMName(I.getName()) << " = "; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 988 | |
Chris Lattner | 504f924 | 2003-09-08 17:45:59 +0000 | [diff] [blame] | 989 | // If this is a volatile load or store, print out the volatile marker |
| 990 | if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) || |
| 991 | (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 992 | Out << "volatile "; |
Chris Lattner | 504f924 | 2003-09-08 17:45:59 +0000 | [diff] [blame] | 993 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 994 | // Print out the opcode... |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 995 | Out << I.getOpcodeName(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 996 | |
| 997 | // Print out the type of the operands... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 998 | const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 999 | |
| 1000 | // Special case conditional branches to swizzle the condition out to the front |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1001 | if (isa<BranchInst>(I) && I.getNumOperands() > 1) { |
| 1002 | writeOperand(I.getOperand(2), true); |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1003 | Out << ','; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1004 | writeOperand(Operand, true); |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1005 | Out << ','; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1006 | writeOperand(I.getOperand(1), true); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1007 | |
Chris Lattner | 8d48df2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 1008 | } else if (isa<SwitchInst>(I)) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1009 | // Special case switch statement to get formatting nice and correct... |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1010 | writeOperand(Operand , true); Out << ','; |
| 1011 | writeOperand(I.getOperand(1), true); Out << " ["; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1012 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1013 | for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1014 | Out << "\n\t\t"; |
| 1015 | writeOperand(I.getOperand(op ), true); Out << ','; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1016 | writeOperand(I.getOperand(op+1), true); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1017 | } |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1018 | Out << "\n\t]"; |
Chris Lattner | da55810 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 1019 | } else if (isa<PHINode>(I)) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1020 | Out << ' '; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1021 | printType(I.getType()); |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1022 | Out << ' '; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1023 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1024 | for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1025 | if (op) Out << ", "; |
| 1026 | Out << '['; |
| 1027 | writeOperand(I.getOperand(op ), false); Out << ','; |
| 1028 | writeOperand(I.getOperand(op+1), false); Out << " ]"; |
Chris Lattner | 931ef3b | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 1029 | } |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1030 | } else if (isa<ReturnInst>(I) && !Operand) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1031 | Out << " void"; |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1032 | } else if (isa<CallInst>(I)) { |
Chris Lattner | 463d6a5 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1033 | const PointerType *PTy = cast<PointerType>(Operand->getType()); |
| 1034 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 1035 | const Type *RetTy = FTy->getReturnType(); |
Chris Lattner | 2f2d947 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 1036 | |
Chris Lattner | 463d6a5 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1037 | // If possible, print out the short form of the call instruction. We can |
Chris Lattner | 6915f8f | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 1038 | // only do this if the first argument is a pointer to a nonvararg function, |
Chris Lattner | 463d6a5 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1039 | // and if the return type is not a pointer to a function. |
Chris Lattner | 2f2d947 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 1040 | // |
Chris Lattner | 463d6a5 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1041 | if (!FTy->isVarArg() && |
Chris Lattner | 8d48df2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 1042 | (!isa<PointerType>(RetTy) || |
Chris Lattner | d9a36a6 | 2002-07-25 20:58:51 +0000 | [diff] [blame] | 1043 | !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1044 | Out << ' '; printType(RetTy); |
Chris Lattner | 2f2d947 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 1045 | writeOperand(Operand, false); |
| 1046 | } else { |
| 1047 | writeOperand(Operand, true); |
| 1048 | } |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1049 | Out << '('; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1050 | if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true); |
| 1051 | for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1052 | Out << ','; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1053 | writeOperand(I.getOperand(op), true); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1056 | Out << " )"; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1057 | } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) { |
Chris Lattner | 463d6a5 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1058 | const PointerType *PTy = cast<PointerType>(Operand->getType()); |
| 1059 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 1060 | const Type *RetTy = FTy->getReturnType(); |
| 1061 | |
| 1062 | // If possible, print out the short form of the invoke instruction. We can |
| 1063 | // only do this if the first argument is a pointer to a nonvararg function, |
| 1064 | // and if the return type is not a pointer to a function. |
| 1065 | // |
| 1066 | if (!FTy->isVarArg() && |
| 1067 | (!isa<PointerType>(RetTy) || |
| 1068 | !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1069 | Out << ' '; printType(RetTy); |
Chris Lattner | 463d6a5 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1070 | writeOperand(Operand, false); |
| 1071 | } else { |
| 1072 | writeOperand(Operand, true); |
| 1073 | } |
| 1074 | |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1075 | Out << '('; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1076 | if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true); |
| 1077 | for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1078 | Out << ','; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1079 | writeOperand(I.getOperand(op), true); |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1082 | Out << " )\n\t\t\tto"; |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1083 | writeOperand(II->getNormalDest(), true); |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1084 | Out << " unwind"; |
Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 1085 | writeOperand(II->getUnwindDest(), true); |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1086 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1087 | } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1088 | Out << ' '; |
Chris Lattner | 8d48df2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 1089 | printType(AI->getType()->getElementType()); |
| 1090 | if (AI->isArrayAllocation()) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1091 | Out << ','; |
Chris Lattner | 8d48df2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 1092 | writeOperand(AI->getArraySize(), true); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1093 | } |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1094 | } else if (isa<CastInst>(I)) { |
Chris Lattner | c96e96b | 2003-11-17 01:17:04 +0000 | [diff] [blame] | 1095 | if (Operand) writeOperand(Operand, true); // Work with broken code |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1096 | Out << " to "; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1097 | printType(I.getType()); |
Chris Lattner | 5b33748 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 1098 | } else if (isa<VAArgInst>(I)) { |
Chris Lattner | c96e96b | 2003-11-17 01:17:04 +0000 | [diff] [blame] | 1099 | if (Operand) writeOperand(Operand, true); // Work with broken code |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1100 | Out << ", "; |
Chris Lattner | f70da10 | 2003-05-08 02:44:12 +0000 | [diff] [blame] | 1101 | printType(I.getType()); |
Chris Lattner | 5b33748 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 1102 | } else if (const VANextInst *VAN = dyn_cast<VANextInst>(&I)) { |
Chris Lattner | c96e96b | 2003-11-17 01:17:04 +0000 | [diff] [blame] | 1103 | if (Operand) writeOperand(Operand, true); // Work with broken code |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1104 | Out << ", "; |
Chris Lattner | 5b33748 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 1105 | printType(VAN->getArgType()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1106 | } else if (Operand) { // Print the normal way... |
| 1107 | |
| 1108 | // PrintAllTypes - Instructions who have operands of all the same type |
| 1109 | // omit the type from all but the first operand. If the instruction has |
| 1110 | // different type operands (for example br), then they are all printed. |
| 1111 | bool PrintAllTypes = false; |
| 1112 | const Type *TheType = Operand->getType(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1113 | |
Chris Lattner | 52bd5cb9 | 2004-03-12 05:53:14 +0000 | [diff] [blame] | 1114 | // Shift Left & Right print both types even for Ubyte LHS, and select prints |
| 1115 | // types even if all operands are bools. |
| 1116 | if (isa<ShiftInst>(I) || isa<SelectInst>(I)) { |
Chris Lattner | deccfaf | 2003-04-16 20:20:02 +0000 | [diff] [blame] | 1117 | PrintAllTypes = true; |
| 1118 | } else { |
| 1119 | for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) { |
| 1120 | Operand = I.getOperand(i); |
| 1121 | if (Operand->getType() != TheType) { |
| 1122 | PrintAllTypes = true; // We have differing types! Print them all! |
| 1123 | break; |
| 1124 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1125 | } |
| 1126 | } |
Chris Lattner | deccfaf | 2003-04-16 20:20:02 +0000 | [diff] [blame] | 1127 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1128 | if (!PrintAllTypes) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1129 | Out << ' '; |
Chris Lattner | deccfaf | 2003-04-16 20:20:02 +0000 | [diff] [blame] | 1130 | printType(TheType); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1131 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1132 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1133 | for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1134 | if (i) Out << ','; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1135 | writeOperand(I.getOperand(i), PrintAllTypes); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1136 | } |
| 1137 | } |
| 1138 | |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1139 | printInfoComment(I); |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1140 | Out << "\n"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | |
| 1144 | //===----------------------------------------------------------------------===// |
| 1145 | // External Interface declarations |
| 1146 | //===----------------------------------------------------------------------===// |
| 1147 | |
Chris Lattner | 8339f7d | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1148 | void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const { |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1149 | SlotMachine SlotTable(this); |
Chris Lattner | 8339f7d | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1150 | AssemblyWriter W(o, SlotTable, this, AAW); |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1151 | W.write(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1154 | void GlobalVariable::print(std::ostream &o) const { |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1155 | SlotMachine SlotTable(getParent()); |
Chris Lattner | 8339f7d | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1156 | AssemblyWriter W(o, SlotTable, getParent(), 0); |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1157 | W.write(this); |
Chris Lattner | adfe0d1 | 2001-09-10 20:08:19 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Chris Lattner | 8339f7d | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1160 | void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const { |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1161 | SlotMachine SlotTable(getParent()); |
Chris Lattner | 8339f7d | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1162 | AssemblyWriter W(o, SlotTable, getParent(), AAW); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1163 | |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1164 | W.write(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Chris Lattner | 8339f7d | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1167 | void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const { |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1168 | SlotMachine SlotTable(getParent()); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1169 | AssemblyWriter W(o, SlotTable, |
Chris Lattner | 8339f7d | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1170 | getParent() ? getParent()->getParent() : 0, AAW); |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1171 | W.write(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Chris Lattner | 8339f7d | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1174 | void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const { |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1175 | const Function *F = getParent() ? getParent()->getParent() : 0; |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1176 | SlotMachine SlotTable(F); |
Chris Lattner | 8339f7d | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1177 | AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1178 | |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1179 | W.write(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1180 | } |
Chris Lattner | 7db7958 | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 1181 | |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1182 | void Constant::print(std::ostream &o) const { |
| 1183 | if (this == 0) { o << "<null> constant value\n"; return; } |
Chris Lattner | 7d73480 | 2002-09-10 15:53:49 +0000 | [diff] [blame] | 1184 | |
Misha Brukman | 21bbdb9 | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 1185 | o << ' ' << getType()->getDescription() << ' '; |
Chris Lattner | 1e19468 | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 1186 | |
Chris Lattner | ab7d1ab | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 1187 | std::map<const Type *, std::string> TypeTable; |
Chris Lattner | 1e19468 | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 1188 | WriteConstantInt(o, this, false, TypeTable, 0); |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | void Type::print(std::ostream &o) const { |
| 1192 | if (this == 0) |
| 1193 | o << "<null Type>"; |
| 1194 | else |
| 1195 | o << getDescription(); |
| 1196 | } |
| 1197 | |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 1198 | void Argument::print(std::ostream &o) const { |
Chris Lattner | e52ed45 | 2004-07-13 23:14:34 +0000 | [diff] [blame] | 1199 | WriteAsOperand(o, this, true, true, |
| 1200 | getParent() ? getParent()->getParent() : 0); |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
Reid Spencer | 5264183 | 2004-05-25 18:14:38 +0000 | [diff] [blame] | 1203 | // Value::dump - allow easy printing of Values from the debugger. |
| 1204 | // Located here because so much of the needed functionality is here. |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1205 | void Value::dump() const { print(std::cerr); } |
Reid Spencer | 5264183 | 2004-05-25 18:14:38 +0000 | [diff] [blame] | 1206 | |
| 1207 | // Type::dump - allow easy printing of Values from the debugger. |
| 1208 | // Located here because so much of the needed functionality is here. |
Reid Spencer | e7e9671 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 1209 | void Type::dump() const { print(std::cerr); } |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1210 | |
| 1211 | //===----------------------------------------------------------------------===// |
| 1212 | // CachedWriter Class Implementation |
| 1213 | //===----------------------------------------------------------------------===// |
| 1214 | |
Chris Lattner | 7db7958 | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 1215 | void CachedWriter::setModule(const Module *M) { |
| 1216 | delete SC; delete AW; |
| 1217 | if (M) { |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1218 | SC = new SlotMachine(M ); |
Misha Brukman | 21bbdb9 | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 1219 | AW = new AssemblyWriter(Out, *SC, M, 0); |
Chris Lattner | 7db7958 | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 1220 | } else { |
| 1221 | SC = 0; AW = 0; |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | CachedWriter::~CachedWriter() { |
| 1226 | delete AW; |
| 1227 | delete SC; |
| 1228 | } |
| 1229 | |
Chris Lattner | 60a7dd1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 1230 | CachedWriter &CachedWriter::operator<<(const Value &V) { |
Chris Lattner | 7db7958 | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 1231 | assert(AW && SC && "CachedWriter does not have a current module!"); |
Chris Lattner | 60a7dd1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 1232 | if (const Instruction *I = dyn_cast<Instruction>(&V)) |
Chris Lattner | 80d2d53 | 2004-06-26 19:40:40 +0000 | [diff] [blame] | 1233 | AW->write(I); |
Chris Lattner | 60a7dd1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 1234 | else if (const BasicBlock *BB = dyn_cast<BasicBlock>(&V)) |
Chris Lattner | 80d2d53 | 2004-06-26 19:40:40 +0000 | [diff] [blame] | 1235 | AW->write(BB); |
Chris Lattner | 60a7dd1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 1236 | else if (const Function *F = dyn_cast<Function>(&V)) |
Chris Lattner | 80d2d53 | 2004-06-26 19:40:40 +0000 | [diff] [blame] | 1237 | AW->write(F); |
Chris Lattner | 60a7dd1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 1238 | else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(&V)) |
Chris Lattner | 80d2d53 | 2004-06-26 19:40:40 +0000 | [diff] [blame] | 1239 | AW->write(GV); |
Chris Lattner | 80d2d53 | 2004-06-26 19:40:40 +0000 | [diff] [blame] | 1240 | else |
Chris Lattner | 60a7dd1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 1241 | AW->writeOperand(&V, true, true); |
Chris Lattner | 7db7958 | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 1242 | return *this; |
| 1243 | } |
Misha Brukman | 4685e26 | 2004-04-28 15:31:21 +0000 | [diff] [blame] | 1244 | |
Chris Lattner | 60a7dd1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 1245 | CachedWriter& CachedWriter::operator<<(const Type &Ty) { |
Misha Brukman | 4685e26 | 2004-04-28 15:31:21 +0000 | [diff] [blame] | 1246 | if (SymbolicTypes) { |
| 1247 | const Module *M = AW->getModule(); |
Chris Lattner | 60a7dd1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 1248 | if (M) WriteTypeSymbolic(Out, &Ty, M); |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1249 | } else { |
Chris Lattner | 60a7dd1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 1250 | AW->write(&Ty); |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1251 | } |
| 1252 | return *this; |
Misha Brukman | 4685e26 | 2004-04-28 15:31:21 +0000 | [diff] [blame] | 1253 | } |
Misha Brukman | da546ea | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 1254 | |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1255 | //===----------------------------------------------------------------------===// |
| 1256 | //===-- SlotMachine Implementation |
| 1257 | //===----------------------------------------------------------------------===// |
| 1258 | |
| 1259 | #if 0 |
| 1260 | #define SC_DEBUG(X) std::cerr << X |
| 1261 | #else |
| 1262 | #define SC_DEBUG(X) |
| 1263 | #endif |
| 1264 | |
| 1265 | // Module level constructor. Causes the contents of the Module (sans functions) |
| 1266 | // to be added to the slot table. |
| 1267 | SlotMachine::SlotMachine(const Module *M) |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1268 | : TheModule(M) ///< Saved for lazy initialization. |
| 1269 | , TheFunction(0) |
Reid Spencer | b0ac8c4 | 2004-08-16 07:46:33 +0000 | [diff] [blame^] | 1270 | , FunctionProcessed(false) |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1271 | , mMap() |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1272 | , mTypes() |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1273 | , fMap() |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1274 | , fTypes() |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1275 | { |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | // Function level constructor. Causes the contents of the Module and the one |
| 1279 | // function provided to be added to the slot table. |
| 1280 | SlotMachine::SlotMachine(const Function *F ) |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1281 | : TheModule( F ? F->getParent() : 0 ) ///< Saved for lazy initialization |
| 1282 | , TheFunction(F) ///< Saved for lazy initialization |
Reid Spencer | b0ac8c4 | 2004-08-16 07:46:33 +0000 | [diff] [blame^] | 1283 | , FunctionProcessed(false) |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1284 | , mMap() |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1285 | , mTypes() |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1286 | , fMap() |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1287 | , fTypes() |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1288 | { |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | inline void SlotMachine::initialize(void) { |
| 1292 | if ( TheModule) { |
| 1293 | processModule(); |
| 1294 | TheModule = 0; ///< Prevent re-processing next time we're called. |
| 1295 | } |
Reid Spencer | b0ac8c4 | 2004-08-16 07:46:33 +0000 | [diff] [blame^] | 1296 | if ( TheFunction && ! FunctionProcessed) { |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1297 | processFunction(); |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | // Iterate through all the global variables, functions, and global |
| 1302 | // variable initializers and create slots for them. |
| 1303 | void SlotMachine::processModule() { |
| 1304 | SC_DEBUG("begin processModule!\n"); |
| 1305 | |
| 1306 | // Add all of the global variables to the value table... |
| 1307 | for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend(); |
| 1308 | I != E; ++I) |
| 1309 | createSlot(I); |
| 1310 | |
| 1311 | // Add all the functions to the table |
| 1312 | for (Module::const_iterator I = TheModule->begin(), E = TheModule->end(); |
| 1313 | I != E; ++I) |
| 1314 | createSlot(I); |
| 1315 | |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1316 | SC_DEBUG("end processModule!\n"); |
| 1317 | } |
| 1318 | |
| 1319 | |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1320 | // Process the arguments, basic blocks, and instructions of a function. |
| 1321 | void SlotMachine::processFunction() { |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1322 | SC_DEBUG("begin processFunction!\n"); |
| 1323 | |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1324 | // Add all the function arguments |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1325 | for(Function::const_aiterator AI = TheFunction->abegin(), |
| 1326 | AE = TheFunction->aend(); AI != AE; ++AI) |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1327 | createSlot(AI); |
| 1328 | |
| 1329 | SC_DEBUG("Inserting Instructions:\n"); |
| 1330 | |
| 1331 | // Add all of the basic blocks and instructions |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1332 | for (Function::const_iterator BB = TheFunction->begin(), |
| 1333 | E = TheFunction->end(); BB != E; ++BB) { |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1334 | createSlot(BB); |
| 1335 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) { |
| 1336 | createSlot(I); |
| 1337 | } |
| 1338 | } |
| 1339 | |
Reid Spencer | b0ac8c4 | 2004-08-16 07:46:33 +0000 | [diff] [blame^] | 1340 | FunctionProcessed = true; |
| 1341 | |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1342 | SC_DEBUG("end processFunction!\n"); |
| 1343 | } |
| 1344 | |
| 1345 | // Clean up after incorporating a function. This is the only way |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1346 | // to get out of the function incorporation state that affects the |
| 1347 | // getSlot/createSlot lock. Function incorporation state is indicated |
| 1348 | // by TheFunction != 0. |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1349 | void SlotMachine::purgeFunction() { |
| 1350 | SC_DEBUG("begin purgeFunction!\n"); |
| 1351 | fMap.clear(); // Simply discard the function level map |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1352 | fTypes.clear(); |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1353 | TheFunction = 0; |
Reid Spencer | b0ac8c4 | 2004-08-16 07:46:33 +0000 | [diff] [blame^] | 1354 | FunctionProcessed = false; |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1355 | SC_DEBUG("end purgeFunction!\n"); |
| 1356 | } |
| 1357 | |
| 1358 | /// Get the slot number for a value. This function will assert if you |
| 1359 | /// ask for a Value that hasn't previously been inserted with createSlot. |
| 1360 | /// Types are forbidden because Type does not inherit from Value (any more). |
Chris Lattner | 757ee0b | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 1361 | int SlotMachine::getSlot(const Value *V) { |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1362 | assert( V && "Can't get slot for null Value" ); |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1363 | assert(!isa<Constant>(V) || isa<GlobalValue>(V) && |
| 1364 | "Can't insert a non-GlobalValue Constant into SlotMachine"); |
| 1365 | |
| 1366 | // Check for uninitialized state and do lazy initialization |
| 1367 | this->initialize(); |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1368 | |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1369 | // Get the type of the value |
| 1370 | const Type* VTy = V->getType(); |
| 1371 | |
| 1372 | // Find the type plane in the module map |
| 1373 | TypedPlanes::const_iterator MI = mMap.find(VTy); |
| 1374 | |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1375 | if ( TheFunction ) { |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1376 | // Lookup the type in the function map too |
| 1377 | TypedPlanes::const_iterator FI = fMap.find(VTy); |
| 1378 | // If there is a corresponding type plane in the function map |
| 1379 | if ( FI != fMap.end() ) { |
| 1380 | // Lookup the Value in the function map |
| 1381 | ValueMap::const_iterator FVI = FI->second.map.find(V); |
| 1382 | // If the value doesn't exist in the function map |
| 1383 | if ( FVI == FI->second.map.end() ) { |
Chris Lattner | 68a038e | 2004-06-09 22:22:10 +0000 | [diff] [blame] | 1384 | // Look up the value in the module map. |
| 1385 | if (MI == mMap.end()) return -1; |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1386 | ValueMap::const_iterator MVI = MI->second.map.find(V); |
| 1387 | // If we didn't find it, it wasn't inserted |
Chris Lattner | 757ee0b | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 1388 | if (MVI == MI->second.map.end()) return -1; |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1389 | assert( MVI != MI->second.map.end() && "Value not found"); |
| 1390 | // We found it only at the module level |
| 1391 | return MVI->second; |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1392 | |
| 1393 | // else the value exists in the function map |
| 1394 | } else { |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1395 | // Return the slot number as the module's contribution to |
| 1396 | // the type plane plus the index in the function's contribution |
| 1397 | // to the type plane. |
Chris Lattner | b1f0478 | 2004-06-15 21:07:32 +0000 | [diff] [blame] | 1398 | if (MI != mMap.end()) |
| 1399 | return MI->second.next_slot + FVI->second; |
| 1400 | else |
| 1401 | return FVI->second; |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1402 | } |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1403 | } |
| 1404 | } |
| 1405 | |
Reid Spencer | 8beac69 | 2004-06-09 15:26:53 +0000 | [diff] [blame] | 1406 | // N.B. Can get here only if either !TheFunction or the function doesn't |
| 1407 | // have a corresponding type plane for the Value |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1408 | |
| 1409 | // Make sure the type plane exists |
Chris Lattner | 757ee0b | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 1410 | if (MI == mMap.end()) return -1; |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1411 | // Lookup the value in the module's map |
| 1412 | ValueMap::const_iterator MVI = MI->second.map.find(V); |
| 1413 | // Make sure we found it. |
Chris Lattner | 757ee0b | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 1414 | if (MVI == MI->second.map.end()) return -1; |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1415 | // Return it. |
| 1416 | return MVI->second; |
| 1417 | } |
| 1418 | |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1419 | /// Get the slot number for a value. This function will assert if you |
| 1420 | /// ask for a Value that hasn't previously been inserted with createSlot. |
| 1421 | /// Types are forbidden because Type does not inherit from Value (any more). |
| 1422 | int SlotMachine::getSlot(const Type *Ty) { |
| 1423 | assert( Ty && "Can't get slot for null Type" ); |
| 1424 | |
| 1425 | // Check for uninitialized state and do lazy initialization |
| 1426 | this->initialize(); |
| 1427 | |
| 1428 | if ( TheFunction ) { |
| 1429 | // Lookup the Type in the function map |
| 1430 | TypeMap::const_iterator FTI = fTypes.map.find(Ty); |
| 1431 | // If the Type doesn't exist in the function map |
| 1432 | if ( FTI == fTypes.map.end() ) { |
| 1433 | TypeMap::const_iterator MTI = mTypes.map.find(Ty); |
| 1434 | // If we didn't find it, it wasn't inserted |
| 1435 | if (MTI == mTypes.map.end()) |
| 1436 | return -1; |
| 1437 | // We found it only at the module level |
| 1438 | return MTI->second; |
| 1439 | |
| 1440 | // else the value exists in the function map |
| 1441 | } else { |
| 1442 | // Return the slot number as the module's contribution to |
| 1443 | // the type plane plus the index in the function's contribution |
| 1444 | // to the type plane. |
| 1445 | return mTypes.next_slot + FTI->second; |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | // N.B. Can get here only if either !TheFunction |
| 1450 | |
| 1451 | // Lookup the value in the module's map |
| 1452 | TypeMap::const_iterator MTI = mTypes.map.find(Ty); |
| 1453 | // Make sure we found it. |
| 1454 | if (MTI == mTypes.map.end()) return -1; |
| 1455 | // Return it. |
| 1456 | return MTI->second; |
| 1457 | } |
| 1458 | |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1459 | // Create a new slot, or return the existing slot if it is already |
| 1460 | // inserted. Note that the logic here parallels getSlot but instead |
| 1461 | // of asserting when the Value* isn't found, it inserts the value. |
| 1462 | unsigned SlotMachine::createSlot(const Value *V) { |
| 1463 | assert( V && "Can't insert a null Value to SlotMachine"); |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1464 | assert(!isa<Constant>(V) || isa<GlobalValue>(V) && |
| 1465 | "Can't insert a non-GlobalValue Constant into SlotMachine"); |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1466 | |
| 1467 | const Type* VTy = V->getType(); |
| 1468 | |
| 1469 | // Just ignore void typed things |
| 1470 | if (VTy == Type::VoidTy) return 0; // FIXME: Wrong return value! |
| 1471 | |
| 1472 | // Look up the type plane for the Value's type from the module map |
| 1473 | TypedPlanes::const_iterator MI = mMap.find(VTy); |
| 1474 | |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1475 | if ( TheFunction ) { |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1476 | // Get the type plane for the Value's type from the function map |
| 1477 | TypedPlanes::const_iterator FI = fMap.find(VTy); |
| 1478 | // If there is a corresponding type plane in the function map |
| 1479 | if ( FI != fMap.end() ) { |
| 1480 | // Lookup the Value in the function map |
| 1481 | ValueMap::const_iterator FVI = FI->second.map.find(V); |
| 1482 | // If the value doesn't exist in the function map |
| 1483 | if ( FVI == FI->second.map.end() ) { |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1484 | // If there is no corresponding type plane in the module map |
| 1485 | if ( MI == mMap.end() ) |
| 1486 | return insertValue(V); |
| 1487 | // Look up the value in the module map |
| 1488 | ValueMap::const_iterator MVI = MI->second.map.find(V); |
| 1489 | // If we didn't find it, it wasn't inserted |
| 1490 | if ( MVI == MI->second.map.end() ) |
| 1491 | return insertValue(V); |
| 1492 | else |
| 1493 | // We found it only at the module level |
| 1494 | return MVI->second; |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1495 | |
| 1496 | // else the value exists in the function map |
| 1497 | } else { |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1498 | if ( MI == mMap.end() ) |
| 1499 | return FVI->second; |
| 1500 | else |
| 1501 | // Return the slot number as the module's contribution to |
| 1502 | // the type plane plus the index in the function's contribution |
| 1503 | // to the type plane. |
| 1504 | return MI->second.next_slot + FVI->second; |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | // else there is not a corresponding type plane in the function map |
| 1508 | } else { |
| 1509 | // If the type plane doesn't exists at the module level |
| 1510 | if ( MI == mMap.end() ) { |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1511 | return insertValue(V); |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1512 | // else type plane exists at the module level, examine it |
| 1513 | } else { |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1514 | // Look up the value in the module's map |
| 1515 | ValueMap::const_iterator MVI = MI->second.map.find(V); |
| 1516 | // If we didn't find it there either |
| 1517 | if ( MVI == MI->second.map.end() ) |
| 1518 | // Return the slot number as the module's contribution to |
| 1519 | // the type plane plus the index of the function map insertion. |
| 1520 | return MI->second.next_slot + insertValue(V); |
| 1521 | else |
| 1522 | return MVI->second; |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1523 | } |
| 1524 | } |
| 1525 | } |
| 1526 | |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1527 | // N.B. Can only get here if !TheFunction |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1528 | |
| 1529 | // If the module map's type plane is not for the Value's type |
| 1530 | if ( MI != mMap.end() ) { |
| 1531 | // Lookup the value in the module's map |
| 1532 | ValueMap::const_iterator MVI = MI->second.map.find(V); |
| 1533 | if ( MVI != MI->second.map.end() ) |
| 1534 | return MVI->second; |
| 1535 | } |
| 1536 | |
| 1537 | return insertValue(V); |
| 1538 | } |
| 1539 | |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1540 | // Create a new slot, or return the existing slot if it is already |
| 1541 | // inserted. Note that the logic here parallels getSlot but instead |
| 1542 | // of asserting when the Value* isn't found, it inserts the value. |
| 1543 | unsigned SlotMachine::createSlot(const Type *Ty) { |
| 1544 | assert( Ty && "Can't insert a null Type to SlotMachine"); |
| 1545 | |
| 1546 | if ( TheFunction ) { |
| 1547 | // Lookup the Type in the function map |
| 1548 | TypeMap::const_iterator FTI = fTypes.map.find(Ty); |
| 1549 | // If the type doesn't exist in the function map |
| 1550 | if ( FTI == fTypes.map.end() ) { |
| 1551 | // Look up the type in the module map |
| 1552 | TypeMap::const_iterator MTI = mTypes.map.find(Ty); |
| 1553 | // If we didn't find it, it wasn't inserted |
| 1554 | if ( MTI == mTypes.map.end() ) |
| 1555 | return insertValue(Ty); |
| 1556 | else |
| 1557 | // We found it only at the module level |
| 1558 | return MTI->second; |
| 1559 | |
| 1560 | // else the value exists in the function map |
| 1561 | } else { |
| 1562 | // Return the slot number as the module's contribution to |
| 1563 | // the type plane plus the index in the function's contribution |
| 1564 | // to the type plane. |
| 1565 | return mTypes.next_slot + FTI->second; |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | // N.B. Can only get here if !TheFunction |
| 1570 | |
| 1571 | // Lookup the type in the module's map |
| 1572 | TypeMap::const_iterator MTI = mTypes.map.find(Ty); |
| 1573 | if ( MTI != mTypes.map.end() ) |
| 1574 | return MTI->second; |
| 1575 | |
| 1576 | return insertValue(Ty); |
| 1577 | } |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1578 | |
| 1579 | // Low level insert function. Minimal checking is done. This |
| 1580 | // function is just for the convenience of createSlot (above). |
| 1581 | unsigned SlotMachine::insertValue(const Value *V ) { |
| 1582 | assert(V && "Can't insert a null Value into SlotMachine!"); |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1583 | assert(!isa<Constant>(V) || isa<GlobalValue>(V) && |
| 1584 | "Can't insert a non-GlobalValue Constant into SlotMachine"); |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1585 | |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1586 | // If this value does not contribute to a plane (is void) |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1587 | // or if the value already has a name then ignore it. |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1588 | if (V->getType() == Type::VoidTy || V->hasName() ) { |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1589 | SC_DEBUG("ignored value " << *V << "\n"); |
| 1590 | return 0; // FIXME: Wrong return value |
| 1591 | } |
| 1592 | |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1593 | const Type *VTy = V->getType(); |
| 1594 | unsigned DestSlot = 0; |
| 1595 | |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1596 | if ( TheFunction ) { |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1597 | TypedPlanes::iterator I = fMap.find( VTy ); |
| 1598 | if ( I == fMap.end() ) |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1599 | I = fMap.insert(std::make_pair(VTy,ValuePlane())).first; |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1600 | DestSlot = I->second.map[V] = I->second.next_slot++; |
| 1601 | } else { |
| 1602 | TypedPlanes::iterator I = mMap.find( VTy ); |
| 1603 | if ( I == mMap.end() ) |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1604 | I = mMap.insert(std::make_pair(VTy,ValuePlane())).first; |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1605 | DestSlot = I->second.map[V] = I->second.next_slot++; |
| 1606 | } |
| 1607 | |
| 1608 | SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" << |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1609 | DestSlot << " ["); |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1610 | // G = Global, C = Constant, T = Type, F = Function, o = other |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1611 | SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : (isa<Function>(V) ? 'F' : |
| 1612 | (isa<Constant>(V) ? 'C' : 'o')))); |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1613 | SC_DEBUG("]\n"); |
| 1614 | return DestSlot; |
| 1615 | } |
| 1616 | |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1617 | // Low level insert function. Minimal checking is done. This |
| 1618 | // function is just for the convenience of createSlot (above). |
| 1619 | unsigned SlotMachine::insertValue(const Type *Ty ) { |
| 1620 | assert(Ty && "Can't insert a null Type into SlotMachine!"); |
| 1621 | |
| 1622 | unsigned DestSlot = 0; |
| 1623 | |
| 1624 | if ( TheFunction ) { |
| 1625 | DestSlot = fTypes.map[Ty] = fTypes.next_slot++; |
| 1626 | } else { |
| 1627 | DestSlot = fTypes.map[Ty] = fTypes.next_slot++; |
| 1628 | } |
| 1629 | SC_DEBUG(" Inserting type [" << DestSlot << "] = " << Ty << "\n"); |
| 1630 | return DestSlot; |
| 1631 | } |
| 1632 | |
Reid Spencer | e7e9671 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 1633 | // vim: sw=2 |