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