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