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