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