Chris Lattner | 8da78af | 2002-04-07 22:31:46 +0000 | [diff] [blame] | 1 | //===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 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 | 02b9399 | 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 | 8f77dae | 2003-05-08 02:44:12 +0000 | [diff] [blame] | 13 | // LLVM code, because it can be used for debugging transformations. |
Chris Lattner | 02b9399 | 2002-04-12 18:21:53 +0000 | [diff] [blame] | 14 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | da1fbcc | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 17 | #include "llvm/Assembly/CachedWriter.h" |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 18 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | f082b80 | 2002-07-23 18:07:49 +0000 | [diff] [blame] | 19 | #include "llvm/Assembly/PrintModulePass.h" |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 20 | #include "llvm/Assembly/AsmAnnotationWriter.h" |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 21 | #include "llvm/CallingConv.h" |
Chris Lattner | f2d577b | 2004-01-20 19:50:34 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
Chris Lattner | 3eb59c0 | 2002-04-29 18:46:50 +0000 | [diff] [blame] | 23 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 863517a | 2006-01-25 18:57:27 +0000 | [diff] [blame] | 24 | #include "llvm/InlineAsm.h" |
Vikram S. Adve | b4dbb44 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 25 | #include "llvm/Instruction.h" |
Misha Brukman | 4433629 | 2004-07-29 16:53:53 +0000 | [diff] [blame] | 26 | #include "llvm/Instructions.h" |
Chris Lattner | f2d577b | 2004-01-20 19:50:34 +0000 | [diff] [blame] | 27 | #include "llvm/Module.h" |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 28 | #include "llvm/SymbolTable.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/StringExtras.h" |
| 30 | #include "llvm/ADT/STLExtras.h" |
Bill Wendling | 8f48766 | 2006-11-28 02:09:03 +0000 | [diff] [blame] | 31 | #include "llvm/Support/CFG.h" |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 32 | #include "llvm/Support/MathExtras.h" |
Bill Wendling | 8f48766 | 2006-11-28 02:09:03 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Streams.h" |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 34 | #include <algorithm> |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 35 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 37 | namespace llvm { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 38 | |
Reid Spencer | edd5d9e | 2005-05-15 16:13:11 +0000 | [diff] [blame] | 39 | // Make virtual table appear in this compilation unit. |
| 40 | AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {} |
| 41 | |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 42 | /// This class provides computation of slot numbers for LLVM Assembly writing. |
| 43 | /// @brief LLVM Assembly Writing Slot Computation. |
| 44 | class SlotMachine { |
| 45 | |
| 46 | /// @name Types |
| 47 | /// @{ |
| 48 | public: |
| 49 | |
| 50 | /// @brief A mapping of Values to slot numbers |
| 51 | typedef std::map<const Value*, unsigned> ValueMap; |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 52 | typedef std::map<const Type*, unsigned> TypeMap; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 53 | |
| 54 | /// @brief A plane with next slot number and ValueMap |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 55 | struct ValuePlane { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 56 | unsigned next_slot; ///< The next slot number to use |
| 57 | ValueMap map; ///< The map of Value* -> unsigned |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 58 | ValuePlane() { next_slot = 0; } ///< Make sure we start at 0 |
| 59 | }; |
| 60 | |
| 61 | struct TypePlane { |
| 62 | unsigned next_slot; |
| 63 | TypeMap map; |
| 64 | TypePlane() { next_slot = 0; } |
| 65 | void clear() { map.clear(); next_slot = 0; } |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | /// @brief The map of planes by Type |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 69 | typedef std::map<const Type*, ValuePlane> TypedPlanes; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 70 | |
| 71 | /// @} |
| 72 | /// @name Constructors |
| 73 | /// @{ |
| 74 | public: |
| 75 | /// @brief Construct from a module |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 76 | SlotMachine(const Module *M); |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 77 | |
| 78 | /// @brief Construct from a function, starting out in incorp state. |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 79 | SlotMachine(const Function *F); |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 80 | |
| 81 | /// @} |
| 82 | /// @name Accessors |
| 83 | /// @{ |
| 84 | public: |
| 85 | /// Return the slot number of the specified value in it's type |
| 86 | /// plane. Its an error to ask for something not in the SlotMachine. |
| 87 | /// Its an error to ask for a Type* |
Chris Lattner | 6956645 | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 88 | int getSlot(const Value *V); |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 89 | int getSlot(const Type*Ty); |
Reid Spencer | fc621e2 | 2004-06-09 15:26:53 +0000 | [diff] [blame] | 90 | |
| 91 | /// Determine if a Value has a slot or not |
| 92 | bool hasSlot(const Value* V); |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 93 | bool hasSlot(const Type* Ty); |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 94 | |
| 95 | /// @} |
| 96 | /// @name Mutators |
| 97 | /// @{ |
| 98 | public: |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 99 | /// If you'd like to deal with a function instead of just a module, use |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 100 | /// this method to get its data into the SlotMachine. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 101 | void incorporateFunction(const Function *F) { |
| 102 | TheFunction = F; |
Reid Spencer | 28531c7 | 2004-08-16 07:46:33 +0000 | [diff] [blame] | 103 | FunctionProcessed = false; |
| 104 | } |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 105 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 106 | /// After calling incorporateFunction, use this method to remove the |
| 107 | /// most recently incorporated function from the SlotMachine. This |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 108 | /// will reset the state of the machine back to just the module contents. |
| 109 | void purgeFunction(); |
| 110 | |
| 111 | /// @} |
| 112 | /// @name Implementation Details |
| 113 | /// @{ |
| 114 | private: |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 115 | /// This function does the actual initialization. |
| 116 | inline void initialize(); |
| 117 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 118 | /// Values can be crammed into here at will. If they haven't |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 119 | /// been inserted already, they get inserted, otherwise they are ignored. |
| 120 | /// Either way, the slot number for the Value* is returned. |
Chris Lattner | de891a6 | 2006-12-06 05:27:40 +0000 | [diff] [blame^] | 121 | unsigned getOrCreateSlot(const Value *V); |
| 122 | unsigned getOrCreateSlot(const Type *Ty); |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 123 | |
| 124 | /// Insert a value into the value table. Return the slot number |
| 125 | /// that it now occupies. BadThings(TM) will happen if you insert a |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 126 | /// Value that's already been inserted. |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 127 | unsigned insertValue(const Value *V); |
| 128 | unsigned insertValue(const Type *Ty); |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 129 | |
| 130 | /// Add all of the module level global variables (and their initializers) |
| 131 | /// and function declarations, but not the contents of those functions. |
| 132 | void processModule(); |
| 133 | |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 134 | /// Add all of the functions arguments, basic blocks, and instructions |
| 135 | void processFunction(); |
| 136 | |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 137 | SlotMachine(const SlotMachine &); // DO NOT IMPLEMENT |
| 138 | void operator=(const SlotMachine &); // DO NOT IMPLEMENT |
| 139 | |
| 140 | /// @} |
| 141 | /// @name Data |
| 142 | /// @{ |
| 143 | public: |
| 144 | |
| 145 | /// @brief The module for which we are holding slot numbers |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 146 | const Module* TheModule; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 147 | |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 148 | /// @brief The function for which we are holding slot numbers |
| 149 | const Function* TheFunction; |
Reid Spencer | 28531c7 | 2004-08-16 07:46:33 +0000 | [diff] [blame] | 150 | bool FunctionProcessed; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 151 | |
| 152 | /// @brief The TypePlanes map for the module level data |
| 153 | TypedPlanes mMap; |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 154 | TypePlane mTypes; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 155 | |
| 156 | /// @brief The TypePlanes map for the function level data |
| 157 | TypedPlanes fMap; |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 158 | TypePlane fTypes; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 159 | |
| 160 | /// @} |
| 161 | |
| 162 | }; |
| 163 | |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 164 | } // end namespace llvm |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 165 | |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 166 | static RegisterPass<PrintModulePass> |
Chris Lattner | 3dd965c | 2006-08-21 17:20:01 +0000 | [diff] [blame] | 167 | X("printm", "Print module to stderr"); |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 168 | static RegisterPass<PrintFunctionPass> |
Chris Lattner | 3dd965c | 2006-08-21 17:20:01 +0000 | [diff] [blame] | 169 | Y("print","Print function to stderr"); |
Chris Lattner | f082b80 | 2002-07-23 18:07:49 +0000 | [diff] [blame] | 170 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 171 | static void WriteAsOperandInternal(std::ostream &Out, const Value *V, |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 172 | bool PrintName, |
| 173 | std::map<const Type *, std::string> &TypeTable, |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 174 | SlotMachine *Machine); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 175 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 176 | static void WriteAsOperandInternal(std::ostream &Out, const Type *T, |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 177 | bool PrintName, |
| 178 | std::map<const Type *, std::string> &TypeTable, |
| 179 | SlotMachine *Machine); |
| 180 | |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 181 | static const Module *getModuleFromVal(const Value *V) { |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 182 | if (const Argument *MA = dyn_cast<Argument>(V)) |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 183 | return MA->getParent() ? MA->getParent()->getParent() : 0; |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 184 | else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 185 | return BB->getParent() ? BB->getParent()->getParent() : 0; |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 186 | else if (const Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 187 | const Function *M = I->getParent() ? I->getParent()->getParent() : 0; |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 188 | return M ? M->getParent() : 0; |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 189 | } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 190 | return GV->getParent(); |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 191 | return 0; |
| 192 | } |
| 193 | |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 194 | static SlotMachine *createSlotMachine(const Value *V) { |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 195 | if (const Argument *FA = dyn_cast<Argument>(V)) { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 196 | return new SlotMachine(FA->getParent()); |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 197 | } else if (const Instruction *I = dyn_cast<Instruction>(V)) { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 198 | return new SlotMachine(I->getParent()->getParent()); |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 199 | } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 200 | return new SlotMachine(BB->getParent()); |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 201 | } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){ |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 202 | return new SlotMachine(GV->getParent()); |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 203 | } else if (const Function *Func = dyn_cast<Function>(V)) { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 204 | return new SlotMachine(Func); |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 205 | } |
| 206 | return 0; |
| 207 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 208 | |
Chris Lattner | 24b8a5d | 2003-08-22 05:40:38 +0000 | [diff] [blame] | 209 | // getLLVMName - Turn the specified string into an 'LLVM name', which is either |
| 210 | // prefixed with % (if the string only contains simple characters) or is |
| 211 | // surrounded with ""'s (if it has special chars in it). |
Alkis Evlogimenos | 9913e59 | 2004-12-10 05:41:10 +0000 | [diff] [blame] | 212 | static std::string getLLVMName(const std::string &Name, |
| 213 | bool prefixName = true) { |
Chris Lattner | 24b8a5d | 2003-08-22 05:40:38 +0000 | [diff] [blame] | 214 | assert(!Name.empty() && "Cannot get empty name!"); |
| 215 | |
| 216 | // First character cannot start with a number... |
| 217 | if (Name[0] >= '0' && Name[0] <= '9') |
| 218 | return "\"" + Name + "\""; |
| 219 | |
| 220 | // Scan to see if we have any characters that are not on the "white list" |
| 221 | for (unsigned i = 0, e = Name.size(); i != e; ++i) { |
| 222 | char C = Name[i]; |
| 223 | assert(C != '"' && "Illegal character in LLVM value name!"); |
| 224 | if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') && |
| 225 | C != '-' && C != '.' && C != '_') |
| 226 | return "\"" + Name + "\""; |
| 227 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 24b8a5d | 2003-08-22 05:40:38 +0000 | [diff] [blame] | 229 | // If we get here, then the identifier is legal to use as a "VarID". |
Alkis Evlogimenos | 9913e59 | 2004-12-10 05:41:10 +0000 | [diff] [blame] | 230 | if (prefixName) |
| 231 | return "%"+Name; |
| 232 | else |
| 233 | return Name; |
Chris Lattner | 24b8a5d | 2003-08-22 05:40:38 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 236 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 237 | /// fillTypeNameTable - If the module has a symbol table, take all global types |
| 238 | /// and stuff their names into the TypeNames map. |
| 239 | /// |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 240 | static void fillTypeNameTable(const Module *M, |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 241 | std::map<const Type *, std::string> &TypeNames) { |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 242 | if (!M) return; |
| 243 | const SymbolTable &ST = M->getSymbolTable(); |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 244 | SymbolTable::type_const_iterator TI = ST.type_begin(); |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 245 | for (; TI != ST.type_end(); ++TI) { |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 246 | // As a heuristic, don't insert pointer to primitive types, because |
| 247 | // they are used too often to have a single useful name. |
| 248 | // |
| 249 | const Type *Ty = cast<Type>(TI->second); |
| 250 | if (!isa<PointerType>(Ty) || |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 251 | !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() || |
| 252 | isa<OpaqueType>(cast<PointerType>(Ty)->getElementType())) |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 253 | TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first))); |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | |
| 258 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 259 | static void calcTypeName(const Type *Ty, |
John Criswell | 4ff620a | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 260 | std::vector<const Type *> &TypeStack, |
| 261 | std::map<const Type *, std::string> &TypeNames, |
| 262 | std::string & Result){ |
| 263 | if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)) { |
| 264 | Result += Ty->getDescription(); // Base case |
| 265 | return; |
| 266 | } |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 267 | |
| 268 | // Check to see if the type is named. |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 269 | std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty); |
John Criswell | 4ff620a | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 270 | if (I != TypeNames.end()) { |
| 271 | Result += I->second; |
| 272 | return; |
| 273 | } |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 274 | |
John Criswell | 4ff620a | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 275 | if (isa<OpaqueType>(Ty)) { |
| 276 | Result += "opaque"; |
| 277 | return; |
| 278 | } |
Chris Lattner | 88c1738 | 2003-10-30 00:22:33 +0000 | [diff] [blame] | 279 | |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 280 | // Check to see if the Type is already on the stack... |
| 281 | unsigned Slot = 0, CurSize = TypeStack.size(); |
| 282 | while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type |
| 283 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 284 | // This is another base case for the recursion. In this case, we know |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 285 | // that we have looped back to a type that we have previously visited. |
| 286 | // Generate the appropriate upreference to handle this. |
John Criswell | 4ff620a | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 287 | if (Slot < CurSize) { |
| 288 | Result += "\\" + utostr(CurSize-Slot); // Here's the upreference |
| 289 | return; |
| 290 | } |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 291 | |
| 292 | TypeStack.push_back(Ty); // Recursive case: Add us to the stack.. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 293 | |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 294 | switch (Ty->getTypeID()) { |
Chris Lattner | 6bfd6a5 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 295 | case Type::FunctionTyID: { |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 296 | const FunctionType *FTy = cast<FunctionType>(Ty); |
John Criswell | 4ff620a | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 297 | calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result); |
| 298 | Result += " ("; |
Chris Lattner | d5d8996 | 2004-02-09 04:14:01 +0000 | [diff] [blame] | 299 | for (FunctionType::param_iterator I = FTy->param_begin(), |
| 300 | E = FTy->param_end(); I != E; ++I) { |
| 301 | if (I != FTy->param_begin()) |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 302 | Result += ", "; |
John Criswell | 4ff620a | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 303 | calcTypeName(*I, TypeStack, TypeNames, Result); |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 304 | } |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 305 | if (FTy->isVarArg()) { |
Chris Lattner | d5d8996 | 2004-02-09 04:14:01 +0000 | [diff] [blame] | 306 | if (FTy->getNumParams()) Result += ", "; |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 307 | Result += "..."; |
| 308 | } |
| 309 | Result += ")"; |
| 310 | break; |
| 311 | } |
| 312 | case Type::StructTyID: { |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 313 | const StructType *STy = cast<StructType>(Ty); |
John Criswell | 4ff620a | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 314 | Result += "{ "; |
Chris Lattner | d21cd80 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 315 | for (StructType::element_iterator I = STy->element_begin(), |
| 316 | E = STy->element_end(); I != E; ++I) { |
| 317 | if (I != STy->element_begin()) |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 318 | Result += ", "; |
John Criswell | 4ff620a | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 319 | calcTypeName(*I, TypeStack, TypeNames, Result); |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 320 | } |
| 321 | Result += " }"; |
| 322 | break; |
| 323 | } |
| 324 | case Type::PointerTyID: |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 325 | calcTypeName(cast<PointerType>(Ty)->getElementType(), |
John Criswell | 4ff620a | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 326 | TypeStack, TypeNames, Result); |
| 327 | Result += "*"; |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 328 | break; |
| 329 | case Type::ArrayTyID: { |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 330 | const ArrayType *ATy = cast<ArrayType>(Ty); |
John Criswell | 4ff620a | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 331 | Result += "[" + utostr(ATy->getNumElements()) + " x "; |
| 332 | calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result); |
| 333 | Result += "]"; |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 334 | break; |
| 335 | } |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 336 | case Type::PackedTyID: { |
| 337 | const PackedType *PTy = cast<PackedType>(Ty); |
| 338 | Result += "<" + utostr(PTy->getNumElements()) + " x "; |
| 339 | calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result); |
| 340 | Result += ">"; |
| 341 | break; |
| 342 | } |
Chris Lattner | 9e094c4 | 2003-05-14 17:50:47 +0000 | [diff] [blame] | 343 | case Type::OpaqueTyID: |
John Criswell | 4ff620a | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 344 | Result += "opaque"; |
Chris Lattner | 9e094c4 | 2003-05-14 17:50:47 +0000 | [diff] [blame] | 345 | break; |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 346 | default: |
John Criswell | 4ff620a | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 347 | Result += "<unrecognized-type>"; |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | TypeStack.pop_back(); // Remove self from stack... |
John Criswell | 4ff620a | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 351 | return; |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | |
Misha Brukman | 9d0802e | 2004-03-01 19:48:13 +0000 | [diff] [blame] | 355 | /// printTypeInt - The internal guts of printing out a type that has a |
| 356 | /// potentially named portion. |
| 357 | /// |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 358 | static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty, |
| 359 | std::map<const Type *, std::string> &TypeNames) { |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 360 | // Primitive types always print out their description, regardless of whether |
| 361 | // they have been named or not. |
| 362 | // |
Chris Lattner | daf2a49 | 2003-10-30 00:12:51 +0000 | [diff] [blame] | 363 | if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)) |
| 364 | return Out << Ty->getDescription(); |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 365 | |
| 366 | // Check to see if the type is named. |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 367 | std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty); |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 368 | if (I != TypeNames.end()) return Out << I->second; |
| 369 | |
| 370 | // Otherwise we have a type that has not been named but is a derived type. |
| 371 | // Carefully recurse the type hierarchy to print out any contained symbolic |
| 372 | // names. |
| 373 | // |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 374 | std::vector<const Type *> TypeStack; |
John Criswell | 4ff620a | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 375 | std::string TypeName; |
| 376 | calcTypeName(Ty, TypeStack, TypeNames, TypeName); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 377 | TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use |
John Criswell | 4ff620a | 2004-06-01 14:54:08 +0000 | [diff] [blame] | 378 | return (Out << TypeName); |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Chris Lattner | e51e03b | 2001-10-31 04:33:19 +0000 | [diff] [blame] | 381 | |
Misha Brukman | 9d0802e | 2004-03-01 19:48:13 +0000 | [diff] [blame] | 382 | /// WriteTypeSymbolic - This attempts to write the specified type as a symbolic |
| 383 | /// type, iff there is an entry in the modules symbol table for the specified |
| 384 | /// type or one of it's component types. This is slower than a simple x << Type |
| 385 | /// |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 386 | std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty, |
| 387 | const Module *M) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 388 | Out << ' '; |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 389 | |
| 390 | // If they want us to print out a type, attempt to make it symbolic if there |
| 391 | // is a symbol table in the module... |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 392 | if (M) { |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 393 | std::map<const Type *, std::string> TypeNames; |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 394 | fillTypeNameTable(M, TypeNames); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 395 | |
Chris Lattner | 7b8660d | 2001-10-29 16:40:32 +0000 | [diff] [blame] | 396 | return printTypeInt(Out, Ty, TypeNames); |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 397 | } else { |
Chris Lattner | 7b8660d | 2001-10-29 16:40:32 +0000 | [diff] [blame] | 398 | return Out << Ty->getDescription(); |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
Chris Lattner | 1836550 | 2006-01-23 23:03:36 +0000 | [diff] [blame] | 402 | // PrintEscapedString - Print each character of the specified string, escaping |
| 403 | // it if it is not printable or if it is an escape char. |
| 404 | static void PrintEscapedString(const std::string &Str, std::ostream &Out) { |
| 405 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 406 | unsigned char C = Str[i]; |
| 407 | if (isprint(C) && C != '"' && C != '\\') { |
| 408 | Out << C; |
| 409 | } else { |
| 410 | Out << '\\' |
| 411 | << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A')) |
| 412 | << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A')); |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
Reid Spencer | 81dfeb3 | 2006-12-04 05:19:18 +0000 | [diff] [blame] | 417 | static const char * getPredicateText(unsigned predicate) { |
| 418 | const char * pred = "unknown"; |
| 419 | switch (predicate) { |
| 420 | case FCmpInst::FCMP_FALSE: pred = "false"; break; |
| 421 | case FCmpInst::FCMP_OEQ: pred = "oeq"; break; |
| 422 | case FCmpInst::FCMP_OGT: pred = "ogt"; break; |
| 423 | case FCmpInst::FCMP_OGE: pred = "oge"; break; |
| 424 | case FCmpInst::FCMP_OLT: pred = "olt"; break; |
| 425 | case FCmpInst::FCMP_OLE: pred = "ole"; break; |
| 426 | case FCmpInst::FCMP_ONE: pred = "one"; break; |
| 427 | case FCmpInst::FCMP_ORD: pred = "ord"; break; |
| 428 | case FCmpInst::FCMP_UNO: pred = "uno"; break; |
| 429 | case FCmpInst::FCMP_UEQ: pred = "ueq"; break; |
| 430 | case FCmpInst::FCMP_UGT: pred = "ugt"; break; |
| 431 | case FCmpInst::FCMP_UGE: pred = "uge"; break; |
| 432 | case FCmpInst::FCMP_ULT: pred = "ult"; break; |
| 433 | case FCmpInst::FCMP_ULE: pred = "ule"; break; |
| 434 | case FCmpInst::FCMP_UNE: pred = "une"; break; |
| 435 | case FCmpInst::FCMP_TRUE: pred = "true"; break; |
| 436 | case ICmpInst::ICMP_EQ: pred = "eq"; break; |
| 437 | case ICmpInst::ICMP_NE: pred = "ne"; break; |
| 438 | case ICmpInst::ICMP_SGT: pred = "sgt"; break; |
| 439 | case ICmpInst::ICMP_SGE: pred = "sge"; break; |
| 440 | case ICmpInst::ICMP_SLT: pred = "slt"; break; |
| 441 | case ICmpInst::ICMP_SLE: pred = "sle"; break; |
| 442 | case ICmpInst::ICMP_UGT: pred = "ugt"; break; |
| 443 | case ICmpInst::ICMP_UGE: pred = "uge"; break; |
| 444 | case ICmpInst::ICMP_ULT: pred = "ult"; break; |
| 445 | case ICmpInst::ICMP_ULE: pred = "ule"; break; |
| 446 | } |
| 447 | return pred; |
| 448 | } |
| 449 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 450 | /// @brief Internal constant writer. |
| 451 | static void WriteConstantInt(std::ostream &Out, const Constant *CV, |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 452 | bool PrintName, |
| 453 | std::map<const Type *, std::string> &TypeTable, |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 454 | SlotMachine *Machine) { |
Jim Laskey | 38a409c | 2006-02-27 10:33:53 +0000 | [diff] [blame] | 455 | const int IndentSize = 4; |
| 456 | static std::string Indent = "\n"; |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 457 | if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) { |
Chris Lattner | 9fb471d | 2006-09-28 22:50:29 +0000 | [diff] [blame] | 458 | Out << (CB->getValue() ? "true" : "false"); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 459 | } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
| 460 | if (CI->getType()->isSigned()) |
| 461 | Out << CI->getSExtValue(); |
| 462 | else |
| 463 | Out << CI->getZExtValue(); |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 464 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
| 465 | // We would like to output the FP constant value in exponential notation, |
| 466 | // but we cannot do this if doing so will lose precision. Check here to |
| 467 | // make sure that we only output it in exponential format if we can parse |
| 468 | // the value back and get the same value. |
| 469 | // |
| 470 | std::string StrVal = ftostr(CFP->getValue()); |
| 471 | |
| 472 | // Check to make sure that the stringized number is not some string like |
| 473 | // "Inf" or NaN, that atof will accept, but the lexer will not. Check that |
| 474 | // the string matches the "[-+]?[0-9]" regex. |
| 475 | // |
| 476 | if ((StrVal[0] >= '0' && StrVal[0] <= '9') || |
| 477 | ((StrVal[0] == '-' || StrVal[0] == '+') && |
Brian Gaeke | b471a23 | 2003-06-17 23:55:35 +0000 | [diff] [blame] | 478 | (StrVal[1] >= '0' && StrVal[1] <= '9'))) |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 479 | // Reparse stringized version! |
| 480 | if (atof(StrVal.c_str()) == CFP->getValue()) { |
Chris Lattner | 71d94d1 | 2005-01-04 01:56:57 +0000 | [diff] [blame] | 481 | Out << StrVal; |
| 482 | return; |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 483 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 484 | |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 485 | // Otherwise we could not reparse it to exactly the same value, so we must |
| 486 | // output the string in hexadecimal format! |
Chris Lattner | 71d94d1 | 2005-01-04 01:56:57 +0000 | [diff] [blame] | 487 | assert(sizeof(double) == sizeof(uint64_t) && |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 488 | "assuming that double is 64 bits!"); |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 489 | Out << "0x" << utohexstr(DoubleToBits(CFP->getValue())); |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 490 | |
Chris Lattner | de512b5 | 2004-02-15 05:55:15 +0000 | [diff] [blame] | 491 | } else if (isa<ConstantAggregateZero>(CV)) { |
| 492 | Out << "zeroinitializer"; |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 493 | } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) { |
| 494 | // As a special case, print the array as a string if it is an array of |
| 495 | // ubytes or an array of sbytes with positive values. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 496 | // |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 497 | const Type *ETy = CA->getType()->getElementType(); |
Chris Lattner | 1836550 | 2006-01-23 23:03:36 +0000 | [diff] [blame] | 498 | if (CA->isString()) { |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 499 | Out << "c\""; |
Chris Lattner | 1836550 | 2006-01-23 23:03:36 +0000 | [diff] [blame] | 500 | PrintEscapedString(CA->getAsString(), Out); |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 501 | Out << "\""; |
| 502 | |
| 503 | } else { // Cannot output in string format... |
Misha Brukman | 40c732c | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 504 | Out << '['; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 505 | if (CA->getNumOperands()) { |
Misha Brukman | 40c732c | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 506 | Out << ' '; |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 507 | printTypeInt(Out, ETy, TypeTable); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 508 | WriteAsOperandInternal(Out, CA->getOperand(0), |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 509 | PrintName, TypeTable, Machine); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 510 | for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) { |
| 511 | Out << ", "; |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 512 | printTypeInt(Out, ETy, TypeTable); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 513 | WriteAsOperandInternal(Out, CA->getOperand(i), PrintName, |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 514 | TypeTable, Machine); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | Out << " ]"; |
| 518 | } |
| 519 | } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) { |
Misha Brukman | 40c732c | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 520 | Out << '{'; |
Jim Laskey | a3f332b | 2006-02-25 12:27:03 +0000 | [diff] [blame] | 521 | unsigned N = CS->getNumOperands(); |
| 522 | if (N) { |
Jim Laskey | 38a409c | 2006-02-27 10:33:53 +0000 | [diff] [blame] | 523 | if (N > 2) { |
| 524 | Indent += std::string(IndentSize, ' '); |
| 525 | Out << Indent; |
| 526 | } else { |
| 527 | Out << ' '; |
| 528 | } |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 529 | printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable); |
| 530 | |
| 531 | WriteAsOperandInternal(Out, CS->getOperand(0), |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 532 | PrintName, TypeTable, Machine); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 533 | |
Jim Laskey | a3f332b | 2006-02-25 12:27:03 +0000 | [diff] [blame] | 534 | for (unsigned i = 1; i < N; i++) { |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 535 | Out << ", "; |
Jim Laskey | 38a409c | 2006-02-27 10:33:53 +0000 | [diff] [blame] | 536 | if (N > 2) Out << Indent; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 537 | printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable); |
| 538 | |
| 539 | WriteAsOperandInternal(Out, CS->getOperand(i), |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 540 | PrintName, TypeTable, Machine); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 541 | } |
Jim Laskey | 38a409c | 2006-02-27 10:33:53 +0000 | [diff] [blame] | 542 | if (N > 2) Indent.resize(Indent.size() - IndentSize); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 543 | } |
Jim Laskey | a3f332b | 2006-02-25 12:27:03 +0000 | [diff] [blame] | 544 | |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 545 | Out << " }"; |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 546 | } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) { |
| 547 | const Type *ETy = CP->getType()->getElementType(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 548 | assert(CP->getNumOperands() > 0 && |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 549 | "Number of operands for a PackedConst must be > 0"); |
| 550 | Out << '<'; |
| 551 | Out << ' '; |
| 552 | printTypeInt(Out, ETy, TypeTable); |
| 553 | WriteAsOperandInternal(Out, CP->getOperand(0), |
| 554 | PrintName, TypeTable, Machine); |
| 555 | for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) { |
| 556 | Out << ", "; |
| 557 | printTypeInt(Out, ETy, TypeTable); |
| 558 | WriteAsOperandInternal(Out, CP->getOperand(i), PrintName, |
| 559 | TypeTable, Machine); |
| 560 | } |
| 561 | Out << " >"; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 562 | } else if (isa<ConstantPointerNull>(CV)) { |
| 563 | Out << "null"; |
| 564 | |
Chris Lattner | b976e66 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 565 | } else if (isa<UndefValue>(CV)) { |
| 566 | Out << "undef"; |
| 567 | |
Chris Lattner | c188eeb | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 568 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
Reid Spencer | 81dfeb3 | 2006-12-04 05:19:18 +0000 | [diff] [blame] | 569 | Out << CE->getOpcodeName(); |
| 570 | if (CE->isCompare()) |
| 571 | Out << " " << getPredicateText(CE->getPredicate()); |
| 572 | Out << " ("; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 573 | |
Vikram S. Adve | b4dbb44 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 574 | for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) { |
| 575 | printTypeInt(Out, (*OI)->getType(), TypeTable); |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 576 | WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Machine); |
Vikram S. Adve | b4dbb44 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 577 | if (OI+1 != CE->op_end()) |
Chris Lattner | c188eeb | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 578 | Out << ", "; |
Vikram S. Adve | b4dbb44 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 579 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 580 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 581 | if (CE->isCast()) { |
Chris Lattner | 95586b8 | 2002-08-15 19:37:43 +0000 | [diff] [blame] | 582 | Out << " to "; |
| 583 | printTypeInt(Out, CE->getType(), TypeTable); |
| 584 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 585 | |
Misha Brukman | 40c732c | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 586 | Out << ')'; |
Chris Lattner | 95586b8 | 2002-08-15 19:37:43 +0000 | [diff] [blame] | 587 | |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 588 | } else { |
Vikram S. Adve | b4dbb44 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 589 | Out << "<placeholder or erroneous Constant>"; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
| 593 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 594 | /// WriteAsOperand - Write the name of the specified value out to the specified |
| 595 | /// ostream. This can be useful when you just want to print int %reg126, not |
| 596 | /// the whole instruction that generated it. |
| 597 | /// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 598 | static void WriteAsOperandInternal(std::ostream &Out, const Value *V, |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 599 | bool PrintName, |
| 600 | std::map<const Type*, std::string> &TypeTable, |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 601 | SlotMachine *Machine) { |
Misha Brukman | 40c732c | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 602 | Out << ' '; |
Reid Spencer | 7970396 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 603 | if ((PrintName || isa<GlobalValue>(V)) && V->hasName()) |
Chris Lattner | 24b8a5d | 2003-08-22 05:40:38 +0000 | [diff] [blame] | 604 | Out << getLLVMName(V->getName()); |
Reid Spencer | 7970396 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 605 | else { |
| 606 | const Constant *CV = dyn_cast<Constant>(V); |
Chris Lattner | 80cd115 | 2006-01-25 22:26:05 +0000 | [diff] [blame] | 607 | if (CV && !isa<GlobalValue>(CV)) { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 608 | WriteConstantInt(Out, CV, PrintName, TypeTable, Machine); |
Chris Lattner | 80cd115 | 2006-01-25 22:26:05 +0000 | [diff] [blame] | 609 | } else if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) { |
| 610 | Out << "asm "; |
| 611 | if (IA->hasSideEffects()) |
| 612 | Out << "sideeffect "; |
| 613 | Out << '"'; |
| 614 | PrintEscapedString(IA->getAsmString(), Out); |
| 615 | Out << "\", \""; |
| 616 | PrintEscapedString(IA->getConstraintString(), Out); |
| 617 | Out << '"'; |
| 618 | } else { |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 619 | int Slot; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 620 | if (Machine) { |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 621 | Slot = Machine->getSlot(V); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 622 | } else { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 623 | Machine = createSlotMachine(V); |
Chris Lattner | 5b5cc5f | 2006-05-14 18:46:52 +0000 | [diff] [blame] | 624 | if (Machine) |
Chris Lattner | 6956645 | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 625 | Slot = Machine->getSlot(V); |
| 626 | else |
| 627 | Slot = -1; |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 628 | delete Machine; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 629 | } |
Chris Lattner | 6956645 | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 630 | if (Slot != -1) |
| 631 | Out << '%' << Slot; |
| 632 | else |
| 633 | Out << "<badref>"; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | } |
| 637 | |
Misha Brukman | 9d0802e | 2004-03-01 19:48:13 +0000 | [diff] [blame] | 638 | /// WriteAsOperand - Write the name of the specified value out to the specified |
| 639 | /// ostream. This can be useful when you just want to print int %reg126, not |
| 640 | /// the whole instruction that generated it. |
| 641 | /// |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 642 | std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V, |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 643 | bool PrintType, bool PrintName, |
Misha Brukman | 9d0802e | 2004-03-01 19:48:13 +0000 | [diff] [blame] | 644 | const Module *Context) { |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 645 | std::map<const Type *, std::string> TypeNames; |
Chris Lattner | 607dc68 | 2002-07-10 16:48:17 +0000 | [diff] [blame] | 646 | if (Context == 0) Context = getModuleFromVal(V); |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 647 | |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 648 | if (Context) |
Chris Lattner | 607dc68 | 2002-07-10 16:48:17 +0000 | [diff] [blame] | 649 | fillTypeNameTable(Context, TypeNames); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 650 | |
| 651 | if (PrintType) |
| 652 | printTypeInt(Out, V->getType(), TypeNames); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 653 | |
Chris Lattner | 607dc68 | 2002-07-10 16:48:17 +0000 | [diff] [blame] | 654 | WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0); |
Chris Lattner | 622f740 | 2001-07-20 19:15:21 +0000 | [diff] [blame] | 655 | return Out; |
| 656 | } |
| 657 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 658 | /// WriteAsOperandInternal - Write the name of the specified value out to |
| 659 | /// the specified ostream. This can be useful when you just want to print |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 660 | /// int %reg126, not the whole instruction that generated it. |
| 661 | /// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 662 | static void WriteAsOperandInternal(std::ostream &Out, const Type *T, |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 663 | bool PrintName, |
| 664 | std::map<const Type*, std::string> &TypeTable, |
| 665 | SlotMachine *Machine) { |
| 666 | Out << ' '; |
| 667 | int Slot; |
| 668 | if (Machine) { |
| 669 | Slot = Machine->getSlot(T); |
| 670 | if (Slot != -1) |
| 671 | Out << '%' << Slot; |
| 672 | else |
| 673 | Out << "<badref>"; |
| 674 | } else { |
| 675 | Out << T->getDescription(); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | /// WriteAsOperand - Write the name of the specified value out to the specified |
| 680 | /// ostream. This can be useful when you just want to print int %reg126, not |
| 681 | /// the whole instruction that generated it. |
| 682 | /// |
| 683 | std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Type *Ty, |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 684 | bool PrintType, bool PrintName, |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 685 | const Module *Context) { |
| 686 | std::map<const Type *, std::string> TypeNames; |
| 687 | assert(Context != 0 && "Can't write types as operand without module context"); |
| 688 | |
| 689 | fillTypeNameTable(Context, TypeNames); |
| 690 | |
| 691 | // if (PrintType) |
| 692 | // printTypeInt(Out, V->getType(), TypeNames); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 693 | |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 694 | printTypeInt(Out, Ty, TypeNames); |
| 695 | |
| 696 | WriteAsOperandInternal(Out, Ty, PrintName, TypeNames, 0); |
| 697 | return Out; |
| 698 | } |
| 699 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 700 | namespace llvm { |
Chris Lattner | d8c2e42 | 2001-07-12 23:35:26 +0000 | [diff] [blame] | 701 | |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 702 | class AssemblyWriter { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 703 | std::ostream &Out; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 704 | SlotMachine &Machine; |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 705 | const Module *TheModule; |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 706 | std::map<const Type *, std::string> TypeNames; |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 707 | AssemblyAnnotationWriter *AnnotationWriter; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 708 | public: |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 709 | inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M, |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 710 | AssemblyAnnotationWriter *AAW) |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 711 | : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) { |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 712 | |
| 713 | // If the module has a symbol table, take all global types and stuff their |
| 714 | // names into the TypeNames map. |
| 715 | // |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 716 | fillTypeNameTable(M, TypeNames); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 719 | inline void write(const Module *M) { printModule(M); } |
| 720 | inline void write(const GlobalVariable *G) { printGlobal(G); } |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 721 | inline void write(const Function *F) { printFunction(F); } |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 722 | inline void write(const BasicBlock *BB) { printBasicBlock(BB); } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 723 | inline void write(const Instruction *I) { printInstruction(*I); } |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 724 | inline void write(const Constant *CPV) { printConstant(CPV); } |
Chris Lattner | da1fbcc | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 725 | inline void write(const Type *Ty) { printType(Ty); } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 726 | |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 727 | void writeOperand(const Value *Op, bool PrintType, bool PrintName = true); |
| 728 | |
Misha Brukman | 5cf1acf | 2004-04-28 15:31:21 +0000 | [diff] [blame] | 729 | const Module* getModule() { return TheModule; } |
| 730 | |
Misha Brukman | f771bea | 2004-11-15 19:30:05 +0000 | [diff] [blame] | 731 | private: |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 732 | void printModule(const Module *M); |
| 733 | void printSymbolTable(const SymbolTable &ST); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 734 | void printConstant(const Constant *CPV); |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 735 | void printGlobal(const GlobalVariable *GV); |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 736 | void printFunction(const Function *F); |
Chris Lattner | 73e2142 | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 737 | void printArgument(const Argument *FA); |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 738 | void printBasicBlock(const BasicBlock *BB); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 739 | void printInstruction(const Instruction &I); |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 740 | |
| 741 | // printType - Go to extreme measures to attempt to print out a short, |
| 742 | // symbolic version of a type name. |
| 743 | // |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 744 | std::ostream &printType(const Type *Ty) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 745 | return printTypeInt(Out, Ty, TypeNames); |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | // printTypeAtLeastOneLevel - Print out one level of the possibly complex type |
| 749 | // without considering any symbolic types that we may have equal to it. |
| 750 | // |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 751 | std::ostream &printTypeAtLeastOneLevel(const Type *Ty); |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 752 | |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 753 | // printInfoComment - Print a little comment after the instruction indicating |
| 754 | // which slot it occupies. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 755 | void printInfoComment(const Value &V); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 756 | }; |
Reid Spencer | 73b7495 | 2004-05-27 22:04:46 +0000 | [diff] [blame] | 757 | } // end of llvm namespace |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 758 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 759 | /// printTypeAtLeastOneLevel - Print out one level of the possibly complex type |
| 760 | /// without considering any symbolic types that we may have equal to it. |
| 761 | /// |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 762 | std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 763 | if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) { |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 764 | printType(FTy->getReturnType()) << " ("; |
Chris Lattner | d5d8996 | 2004-02-09 04:14:01 +0000 | [diff] [blame] | 765 | for (FunctionType::param_iterator I = FTy->param_begin(), |
| 766 | E = FTy->param_end(); I != E; ++I) { |
| 767 | if (I != FTy->param_begin()) |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 768 | Out << ", "; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 769 | printType(*I); |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 770 | } |
| 771 | if (FTy->isVarArg()) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 772 | if (FTy->getNumParams()) Out << ", "; |
| 773 | Out << "..."; |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 774 | } |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 775 | Out << ')'; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 776 | } else if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 777 | Out << "{ "; |
Chris Lattner | d21cd80 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 778 | for (StructType::element_iterator I = STy->element_begin(), |
| 779 | E = STy->element_end(); I != E; ++I) { |
| 780 | if (I != STy->element_begin()) |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 781 | Out << ", "; |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 782 | printType(*I); |
| 783 | } |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 784 | Out << " }"; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 785 | } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) { |
Misha Brukman | 40c732c | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 786 | printType(PTy->getElementType()) << '*'; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 787 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 788 | Out << '[' << ATy->getNumElements() << " x "; |
Misha Brukman | 40c732c | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 789 | printType(ATy->getElementType()) << ']'; |
Reid Spencer | 5527c0b | 2004-08-20 15:37:30 +0000 | [diff] [blame] | 790 | } else if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) { |
| 791 | Out << '<' << PTy->getNumElements() << " x "; |
| 792 | printType(PTy->getElementType()) << '>'; |
| 793 | } |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 794 | else if (isa<OpaqueType>(Ty)) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 795 | Out << "opaque"; |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 796 | } else { |
Vikram S. Adve | b4dbb44 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 797 | if (!Ty->isPrimitiveType()) |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 798 | Out << "<unknown derived type>"; |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 799 | printType(Ty); |
| 800 | } |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 801 | return Out; |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 805 | void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType, |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 806 | bool PrintName) { |
Chris Lattner | aab1820 | 2005-02-24 16:58:29 +0000 | [diff] [blame] | 807 | if (Operand != 0) { |
| 808 | if (PrintType) { Out << ' '; printType(Operand->getType()); } |
| 809 | WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Machine); |
| 810 | } else { |
| 811 | Out << "<null operand!>"; |
| 812 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 815 | |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 816 | void AssemblyWriter::printModule(const Module *M) { |
Chris Lattner | 31ab1b3 | 2005-03-02 23:12:40 +0000 | [diff] [blame] | 817 | if (!M->getModuleIdentifier().empty() && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 818 | // Don't print the ID if it will start a new line (which would |
Chris Lattner | 31ab1b3 | 2005-03-02 23:12:40 +0000 | [diff] [blame] | 819 | // require a comment char before it). |
| 820 | M->getModuleIdentifier().find('\n') == std::string::npos) |
| 821 | Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n"; |
| 822 | |
Owen Anderson | cf7ff2b | 2006-10-18 02:21:12 +0000 | [diff] [blame] | 823 | if (!M->getDataLayout().empty()) |
Chris Lattner | d2f9e60 | 2006-10-22 06:06:56 +0000 | [diff] [blame] | 824 | Out << "target datalayout = \"" << M->getDataLayout() << "\"\n"; |
Owen Anderson | cf7ff2b | 2006-10-18 02:21:12 +0000 | [diff] [blame] | 825 | |
Chris Lattner | eb5d3a1 | 2003-08-24 13:48:48 +0000 | [diff] [blame] | 826 | switch (M->getEndianness()) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 827 | case Module::LittleEndian: Out << "target endian = little\n"; break; |
| 828 | case Module::BigEndian: Out << "target endian = big\n"; break; |
Chris Lattner | eb5d3a1 | 2003-08-24 13:48:48 +0000 | [diff] [blame] | 829 | case Module::AnyEndianness: break; |
| 830 | } |
| 831 | switch (M->getPointerSize()) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 832 | case Module::Pointer32: Out << "target pointersize = 32\n"; break; |
| 833 | case Module::Pointer64: Out << "target pointersize = 64\n"; break; |
Chris Lattner | eb5d3a1 | 2003-08-24 13:48:48 +0000 | [diff] [blame] | 834 | case Module::AnyPointerSize: break; |
| 835 | } |
Reid Spencer | cddc86f | 2004-07-25 21:44:54 +0000 | [diff] [blame] | 836 | if (!M->getTargetTriple().empty()) |
Reid Spencer | c9a1f0d | 2004-07-25 21:29:43 +0000 | [diff] [blame] | 837 | Out << "target triple = \"" << M->getTargetTriple() << "\"\n"; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 838 | |
Chris Lattner | cc041ba | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 839 | if (!M->getModuleInlineAsm().empty()) { |
Chris Lattner | 42a162e | 2006-01-24 00:45:30 +0000 | [diff] [blame] | 840 | // Split the string into lines, to make it easier to read the .ll file. |
Chris Lattner | cc041ba | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 841 | std::string Asm = M->getModuleInlineAsm(); |
Chris Lattner | 42a162e | 2006-01-24 00:45:30 +0000 | [diff] [blame] | 842 | size_t CurPos = 0; |
| 843 | size_t NewLine = Asm.find_first_of('\n', CurPos); |
| 844 | while (NewLine != std::string::npos) { |
| 845 | // We found a newline, print the portion of the asm string from the |
| 846 | // last newline up to this newline. |
| 847 | Out << "module asm \""; |
| 848 | PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine), |
| 849 | Out); |
| 850 | Out << "\"\n"; |
| 851 | CurPos = NewLine+1; |
| 852 | NewLine = Asm.find_first_of('\n', CurPos); |
| 853 | } |
Chris Lattner | 71cdba3 | 2006-01-24 00:40:17 +0000 | [diff] [blame] | 854 | Out << "module asm \""; |
Chris Lattner | 42a162e | 2006-01-24 00:45:30 +0000 | [diff] [blame] | 855 | PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out); |
Chris Lattner | 1836550 | 2006-01-23 23:03:36 +0000 | [diff] [blame] | 856 | Out << "\"\n"; |
| 857 | } |
| 858 | |
Chris Lattner | 44da7d7 | 2004-09-14 05:06:58 +0000 | [diff] [blame] | 859 | // Loop over the dependent libraries and emit them. |
Chris Lattner | cfe97b7 | 2004-09-14 04:51:44 +0000 | [diff] [blame] | 860 | Module::lib_iterator LI = M->lib_begin(); |
| 861 | Module::lib_iterator LE = M->lib_end(); |
Reid Spencer | cddc86f | 2004-07-25 21:44:54 +0000 | [diff] [blame] | 862 | if (LI != LE) { |
Chris Lattner | cfe97b7 | 2004-09-14 04:51:44 +0000 | [diff] [blame] | 863 | Out << "deplibs = [ "; |
| 864 | while (LI != LE) { |
Chris Lattner | 44da7d7 | 2004-09-14 05:06:58 +0000 | [diff] [blame] | 865 | Out << '"' << *LI << '"'; |
Reid Spencer | c9a1f0d | 2004-07-25 21:29:43 +0000 | [diff] [blame] | 866 | ++LI; |
Chris Lattner | cfe97b7 | 2004-09-14 04:51:44 +0000 | [diff] [blame] | 867 | if (LI != LE) |
| 868 | Out << ", "; |
Reid Spencer | c9a1f0d | 2004-07-25 21:29:43 +0000 | [diff] [blame] | 869 | } |
| 870 | Out << " ]\n"; |
Reid Spencer | 83f6a77 | 2004-07-25 18:08:18 +0000 | [diff] [blame] | 871 | } |
Reid Spencer | e59eaf4 | 2004-09-13 23:44:23 +0000 | [diff] [blame] | 872 | |
Chris Lattner | 44da7d7 | 2004-09-14 05:06:58 +0000 | [diff] [blame] | 873 | // Loop over the symbol table, emitting all named constants. |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 874 | printSymbolTable(M->getSymbolTable()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 875 | |
Chris Lattner | d6d826c | 2006-12-06 04:41:52 +0000 | [diff] [blame] | 876 | for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); |
| 877 | I != E; ++I) |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 878 | printGlobal(I); |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 879 | |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 880 | Out << "\nimplementation ; Functions:\n"; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 881 | |
Chris Lattner | 44da7d7 | 2004-09-14 05:06:58 +0000 | [diff] [blame] | 882 | // Output all of the functions. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 883 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 884 | printFunction(I); |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 887 | void AssemblyWriter::printGlobal(const GlobalVariable *GV) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 888 | if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = "; |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 889 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 890 | if (!GV->hasInitializer()) |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 891 | switch (GV->getLinkage()) { |
| 892 | case GlobalValue::DLLImportLinkage: Out << "dllimport "; break; |
| 893 | case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break; |
| 894 | default: Out << "external "; break; |
| 895 | } |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 896 | else |
| 897 | switch (GV->getLinkage()) { |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 898 | case GlobalValue::InternalLinkage: Out << "internal "; break; |
| 899 | case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break; |
| 900 | case GlobalValue::WeakLinkage: Out << "weak "; break; |
| 901 | case GlobalValue::AppendingLinkage: Out << "appending "; break; |
| 902 | case GlobalValue::DLLImportLinkage: Out << "dllimport "; break; |
| 903 | case GlobalValue::DLLExportLinkage: Out << "dllexport "; break; |
| 904 | case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break; |
| 905 | case GlobalValue::ExternalLinkage: break; |
Misha Brukman | 3c8f2c6 | 2004-11-14 21:04:34 +0000 | [diff] [blame] | 906 | case GlobalValue::GhostLinkage: |
Bill Wendling | 8f48766 | 2006-11-28 02:09:03 +0000 | [diff] [blame] | 907 | llvm_cerr << "GhostLinkage not allowed in AsmWriter!\n"; |
Misha Brukman | 3c8f2c6 | 2004-11-14 21:04:34 +0000 | [diff] [blame] | 908 | abort(); |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 909 | } |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 910 | |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 911 | Out << (GV->isConstant() ? "constant " : "global "); |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 912 | printType(GV->getType()->getElementType()); |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 913 | |
Reid Spencer | 7970396 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 914 | if (GV->hasInitializer()) { |
| 915 | Constant* C = cast<Constant>(GV->getInitializer()); |
| 916 | assert(C && "GlobalVar initializer isn't constant?"); |
Reid Spencer | acc9280 | 2004-07-18 01:04:19 +0000 | [diff] [blame] | 917 | writeOperand(GV->getInitializer(), false, isa<GlobalValue>(C)); |
Reid Spencer | 7970396 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 918 | } |
Chris Lattner | 30caa28 | 2005-11-06 06:48:53 +0000 | [diff] [blame] | 919 | |
Chris Lattner | 60962db | 2005-11-12 00:10:19 +0000 | [diff] [blame] | 920 | if (GV->hasSection()) |
| 921 | Out << ", section \"" << GV->getSection() << '"'; |
| 922 | if (GV->getAlignment()) |
Chris Lattner | 30caa28 | 2005-11-06 06:48:53 +0000 | [diff] [blame] | 923 | Out << ", align " << GV->getAlignment(); |
Chris Lattner | 60962db | 2005-11-12 00:10:19 +0000 | [diff] [blame] | 924 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 925 | printInfoComment(*GV); |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 926 | Out << "\n"; |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 929 | |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 930 | // printSymbolTable - Run through symbol table looking for constants |
| 931 | // and types. Emit their declarations. |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 932 | void AssemblyWriter::printSymbolTable(const SymbolTable &ST) { |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 933 | |
| 934 | // Print the types. |
| 935 | for (SymbolTable::type_const_iterator TI = ST.type_begin(); |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 936 | TI != ST.type_end(); ++TI) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 937 | Out << "\t" << getLLVMName(TI->first) << " = type "; |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 938 | |
| 939 | // Make sure we print out at least one level of the type structure, so |
| 940 | // that we do not get %FILE = type %FILE |
| 941 | // |
| 942 | printTypeAtLeastOneLevel(TI->second) << "\n"; |
| 943 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 944 | |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 945 | // Print the constants, in type plane order. |
| 946 | for (SymbolTable::plane_const_iterator PI = ST.plane_begin(); |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 947 | PI != ST.plane_end(); ++PI) { |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 948 | SymbolTable::value_const_iterator VI = ST.value_begin(PI->first); |
| 949 | SymbolTable::value_const_iterator VE = ST.value_end(PI->first); |
| 950 | |
| 951 | for (; VI != VE; ++VI) { |
Reid Spencer | 7970396 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 952 | const Value* V = VI->second; |
| 953 | const Constant *CPV = dyn_cast<Constant>(V) ; |
| 954 | if (CPV && !isa<GlobalValue>(V)) { |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 955 | printConstant(CPV); |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 956 | } |
| 957 | } |
Chris Lattner | 739a56d | 2001-07-15 06:35:59 +0000 | [diff] [blame] | 958 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 962 | /// printConstant - Print out a constant pool entry... |
| 963 | /// |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 964 | void AssemblyWriter::printConstant(const Constant *CPV) { |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 965 | // Don't print out unnamed constants, they will be inlined |
| 966 | if (!CPV->hasName()) return; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 967 | |
Chris Lattner | 1333ed5 | 2001-07-26 16:29:38 +0000 | [diff] [blame] | 968 | // Print out name... |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 969 | Out << "\t" << getLLVMName(CPV->getName()) << " ="; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 970 | |
| 971 | // Write the value out now... |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 972 | writeOperand(CPV, true, false); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 973 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 974 | printInfoComment(*CPV); |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 975 | Out << "\n"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 978 | /// printFunction - Print all aspects of a function. |
| 979 | /// |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 980 | void AssemblyWriter::printFunction(const Function *F) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 981 | // Print out the return type and name... |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 982 | Out << "\n"; |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 983 | |
Chris Lattner | 333529e | 2004-12-05 06:44:09 +0000 | [diff] [blame] | 984 | // Ensure that no local symbols conflict with global symbols. |
| 985 | const_cast<Function*>(F)->renameLocalSymbols(); |
| 986 | |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 987 | if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out); |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 988 | |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 989 | if (F->isExternal()) |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 990 | switch (F->getLinkage()) { |
| 991 | case GlobalValue::DLLImportLinkage: Out << "declare dllimport "; break; |
| 992 | case GlobalValue::ExternalWeakLinkage: Out << "declare extern_weak "; break; |
| 993 | default: Out << "declare "; |
| 994 | } |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 995 | else |
| 996 | switch (F->getLinkage()) { |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 997 | case GlobalValue::InternalLinkage: Out << "internal "; break; |
| 998 | case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break; |
| 999 | case GlobalValue::WeakLinkage: Out << "weak "; break; |
| 1000 | case GlobalValue::AppendingLinkage: Out << "appending "; break; |
| 1001 | case GlobalValue::DLLImportLinkage: Out << "dllimport "; break; |
| 1002 | case GlobalValue::DLLExportLinkage: Out << "dllexport "; break; |
| 1003 | case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break; |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 1004 | case GlobalValue::ExternalLinkage: break; |
Misha Brukman | 3c8f2c6 | 2004-11-14 21:04:34 +0000 | [diff] [blame] | 1005 | case GlobalValue::GhostLinkage: |
Bill Wendling | 8f48766 | 2006-11-28 02:09:03 +0000 | [diff] [blame] | 1006 | llvm_cerr << "GhostLinkage not allowed in AsmWriter!\n"; |
Misha Brukman | 3c8f2c6 | 2004-11-14 21:04:34 +0000 | [diff] [blame] | 1007 | abort(); |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1010 | // Print the calling convention. |
| 1011 | switch (F->getCallingConv()) { |
| 1012 | case CallingConv::C: break; // default |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 1013 | case CallingConv::CSRet: Out << "csretcc "; break; |
| 1014 | case CallingConv::Fast: Out << "fastcc "; break; |
| 1015 | case CallingConv::Cold: Out << "coldcc "; break; |
| 1016 | case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break; |
| 1017 | case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break; |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1018 | default: Out << "cc" << F->getCallingConv() << " "; break; |
| 1019 | } |
| 1020 | |
Misha Brukman | 40c732c | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 1021 | printType(F->getReturnType()) << ' '; |
Chris Lattner | 4d45bd0 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 1022 | if (!F->getName().empty()) |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1023 | Out << getLLVMName(F->getName()); |
Chris Lattner | 4d45bd0 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 1024 | else |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1025 | Out << "\"\""; |
| 1026 | Out << '('; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1027 | Machine.incorporateFunction(F); |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1028 | |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1029 | // Loop over the arguments, printing them... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1030 | const FunctionType *FT = F->getFunctionType(); |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1031 | |
Chris Lattner | d6d826c | 2006-12-06 04:41:52 +0000 | [diff] [blame] | 1032 | for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 1033 | I != E; ++I) |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 1034 | printArgument(I); |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1035 | |
| 1036 | // Finish printing arguments... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1037 | if (FT->isVarArg()) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1038 | if (FT->getNumParams()) Out << ", "; |
| 1039 | Out << "..."; // Output varargs portion of signature! |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1040 | } |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1041 | Out << ')'; |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1042 | |
Chris Lattner | 60962db | 2005-11-12 00:10:19 +0000 | [diff] [blame] | 1043 | if (F->hasSection()) |
| 1044 | Out << " section \"" << F->getSection() << '"'; |
Chris Lattner | 30caa28 | 2005-11-06 06:48:53 +0000 | [diff] [blame] | 1045 | if (F->getAlignment()) |
| 1046 | Out << " align " << F->getAlignment(); |
Chris Lattner | 60962db | 2005-11-12 00:10:19 +0000 | [diff] [blame] | 1047 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1048 | if (F->isExternal()) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1049 | Out << "\n"; |
Chris Lattner | 03e2acb | 2002-05-06 03:00:40 +0000 | [diff] [blame] | 1050 | } else { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1051 | Out << " {"; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1052 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 1053 | // Output all of its basic blocks... for the function |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1054 | for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I) |
| 1055 | printBasicBlock(I); |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1056 | |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1057 | Out << "}\n"; |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1060 | Machine.purgeFunction(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1063 | /// printArgument - This member is called for every argument that is passed into |
| 1064 | /// the function. Simply print it out |
| 1065 | /// |
Chris Lattner | 73e2142 | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 1066 | void AssemblyWriter::printArgument(const Argument *Arg) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1067 | // Insert commas as we go... the first arg doesn't get a comma |
Chris Lattner | 39220de | 2005-03-15 05:03:36 +0000 | [diff] [blame] | 1068 | if (Arg != Arg->getParent()->arg_begin()) Out << ", "; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1069 | |
| 1070 | // Output type... |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1071 | printType(Arg->getType()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1072 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1073 | // Output name, if available... |
| 1074 | if (Arg->hasName()) |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1075 | Out << ' ' << getLLVMName(Arg->getName()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1078 | /// printBasicBlock - This member is called for each basic block in a method. |
| 1079 | /// |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1080 | void AssemblyWriter::printBasicBlock(const BasicBlock *BB) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1081 | if (BB->hasName()) { // Print out the label if it exists... |
Alkis Evlogimenos | 9913e59 | 2004-12-10 05:41:10 +0000 | [diff] [blame] | 1082 | Out << "\n" << getLLVMName(BB->getName(), false) << ':'; |
Chris Lattner | afc3868 | 2002-05-14 16:02:05 +0000 | [diff] [blame] | 1083 | } else if (!BB->use_empty()) { // Don't print block # of no uses... |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1084 | Out << "\n; <label>:"; |
Chris Lattner | 6956645 | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 1085 | int Slot = Machine.getSlot(BB); |
| 1086 | if (Slot != -1) |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1087 | Out << Slot; |
Chris Lattner | 6956645 | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 1088 | else |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1089 | Out << "<badref>"; |
Chris Lattner | 061269b | 2002-10-02 19:38:55 +0000 | [diff] [blame] | 1090 | } |
Chris Lattner | 4e4d862 | 2003-11-20 00:09:43 +0000 | [diff] [blame] | 1091 | |
| 1092 | if (BB->getParent() == 0) |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1093 | Out << "\t\t; Error: Block without parent!"; |
Chris Lattner | 4e4d862 | 2003-11-20 00:09:43 +0000 | [diff] [blame] | 1094 | else { |
| 1095 | if (BB != &BB->getParent()->front()) { // Not the entry block? |
| 1096 | // Output predecessors for the block... |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1097 | Out << "\t\t;"; |
Chris Lattner | 4e4d862 | 2003-11-20 00:09:43 +0000 | [diff] [blame] | 1098 | pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1099 | |
Chris Lattner | 4e4d862 | 2003-11-20 00:09:43 +0000 | [diff] [blame] | 1100 | if (PI == PE) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1101 | Out << " No predecessors!"; |
Chris Lattner | 4e4d862 | 2003-11-20 00:09:43 +0000 | [diff] [blame] | 1102 | } else { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1103 | Out << " preds ="; |
Chris Lattner | 40efcec | 2003-11-16 22:59:57 +0000 | [diff] [blame] | 1104 | writeOperand(*PI, false, true); |
Chris Lattner | 4e4d862 | 2003-11-20 00:09:43 +0000 | [diff] [blame] | 1105 | for (++PI; PI != PE; ++PI) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1106 | Out << ','; |
Chris Lattner | 4e4d862 | 2003-11-20 00:09:43 +0000 | [diff] [blame] | 1107 | writeOperand(*PI, false, true); |
| 1108 | } |
Chris Lattner | 40efcec | 2003-11-16 22:59:57 +0000 | [diff] [blame] | 1109 | } |
Chris Lattner | 061269b | 2002-10-02 19:38:55 +0000 | [diff] [blame] | 1110 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1111 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1112 | |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1113 | Out << "\n"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1114 | |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1115 | if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out); |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1116 | |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1117 | // Output all of the instructions in the basic block... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1118 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 1119 | printInstruction(*I); |
Chris Lattner | 9f717ef | 2004-03-08 18:51:45 +0000 | [diff] [blame] | 1120 | |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1121 | if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1124 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1125 | /// printInfoComment - Print a little comment after the instruction indicating |
| 1126 | /// which slot it occupies. |
| 1127 | /// |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1128 | void AssemblyWriter::printInfoComment(const Value &V) { |
| 1129 | if (V.getType() != Type::VoidTy) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1130 | Out << "\t\t; <"; |
Misha Brukman | 40c732c | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 1131 | printType(V.getType()) << '>'; |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1132 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1133 | if (!V.hasName()) { |
Chris Lattner | 6956645 | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 1134 | int SlotNum = Machine.getSlot(&V); |
| 1135 | if (SlotNum == -1) |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1136 | Out << ":<badref>"; |
Reid Spencer | fc621e2 | 2004-06-09 15:26:53 +0000 | [diff] [blame] | 1137 | else |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1138 | Out << ':' << SlotNum; // Print out the def slot taken. |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1139 | } |
Chris Lattner | 5c46140 | 2005-02-01 01:24:01 +0000 | [diff] [blame] | 1140 | Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1141 | } |
| 1142 | } |
| 1143 | |
Reid Spencer | 3a9ec24 | 2006-08-28 01:02:49 +0000 | [diff] [blame] | 1144 | // This member is called for each Instruction in a function.. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1145 | void AssemblyWriter::printInstruction(const Instruction &I) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1146 | if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out); |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1147 | |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1148 | Out << "\t"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1149 | |
| 1150 | // Print out name if it exists... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1151 | if (I.hasName()) |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1152 | Out << getLLVMName(I.getName()) << " = "; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1153 | |
Chris Lattner | ddb6db4 | 2005-05-06 05:51:46 +0000 | [diff] [blame] | 1154 | // If this is a volatile load or store, print out the volatile marker. |
Chris Lattner | e5e475e | 2003-09-08 17:45:59 +0000 | [diff] [blame] | 1155 | if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) || |
Chris Lattner | ddb6db4 | 2005-05-06 05:51:46 +0000 | [diff] [blame] | 1156 | (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1157 | Out << "volatile "; |
Chris Lattner | ddb6db4 | 2005-05-06 05:51:46 +0000 | [diff] [blame] | 1158 | } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) { |
| 1159 | // If this is a call, check if it's a tail call. |
| 1160 | Out << "tail "; |
| 1161 | } |
Chris Lattner | e5e475e | 2003-09-08 17:45:59 +0000 | [diff] [blame] | 1162 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1163 | // Print out the opcode... |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1164 | Out << I.getOpcodeName(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1165 | |
Reid Spencer | 74f1642 | 2006-12-03 06:27:29 +0000 | [diff] [blame] | 1166 | // Print out the compare instruction predicates |
| 1167 | if (const FCmpInst *FCI = dyn_cast<FCmpInst>(&I)) { |
Reid Spencer | 81dfeb3 | 2006-12-04 05:19:18 +0000 | [diff] [blame] | 1168 | Out << " " << getPredicateText(FCI->getPredicate()); |
Reid Spencer | 74f1642 | 2006-12-03 06:27:29 +0000 | [diff] [blame] | 1169 | } else if (const ICmpInst *ICI = dyn_cast<ICmpInst>(&I)) { |
Reid Spencer | 81dfeb3 | 2006-12-04 05:19:18 +0000 | [diff] [blame] | 1170 | Out << " " << getPredicateText(ICI->getPredicate()); |
Reid Spencer | 74f1642 | 2006-12-03 06:27:29 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1173 | // Print out the type of the operands... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1174 | const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1175 | |
| 1176 | // Special case conditional branches to swizzle the condition out to the front |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1177 | if (isa<BranchInst>(I) && I.getNumOperands() > 1) { |
| 1178 | writeOperand(I.getOperand(2), true); |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1179 | Out << ','; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1180 | writeOperand(Operand, true); |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1181 | Out << ','; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1182 | writeOperand(I.getOperand(1), true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1183 | |
Chris Lattner | 94dc1f2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 1184 | } else if (isa<SwitchInst>(I)) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1185 | // Special case switch statement to get formatting nice and correct... |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1186 | writeOperand(Operand , true); Out << ','; |
| 1187 | writeOperand(I.getOperand(1), true); Out << " ["; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1188 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1189 | for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1190 | Out << "\n\t\t"; |
| 1191 | writeOperand(I.getOperand(op ), true); Out << ','; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1192 | writeOperand(I.getOperand(op+1), true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1193 | } |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1194 | Out << "\n\t]"; |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 1195 | } else if (isa<PHINode>(I)) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1196 | Out << ' '; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1197 | printType(I.getType()); |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1198 | Out << ' '; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1199 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1200 | for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1201 | if (op) Out << ", "; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1202 | Out << '['; |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1203 | writeOperand(I.getOperand(op ), false); Out << ','; |
| 1204 | writeOperand(I.getOperand(op+1), false); Out << " ]"; |
Chris Lattner | c24d208 | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 1205 | } |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1206 | } else if (isa<ReturnInst>(I) && !Operand) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1207 | Out << " void"; |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1208 | } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) { |
| 1209 | // Print the calling convention being used. |
| 1210 | switch (CI->getCallingConv()) { |
| 1211 | case CallingConv::C: break; // default |
Chris Lattner | 0deaab8 | 2006-05-19 21:58:52 +0000 | [diff] [blame] | 1212 | case CallingConv::CSRet: Out << " csretcc"; break; |
| 1213 | case CallingConv::Fast: Out << " fastcc"; break; |
| 1214 | case CallingConv::Cold: Out << " coldcc"; break; |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 1215 | case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break; |
| 1216 | case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break; |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1217 | default: Out << " cc" << CI->getCallingConv(); break; |
| 1218 | } |
| 1219 | |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1220 | const PointerType *PTy = cast<PointerType>(Operand->getType()); |
| 1221 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 1222 | const Type *RetTy = FTy->getReturnType(); |
Chris Lattner | 268de04 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 1223 | |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1224 | // If possible, print out the short form of the call instruction. We can |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 1225 | // only do this if the first argument is a pointer to a nonvararg function, |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1226 | // and if the return type is not a pointer to a function. |
Chris Lattner | 268de04 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 1227 | // |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1228 | if (!FTy->isVarArg() && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1229 | (!isa<PointerType>(RetTy) || |
Chris Lattner | c1b2718 | 2002-07-25 20:58:51 +0000 | [diff] [blame] | 1230 | !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1231 | Out << ' '; printType(RetTy); |
Chris Lattner | 268de04 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 1232 | writeOperand(Operand, false); |
| 1233 | } else { |
| 1234 | writeOperand(Operand, true); |
| 1235 | } |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1236 | Out << '('; |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1237 | if (CI->getNumOperands() > 1) writeOperand(CI->getOperand(1), true); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1238 | for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1239 | Out << ','; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1240 | writeOperand(I.getOperand(op), true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1243 | Out << " )"; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1244 | } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) { |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1245 | const PointerType *PTy = cast<PointerType>(Operand->getType()); |
| 1246 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 1247 | const Type *RetTy = FTy->getReturnType(); |
| 1248 | |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1249 | // Print the calling convention being used. |
| 1250 | switch (II->getCallingConv()) { |
| 1251 | case CallingConv::C: break; // default |
Chris Lattner | 0deaab8 | 2006-05-19 21:58:52 +0000 | [diff] [blame] | 1252 | case CallingConv::CSRet: Out << " csretcc"; break; |
| 1253 | case CallingConv::Fast: Out << " fastcc"; break; |
| 1254 | case CallingConv::Cold: Out << " coldcc"; break; |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 1255 | case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break; |
| 1256 | case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break; |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1257 | default: Out << " cc" << II->getCallingConv(); break; |
| 1258 | } |
| 1259 | |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1260 | // If possible, print out the short form of the invoke instruction. We can |
| 1261 | // only do this if the first argument is a pointer to a nonvararg function, |
| 1262 | // and if the return type is not a pointer to a function. |
| 1263 | // |
| 1264 | if (!FTy->isVarArg() && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1265 | (!isa<PointerType>(RetTy) || |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1266 | !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1267 | Out << ' '; printType(RetTy); |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1268 | writeOperand(Operand, false); |
| 1269 | } else { |
| 1270 | writeOperand(Operand, true); |
| 1271 | } |
| 1272 | |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1273 | Out << '('; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1274 | if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true); |
| 1275 | for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1276 | Out << ','; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1277 | writeOperand(I.getOperand(op), true); |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1280 | Out << " )\n\t\t\tto"; |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1281 | writeOperand(II->getNormalDest(), true); |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1282 | Out << " unwind"; |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 1283 | writeOperand(II->getUnwindDest(), true); |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1284 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1285 | } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1286 | Out << ' '; |
Chris Lattner | 94dc1f2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 1287 | printType(AI->getType()->getElementType()); |
| 1288 | if (AI->isArrayAllocation()) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1289 | Out << ','; |
Chris Lattner | 94dc1f2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 1290 | writeOperand(AI->getArraySize(), true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1291 | } |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 1292 | if (AI->getAlignment()) { |
Chris Lattner | 9fad0b9 | 2005-11-05 21:20:34 +0000 | [diff] [blame] | 1293 | Out << ", align " << AI->getAlignment(); |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 1294 | } |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1295 | } else if (isa<CastInst>(I)) { |
Chris Lattner | 41495a2 | 2003-11-17 01:17:04 +0000 | [diff] [blame] | 1296 | if (Operand) writeOperand(Operand, true); // Work with broken code |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1297 | Out << " to "; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1298 | printType(I.getType()); |
Chris Lattner | 4d45bd0 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 1299 | } else if (isa<VAArgInst>(I)) { |
Chris Lattner | 41495a2 | 2003-11-17 01:17:04 +0000 | [diff] [blame] | 1300 | if (Operand) writeOperand(Operand, true); // Work with broken code |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1301 | Out << ", "; |
Chris Lattner | 8f77dae | 2003-05-08 02:44:12 +0000 | [diff] [blame] | 1302 | printType(I.getType()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1303 | } else if (Operand) { // Print the normal way... |
| 1304 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1305 | // PrintAllTypes - Instructions who have operands of all the same type |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1306 | // omit the type from all but the first operand. If the instruction has |
| 1307 | // different type operands (for example br), then they are all printed. |
| 1308 | bool PrintAllTypes = false; |
| 1309 | const Type *TheType = Operand->getType(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1310 | |
Chris Lattner | cfdd148 | 2004-03-12 05:53:14 +0000 | [diff] [blame] | 1311 | // Shift Left & Right print both types even for Ubyte LHS, and select prints |
| 1312 | // types even if all operands are bools. |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1313 | if (isa<ShiftInst>(I) || isa<SelectInst>(I) || isa<StoreInst>(I) || |
| 1314 | isa<ShuffleVectorInst>(I)) { |
Chris Lattner | ffd9bf4 | 2003-04-16 20:20:02 +0000 | [diff] [blame] | 1315 | PrintAllTypes = true; |
| 1316 | } else { |
| 1317 | for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) { |
| 1318 | Operand = I.getOperand(i); |
| 1319 | if (Operand->getType() != TheType) { |
| 1320 | PrintAllTypes = true; // We have differing types! Print them all! |
| 1321 | break; |
| 1322 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1323 | } |
| 1324 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1325 | |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1326 | if (!PrintAllTypes) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1327 | Out << ' '; |
Chris Lattner | ffd9bf4 | 2003-04-16 20:20:02 +0000 | [diff] [blame] | 1328 | printType(TheType); |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1329 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1330 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1331 | for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1332 | if (i) Out << ','; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1333 | writeOperand(I.getOperand(i), PrintAllTypes); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1334 | } |
| 1335 | } |
| 1336 | |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1337 | printInfoComment(I); |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1338 | Out << "\n"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | |
| 1342 | //===----------------------------------------------------------------------===// |
| 1343 | // External Interface declarations |
| 1344 | //===----------------------------------------------------------------------===// |
| 1345 | |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1346 | void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1347 | SlotMachine SlotTable(this); |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1348 | AssemblyWriter W(o, SlotTable, this, AAW); |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1349 | W.write(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1352 | void GlobalVariable::print(std::ostream &o) const { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1353 | SlotMachine SlotTable(getParent()); |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1354 | AssemblyWriter W(o, SlotTable, getParent(), 0); |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1355 | W.write(this); |
Chris Lattner | b0e4523 | 2001-09-10 20:08:19 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1358 | void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1359 | SlotMachine SlotTable(getParent()); |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1360 | AssemblyWriter W(o, SlotTable, getParent(), AAW); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1361 | |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1362 | W.write(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Chris Lattner | cc041ba | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 1365 | void InlineAsm::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const { |
Chris Lattner | 80cd115 | 2006-01-25 22:26:05 +0000 | [diff] [blame] | 1366 | WriteAsOperand(o, this, true, true, 0); |
Chris Lattner | cc041ba | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1369 | void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1370 | SlotMachine SlotTable(getParent()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1371 | AssemblyWriter W(o, SlotTable, |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1372 | getParent() ? getParent()->getParent() : 0, AAW); |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1373 | W.write(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1376 | void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const { |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1377 | const Function *F = getParent() ? getParent()->getParent() : 0; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1378 | SlotMachine SlotTable(F); |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1379 | AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1380 | |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1381 | W.write(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1382 | } |
Chris Lattner | da1fbcc | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 1383 | |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1384 | void Constant::print(std::ostream &o) const { |
| 1385 | if (this == 0) { o << "<null> constant value\n"; return; } |
Chris Lattner | 3bc06b3 | 2002-09-10 15:53:49 +0000 | [diff] [blame] | 1386 | |
Misha Brukman | 40c732c | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 1387 | o << ' ' << getType()->getDescription() << ' '; |
Evan Cheng | a4ffcc2 | 2006-03-01 22:17:00 +0000 | [diff] [blame] | 1388 | |
| 1389 | std::map<const Type *, std::string> TypeTable; |
| 1390 | WriteConstantInt(o, this, false, TypeTable, 0); |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1393 | void Type::print(std::ostream &o) const { |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1394 | if (this == 0) |
| 1395 | o << "<null Type>"; |
| 1396 | else |
| 1397 | o << getDescription(); |
| 1398 | } |
| 1399 | |
Chris Lattner | 73e2142 | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 1400 | void Argument::print(std::ostream &o) const { |
Chris Lattner | 144d9ba | 2004-07-13 23:14:34 +0000 | [diff] [blame] | 1401 | WriteAsOperand(o, this, true, true, |
| 1402 | getParent() ? getParent()->getParent() : 0); |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
Reid Spencer | fa452c0 | 2004-05-25 18:14:38 +0000 | [diff] [blame] | 1405 | // Value::dump - allow easy printing of Values from the debugger. |
| 1406 | // Located here because so much of the needed functionality is here. |
Bill Wendling | 8f48766 | 2006-11-28 02:09:03 +0000 | [diff] [blame] | 1407 | void Value::dump() const { print(std::cerr); llvm_cerr << '\n'; } |
Reid Spencer | fa452c0 | 2004-05-25 18:14:38 +0000 | [diff] [blame] | 1408 | |
| 1409 | // Type::dump - allow easy printing of Values from the debugger. |
| 1410 | // Located here because so much of the needed functionality is here. |
Bill Wendling | 8f48766 | 2006-11-28 02:09:03 +0000 | [diff] [blame] | 1411 | void Type::dump() const { print(std::cerr); llvm_cerr << '\n'; } |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1412 | |
| 1413 | //===----------------------------------------------------------------------===// |
| 1414 | // CachedWriter Class Implementation |
| 1415 | //===----------------------------------------------------------------------===// |
| 1416 | |
Chris Lattner | da1fbcc | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 1417 | void CachedWriter::setModule(const Module *M) { |
| 1418 | delete SC; delete AW; |
| 1419 | if (M) { |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1420 | SC = new SlotMachine(M); |
Misha Brukman | 40c732c | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 1421 | AW = new AssemblyWriter(Out, *SC, M, 0); |
Chris Lattner | da1fbcc | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 1422 | } else { |
| 1423 | SC = 0; AW = 0; |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | CachedWriter::~CachedWriter() { |
| 1428 | delete AW; |
| 1429 | delete SC; |
| 1430 | } |
| 1431 | |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 1432 | CachedWriter &CachedWriter::operator<<(const Value &V) { |
Chris Lattner | da1fbcc | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 1433 | assert(AW && SC && "CachedWriter does not have a current module!"); |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 1434 | if (const Instruction *I = dyn_cast<Instruction>(&V)) |
Chris Lattner | fae098a | 2004-06-26 19:40:40 +0000 | [diff] [blame] | 1435 | AW->write(I); |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 1436 | else if (const BasicBlock *BB = dyn_cast<BasicBlock>(&V)) |
Chris Lattner | fae098a | 2004-06-26 19:40:40 +0000 | [diff] [blame] | 1437 | AW->write(BB); |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 1438 | else if (const Function *F = dyn_cast<Function>(&V)) |
Chris Lattner | fae098a | 2004-06-26 19:40:40 +0000 | [diff] [blame] | 1439 | AW->write(F); |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 1440 | else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(&V)) |
Chris Lattner | fae098a | 2004-06-26 19:40:40 +0000 | [diff] [blame] | 1441 | AW->write(GV); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1442 | else |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 1443 | AW->writeOperand(&V, true, true); |
Chris Lattner | da1fbcc | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 1444 | return *this; |
| 1445 | } |
Misha Brukman | 5cf1acf | 2004-04-28 15:31:21 +0000 | [diff] [blame] | 1446 | |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 1447 | CachedWriter& CachedWriter::operator<<(const Type &Ty) { |
Misha Brukman | 5cf1acf | 2004-04-28 15:31:21 +0000 | [diff] [blame] | 1448 | if (SymbolicTypes) { |
| 1449 | const Module *M = AW->getModule(); |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 1450 | if (M) WriteTypeSymbolic(Out, &Ty, M); |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1451 | } else { |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 1452 | AW->write(&Ty); |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1453 | } |
| 1454 | return *this; |
Misha Brukman | 5cf1acf | 2004-04-28 15:31:21 +0000 | [diff] [blame] | 1455 | } |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 1456 | |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1457 | //===----------------------------------------------------------------------===// |
| 1458 | //===-- SlotMachine Implementation |
| 1459 | //===----------------------------------------------------------------------===// |
| 1460 | |
| 1461 | #if 0 |
Bill Wendling | 8f48766 | 2006-11-28 02:09:03 +0000 | [diff] [blame] | 1462 | #define SC_DEBUG(X) llvm_cerr << X |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1463 | #else |
| 1464 | #define SC_DEBUG(X) |
| 1465 | #endif |
| 1466 | |
| 1467 | // Module level constructor. Causes the contents of the Module (sans functions) |
| 1468 | // to be added to the slot table. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1469 | SlotMachine::SlotMachine(const Module *M) |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1470 | : TheModule(M) ///< Saved for lazy initialization. |
| 1471 | , TheFunction(0) |
Reid Spencer | 28531c7 | 2004-08-16 07:46:33 +0000 | [diff] [blame] | 1472 | , FunctionProcessed(false) |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1473 | , mMap() |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1474 | , mTypes() |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1475 | , fMap() |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1476 | , fTypes() |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1477 | { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | // Function level constructor. Causes the contents of the Module and the one |
| 1481 | // function provided to be added to the slot table. |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1482 | SlotMachine::SlotMachine(const Function *F) |
| 1483 | : TheModule(F ? F->getParent() : 0) ///< Saved for lazy initialization |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1484 | , TheFunction(F) ///< Saved for lazy initialization |
Reid Spencer | 28531c7 | 2004-08-16 07:46:33 +0000 | [diff] [blame] | 1485 | , FunctionProcessed(false) |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1486 | , mMap() |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1487 | , mTypes() |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1488 | , fMap() |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1489 | , fTypes() |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1490 | { |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | inline void SlotMachine::initialize(void) { |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1494 | if (TheModule) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1495 | processModule(); |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1496 | TheModule = 0; ///< Prevent re-processing next time we're called. |
| 1497 | } |
Chris Lattner | de891a6 | 2006-12-06 05:27:40 +0000 | [diff] [blame^] | 1498 | if (TheFunction && !FunctionProcessed) |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1499 | processFunction(); |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | // Iterate through all the global variables, functions, and global |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1503 | // variable initializers and create slots for them. |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1504 | void SlotMachine::processModule() { |
| 1505 | SC_DEBUG("begin processModule!\n"); |
| 1506 | |
| 1507 | // Add all of the global variables to the value table... |
Chris Lattner | d6d826c | 2006-12-06 04:41:52 +0000 | [diff] [blame] | 1508 | for (Module::const_global_iterator I = TheModule->global_begin(), |
| 1509 | E = TheModule->global_end(); I != E; ++I) |
Chris Lattner | de891a6 | 2006-12-06 05:27:40 +0000 | [diff] [blame^] | 1510 | getOrCreateSlot(I); |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1511 | |
| 1512 | // Add all the functions to the table |
| 1513 | for (Module::const_iterator I = TheModule->begin(), E = TheModule->end(); |
| 1514 | I != E; ++I) |
Chris Lattner | de891a6 | 2006-12-06 05:27:40 +0000 | [diff] [blame^] | 1515 | getOrCreateSlot(I); |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1516 | |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1517 | SC_DEBUG("end processModule!\n"); |
| 1518 | } |
| 1519 | |
| 1520 | |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1521 | // Process the arguments, basic blocks, and instructions of a function. |
| 1522 | void SlotMachine::processFunction() { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1523 | SC_DEBUG("begin processFunction!\n"); |
| 1524 | |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1525 | // Add all the function arguments |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1526 | for(Function::const_arg_iterator AI = TheFunction->arg_begin(), |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 1527 | AE = TheFunction->arg_end(); AI != AE; ++AI) |
Chris Lattner | de891a6 | 2006-12-06 05:27:40 +0000 | [diff] [blame^] | 1528 | getOrCreateSlot(AI); |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1529 | |
| 1530 | SC_DEBUG("Inserting Instructions:\n"); |
| 1531 | |
| 1532 | // Add all of the basic blocks and instructions |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1533 | for (Function::const_iterator BB = TheFunction->begin(), |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1534 | E = TheFunction->end(); BB != E; ++BB) { |
Chris Lattner | de891a6 | 2006-12-06 05:27:40 +0000 | [diff] [blame^] | 1535 | getOrCreateSlot(BB); |
| 1536 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) |
| 1537 | getOrCreateSlot(I); |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
Reid Spencer | 28531c7 | 2004-08-16 07:46:33 +0000 | [diff] [blame] | 1540 | FunctionProcessed = true; |
| 1541 | |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1542 | SC_DEBUG("end processFunction!\n"); |
| 1543 | } |
| 1544 | |
Chris Lattner | de891a6 | 2006-12-06 05:27:40 +0000 | [diff] [blame^] | 1545 | /// Clean up after incorporating a function. This is the only way to get out of |
| 1546 | /// the function incorporation state that affects the |
| 1547 | /// getSlot/getOrCreateSlot lock. Function incorporation state is indicated |
| 1548 | /// by TheFunction != 0. |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1549 | void SlotMachine::purgeFunction() { |
| 1550 | SC_DEBUG("begin purgeFunction!\n"); |
| 1551 | fMap.clear(); // Simply discard the function level map |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1552 | fTypes.clear(); |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1553 | TheFunction = 0; |
Reid Spencer | 28531c7 | 2004-08-16 07:46:33 +0000 | [diff] [blame] | 1554 | FunctionProcessed = false; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1555 | SC_DEBUG("end purgeFunction!\n"); |
| 1556 | } |
| 1557 | |
| 1558 | /// Get the slot number for a value. This function will assert if you |
Chris Lattner | de891a6 | 2006-12-06 05:27:40 +0000 | [diff] [blame^] | 1559 | /// ask for a Value that hasn't previously been inserted with getOrCreateSlot. |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1560 | /// Types are forbidden because Type does not inherit from Value (any more). |
Chris Lattner | 6956645 | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 1561 | int SlotMachine::getSlot(const Value *V) { |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1562 | assert(V && "Can't get slot for null Value"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1563 | assert(!isa<Constant>(V) || isa<GlobalValue>(V) && |
| 1564 | "Can't insert a non-GlobalValue Constant into SlotMachine"); |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1565 | |
| 1566 | // Check for uninitialized state and do lazy initialization |
| 1567 | this->initialize(); |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1568 | |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1569 | // Get the type of the value |
| 1570 | const Type* VTy = V->getType(); |
| 1571 | |
| 1572 | // Find the type plane in the module map |
| 1573 | TypedPlanes::const_iterator MI = mMap.find(VTy); |
| 1574 | |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1575 | if (TheFunction) { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1576 | // Lookup the type in the function map too |
| 1577 | TypedPlanes::const_iterator FI = fMap.find(VTy); |
| 1578 | // If there is a corresponding type plane in the function map |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1579 | if (FI != fMap.end()) { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1580 | // Lookup the Value in the function map |
| 1581 | ValueMap::const_iterator FVI = FI->second.map.find(V); |
| 1582 | // If the value doesn't exist in the function map |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1583 | if (FVI == FI->second.map.end()) { |
Chris Lattner | e9e326e | 2004-06-09 22:22:10 +0000 | [diff] [blame] | 1584 | // Look up the value in the module map. |
| 1585 | if (MI == mMap.end()) return -1; |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1586 | ValueMap::const_iterator MVI = MI->second.map.find(V); |
| 1587 | // If we didn't find it, it wasn't inserted |
Chris Lattner | 6956645 | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 1588 | if (MVI == MI->second.map.end()) return -1; |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1589 | assert(MVI != MI->second.map.end() && "Value not found"); |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1590 | // We found it only at the module level |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1591 | return MVI->second; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1592 | |
| 1593 | // else the value exists in the function map |
| 1594 | } else { |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1595 | // Return the slot number as the module's contribution to |
| 1596 | // the type plane plus the index in the function's contribution |
| 1597 | // to the type plane. |
Chris Lattner | d1cd328 | 2004-06-15 21:07:32 +0000 | [diff] [blame] | 1598 | if (MI != mMap.end()) |
| 1599 | return MI->second.next_slot + FVI->second; |
| 1600 | else |
| 1601 | return FVI->second; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1602 | } |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1603 | } |
| 1604 | } |
| 1605 | |
Reid Spencer | fc621e2 | 2004-06-09 15:26:53 +0000 | [diff] [blame] | 1606 | // N.B. Can get here only if either !TheFunction or the function doesn't |
| 1607 | // have a corresponding type plane for the Value |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1608 | |
| 1609 | // Make sure the type plane exists |
Chris Lattner | 6956645 | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 1610 | if (MI == mMap.end()) return -1; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1611 | // Lookup the value in the module's map |
| 1612 | ValueMap::const_iterator MVI = MI->second.map.find(V); |
| 1613 | // Make sure we found it. |
Chris Lattner | 6956645 | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 1614 | if (MVI == MI->second.map.end()) return -1; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1615 | // Return it. |
| 1616 | return MVI->second; |
| 1617 | } |
| 1618 | |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1619 | /// Get the slot number for a value. This function will assert if you |
Chris Lattner | de891a6 | 2006-12-06 05:27:40 +0000 | [diff] [blame^] | 1620 | /// ask for a Value that hasn't previously been inserted with getOrCreateSlot. |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1621 | /// Types are forbidden because Type does not inherit from Value (any more). |
| 1622 | int SlotMachine::getSlot(const Type *Ty) { |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1623 | assert(Ty && "Can't get slot for null Type"); |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1624 | |
| 1625 | // Check for uninitialized state and do lazy initialization |
| 1626 | this->initialize(); |
| 1627 | |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1628 | if (TheFunction) { |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1629 | // Lookup the Type in the function map |
| 1630 | TypeMap::const_iterator FTI = fTypes.map.find(Ty); |
| 1631 | // If the Type doesn't exist in the function map |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1632 | if (FTI == fTypes.map.end()) { |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1633 | TypeMap::const_iterator MTI = mTypes.map.find(Ty); |
| 1634 | // If we didn't find it, it wasn't inserted |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1635 | if (MTI == mTypes.map.end()) |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1636 | return -1; |
| 1637 | // We found it only at the module level |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1638 | return MTI->second; |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1639 | |
| 1640 | // else the value exists in the function map |
| 1641 | } else { |
| 1642 | // Return the slot number as the module's contribution to |
| 1643 | // the type plane plus the index in the function's contribution |
| 1644 | // to the type plane. |
| 1645 | return mTypes.next_slot + FTI->second; |
| 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | // N.B. Can get here only if either !TheFunction |
| 1650 | |
| 1651 | // Lookup the value in the module's map |
| 1652 | TypeMap::const_iterator MTI = mTypes.map.find(Ty); |
| 1653 | // Make sure we found it. |
| 1654 | if (MTI == mTypes.map.end()) return -1; |
| 1655 | // Return it. |
| 1656 | return MTI->second; |
| 1657 | } |
| 1658 | |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1659 | // Create a new slot, or return the existing slot if it is already |
| 1660 | // inserted. Note that the logic here parallels getSlot but instead |
| 1661 | // of asserting when the Value* isn't found, it inserts the value. |
Chris Lattner | de891a6 | 2006-12-06 05:27:40 +0000 | [diff] [blame^] | 1662 | unsigned SlotMachine::getOrCreateSlot(const Value *V) { |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1663 | assert(V && "Can't insert a null Value to SlotMachine"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1664 | assert(!isa<Constant>(V) || isa<GlobalValue>(V) && |
| 1665 | "Can't insert a non-GlobalValue Constant into SlotMachine"); |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1666 | |
| 1667 | const Type* VTy = V->getType(); |
| 1668 | |
| 1669 | // Just ignore void typed things |
| 1670 | if (VTy == Type::VoidTy) return 0; // FIXME: Wrong return value! |
| 1671 | |
| 1672 | // Look up the type plane for the Value's type from the module map |
| 1673 | TypedPlanes::const_iterator MI = mMap.find(VTy); |
| 1674 | |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1675 | if (TheFunction) { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1676 | // Get the type plane for the Value's type from the function map |
| 1677 | TypedPlanes::const_iterator FI = fMap.find(VTy); |
| 1678 | // If there is a corresponding type plane in the function map |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1679 | if (FI != fMap.end()) { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1680 | // Lookup the Value in the function map |
| 1681 | ValueMap::const_iterator FVI = FI->second.map.find(V); |
| 1682 | // If the value doesn't exist in the function map |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1683 | if (FVI == FI->second.map.end()) { |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1684 | // If there is no corresponding type plane in the module map |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1685 | if (MI == mMap.end()) |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1686 | return insertValue(V); |
| 1687 | // Look up the value in the module map |
| 1688 | ValueMap::const_iterator MVI = MI->second.map.find(V); |
| 1689 | // If we didn't find it, it wasn't inserted |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1690 | if (MVI == MI->second.map.end()) |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1691 | return insertValue(V); |
| 1692 | else |
| 1693 | // We found it only at the module level |
| 1694 | return MVI->second; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1695 | |
| 1696 | // else the value exists in the function map |
| 1697 | } else { |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1698 | if (MI == mMap.end()) |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1699 | return FVI->second; |
| 1700 | else |
| 1701 | // Return the slot number as the module's contribution to |
| 1702 | // the type plane plus the index in the function's contribution |
| 1703 | // to the type plane. |
| 1704 | return MI->second.next_slot + FVI->second; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | // else there is not a corresponding type plane in the function map |
| 1708 | } else { |
| 1709 | // If the type plane doesn't exists at the module level |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1710 | if (MI == mMap.end()) { |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1711 | return insertValue(V); |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1712 | // else type plane exists at the module level, examine it |
| 1713 | } else { |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1714 | // Look up the value in the module's map |
| 1715 | ValueMap::const_iterator MVI = MI->second.map.find(V); |
| 1716 | // If we didn't find it there either |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1717 | if (MVI == MI->second.map.end()) |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1718 | // Return the slot number as the module's contribution to |
| 1719 | // the type plane plus the index of the function map insertion. |
| 1720 | return MI->second.next_slot + insertValue(V); |
| 1721 | else |
| 1722 | return MVI->second; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1723 | } |
| 1724 | } |
| 1725 | } |
| 1726 | |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1727 | // N.B. Can only get here if !TheFunction |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1728 | |
| 1729 | // If the module map's type plane is not for the Value's type |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1730 | if (MI != mMap.end()) { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1731 | // Lookup the value in the module's map |
| 1732 | ValueMap::const_iterator MVI = MI->second.map.find(V); |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1733 | if (MVI != MI->second.map.end()) |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1734 | return MVI->second; |
| 1735 | } |
| 1736 | |
| 1737 | return insertValue(V); |
| 1738 | } |
| 1739 | |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1740 | // Create a new slot, or return the existing slot if it is already |
| 1741 | // inserted. Note that the logic here parallels getSlot but instead |
| 1742 | // of asserting when the Value* isn't found, it inserts the value. |
Chris Lattner | de891a6 | 2006-12-06 05:27:40 +0000 | [diff] [blame^] | 1743 | unsigned SlotMachine::getOrCreateSlot(const Type *Ty) { |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1744 | assert(Ty && "Can't insert a null Type to SlotMachine"); |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1745 | |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1746 | if (TheFunction) { |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1747 | // Lookup the Type in the function map |
| 1748 | TypeMap::const_iterator FTI = fTypes.map.find(Ty); |
| 1749 | // If the type doesn't exist in the function map |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1750 | if (FTI == fTypes.map.end()) { |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1751 | // Look up the type in the module map |
| 1752 | TypeMap::const_iterator MTI = mTypes.map.find(Ty); |
| 1753 | // If we didn't find it, it wasn't inserted |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1754 | if (MTI == mTypes.map.end()) |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1755 | return insertValue(Ty); |
| 1756 | else |
| 1757 | // We found it only at the module level |
| 1758 | return MTI->second; |
| 1759 | |
| 1760 | // else the value exists in the function map |
| 1761 | } else { |
| 1762 | // Return the slot number as the module's contribution to |
| 1763 | // the type plane plus the index in the function's contribution |
| 1764 | // to the type plane. |
| 1765 | return mTypes.next_slot + FTI->second; |
| 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | // N.B. Can only get here if !TheFunction |
| 1770 | |
| 1771 | // Lookup the type in the module's map |
| 1772 | TypeMap::const_iterator MTI = mTypes.map.find(Ty); |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1773 | if (MTI != mTypes.map.end()) |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1774 | return MTI->second; |
| 1775 | |
| 1776 | return insertValue(Ty); |
| 1777 | } |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1778 | |
| 1779 | // Low level insert function. Minimal checking is done. This |
Chris Lattner | de891a6 | 2006-12-06 05:27:40 +0000 | [diff] [blame^] | 1780 | // function is just for the convenience of getOrCreateSlot (above). |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1781 | unsigned SlotMachine::insertValue(const Value *V) { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1782 | assert(V && "Can't insert a null Value into SlotMachine!"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1783 | assert(!isa<Constant>(V) || isa<GlobalValue>(V) && |
| 1784 | "Can't insert a non-GlobalValue Constant into SlotMachine"); |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1785 | |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1786 | // If this value does not contribute to a plane (is void) |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1787 | // or if the value already has a name then ignore it. |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1788 | if (V->getType() == Type::VoidTy || V->hasName()) { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1789 | SC_DEBUG("ignored value " << *V << "\n"); |
| 1790 | return 0; // FIXME: Wrong return value |
| 1791 | } |
| 1792 | |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1793 | const Type *VTy = V->getType(); |
| 1794 | unsigned DestSlot = 0; |
| 1795 | |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1796 | if (TheFunction) { |
| 1797 | TypedPlanes::iterator I = fMap.find(VTy); |
| 1798 | if (I == fMap.end()) |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1799 | I = fMap.insert(std::make_pair(VTy,ValuePlane())).first; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1800 | DestSlot = I->second.map[V] = I->second.next_slot++; |
| 1801 | } else { |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1802 | TypedPlanes::iterator I = mMap.find(VTy); |
| 1803 | if (I == mMap.end()) |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1804 | I = mMap.insert(std::make_pair(VTy,ValuePlane())).first; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1805 | DestSlot = I->second.map[V] = I->second.next_slot++; |
| 1806 | } |
| 1807 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1808 | SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" << |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 1809 | DestSlot << " ["); |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1810 | // G = Global, C = Constant, T = Type, F = Function, o = other |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1811 | SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : (isa<Function>(V) ? 'F' : |
Reid Spencer | 7970396 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1812 | (isa<Constant>(V) ? 'C' : 'o')))); |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1813 | SC_DEBUG("]\n"); |
| 1814 | return DestSlot; |
| 1815 | } |
| 1816 | |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1817 | // Low level insert function. Minimal checking is done. This |
Chris Lattner | de891a6 | 2006-12-06 05:27:40 +0000 | [diff] [blame^] | 1818 | // function is just for the convenience of getOrCreateSlot (above). |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1819 | unsigned SlotMachine::insertValue(const Type *Ty) { |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1820 | assert(Ty && "Can't insert a null Type into SlotMachine!"); |
| 1821 | |
| 1822 | unsigned DestSlot = 0; |
| 1823 | |
Chris Lattner | c96ce89 | 2006-12-06 05:12:21 +0000 | [diff] [blame] | 1824 | if (TheFunction) { |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1825 | DestSlot = fTypes.map[Ty] = fTypes.next_slot++; |
| 1826 | } else { |
| 1827 | DestSlot = fTypes.map[Ty] = fTypes.next_slot++; |
| 1828 | } |
| 1829 | SC_DEBUG(" Inserting type [" << DestSlot << "] = " << Ty << "\n"); |
| 1830 | return DestSlot; |
| 1831 | } |
| 1832 | |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 1833 | // vim: sw=2 |