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