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