Chris Lattner | f7e7948 | 2002-04-07 22:31:46 +0000 | [diff] [blame] | 1 | //===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2 | // |
| 3 | // This library implements the functionality defined in llvm/Assembly/Writer.h |
| 4 | // |
Chris Lattner | 189088e | 2002-04-12 18:21:53 +0000 | [diff] [blame] | 5 | // Note that these routines must be extremely tolerant of various errors in the |
| 6 | // LLVM code, because of of the primary uses of it is for debugging |
| 7 | // transformations. |
| 8 | // |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // TODO: print out the type name instead of the full type if a particular type |
Chris Lattner | 189088e | 2002-04-12 18:21:53 +0000 | [diff] [blame] | 10 | // is in the symbol table... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 7db7958 | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 14 | #include "llvm/Assembly/CachedWriter.h" |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 15 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 6915f8f | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 16 | #include "llvm/SlotCalculator.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 18 | #include "llvm/Function.h" |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 19 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 20 | #include "llvm/BasicBlock.h" |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 21 | #include "llvm/ConstantVals.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 22 | #include "llvm/iMemory.h" |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 23 | #include "llvm/iTerminators.h" |
Chris Lattner | fb5ae02 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 24 | #include "llvm/iPHINode.h" |
| 25 | #include "llvm/iOther.h" |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 26 | #include "llvm/SymbolTable.h" |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 27 | #include "llvm/Argument.h" |
Chris Lattner | 5de2204 | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 28 | #include "Support/StringExtras.h" |
| 29 | #include "Support/STLExtras.h" |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 30 | #include <algorithm> |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 31 | #include <map> |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 32 | using std::string; |
| 33 | using std::map; |
| 34 | using std::vector; |
| 35 | using std::ostream; |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 36 | |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame^] | 37 | static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName, |
| 38 | map<const Type *, string> &TypeTable, |
| 39 | SlotCalculator *Table); |
| 40 | |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 41 | static const Module *getModuleFromVal(const Value *V) { |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 42 | if (const Argument *MA = dyn_cast<const Argument>(V)) |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 43 | return MA->getParent() ? MA->getParent()->getParent() : 0; |
| 44 | else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) |
| 45 | return BB->getParent() ? BB->getParent()->getParent() : 0; |
| 46 | else if (const Instruction *I = dyn_cast<const Instruction>(V)) { |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 47 | const Function *M = I->getParent() ? I->getParent()->getParent() : 0; |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 48 | return M ? M->getParent() : 0; |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 49 | } else if (const GlobalValue *GV = dyn_cast<const GlobalValue>(V)) |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 50 | return GV->getParent(); |
| 51 | else if (const Module *Mod = dyn_cast<const Module>(V)) |
| 52 | return Mod; |
| 53 | return 0; |
| 54 | } |
| 55 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 56 | static SlotCalculator *createSlotCalculator(const Value *V) { |
| 57 | assert(!isa<Type>(V) && "Can't create an SC for a type!"); |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 58 | if (const Argument *FA = dyn_cast<const Argument>(V)) { |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 59 | return new SlotCalculator(FA->getParent(), true); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 60 | } else if (const Instruction *I = dyn_cast<const Instruction>(V)) { |
| 61 | return new SlotCalculator(I->getParent()->getParent(), true); |
| 62 | } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) { |
| 63 | return new SlotCalculator(BB->getParent(), true); |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 64 | } else if (const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V)){ |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 65 | return new SlotCalculator(GV->getParent(), true); |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 66 | } else if (const Function *Func = dyn_cast<const Function>(V)) { |
| 67 | return new SlotCalculator(Func, true); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 68 | } else if (const Module *Mod = dyn_cast<const Module>(V)) { |
| 69 | return new SlotCalculator(Mod, true); |
| 70 | } |
| 71 | return 0; |
| 72 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 73 | |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 74 | |
| 75 | // If the module has a symbol table, take all global types and stuff their |
| 76 | // names into the TypeNames map. |
| 77 | // |
| 78 | static void fillTypeNameTable(const Module *M, |
| 79 | map<const Type *, string> &TypeNames) { |
| 80 | if (M && M->hasSymbolTable()) { |
| 81 | const SymbolTable *ST = M->getSymbolTable(); |
| 82 | SymbolTable::const_iterator PI = ST->find(Type::TypeTy); |
| 83 | if (PI != ST->end()) { |
| 84 | SymbolTable::type_const_iterator I = PI->second.begin(); |
| 85 | for (; I != PI->second.end(); ++I) { |
| 86 | // As a heuristic, don't insert pointer to primitive types, because |
| 87 | // they are used too often to have a single useful name. |
| 88 | // |
| 89 | const Type *Ty = cast<const Type>(I->second); |
| 90 | if (!isa<PointerType>(Ty) || |
Chris Lattner | 2413b16 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 91 | !cast<PointerType>(Ty)->getElementType()->isPrimitiveType()) |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 92 | TypeNames.insert(std::make_pair(Ty, "%"+I->first)); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | |
| 99 | |
| 100 | static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack, |
| 101 | map<const Type *, string> &TypeNames) { |
| 102 | if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case |
| 103 | |
| 104 | // Check to see if the type is named. |
| 105 | map<const Type *, string>::iterator I = TypeNames.find(Ty); |
| 106 | if (I != TypeNames.end()) return I->second; |
| 107 | |
| 108 | // Check to see if the Type is already on the stack... |
| 109 | unsigned Slot = 0, CurSize = TypeStack.size(); |
| 110 | while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type |
| 111 | |
| 112 | // This is another base case for the recursion. In this case, we know |
| 113 | // that we have looped back to a type that we have previously visited. |
| 114 | // Generate the appropriate upreference to handle this. |
| 115 | // |
| 116 | if (Slot < CurSize) |
| 117 | return "\\" + utostr(CurSize-Slot); // Here's the upreference |
| 118 | |
| 119 | TypeStack.push_back(Ty); // Recursive case: Add us to the stack.. |
| 120 | |
| 121 | string Result; |
| 122 | switch (Ty->getPrimitiveID()) { |
Chris Lattner | 91db582 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 123 | case Type::FunctionTyID: { |
Chris Lattner | d816b53 | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 124 | const FunctionType *FTy = cast<const FunctionType>(Ty); |
| 125 | Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " ("; |
Chris Lattner | 91db582 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 126 | for (FunctionType::ParamTypes::const_iterator |
Chris Lattner | d816b53 | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 127 | I = FTy->getParamTypes().begin(), |
| 128 | E = FTy->getParamTypes().end(); I != E; ++I) { |
| 129 | if (I != FTy->getParamTypes().begin()) |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 130 | Result += ", "; |
| 131 | Result += calcTypeName(*I, TypeStack, TypeNames); |
| 132 | } |
Chris Lattner | d816b53 | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 133 | if (FTy->isVarArg()) { |
| 134 | if (!FTy->getParamTypes().empty()) Result += ", "; |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 135 | Result += "..."; |
| 136 | } |
| 137 | Result += ")"; |
| 138 | break; |
| 139 | } |
| 140 | case Type::StructTyID: { |
| 141 | const StructType *STy = cast<const StructType>(Ty); |
| 142 | Result = "{ "; |
| 143 | for (StructType::ElementTypes::const_iterator |
| 144 | I = STy->getElementTypes().begin(), |
| 145 | E = STy->getElementTypes().end(); I != E; ++I) { |
| 146 | if (I != STy->getElementTypes().begin()) |
| 147 | Result += ", "; |
| 148 | Result += calcTypeName(*I, TypeStack, TypeNames); |
| 149 | } |
| 150 | Result += " }"; |
| 151 | break; |
| 152 | } |
| 153 | case Type::PointerTyID: |
Chris Lattner | 2413b16 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 154 | Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(), |
Chris Lattner | 189088e | 2002-04-12 18:21:53 +0000 | [diff] [blame] | 155 | TypeStack, TypeNames) + "*"; |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 156 | break; |
| 157 | case Type::ArrayTyID: { |
| 158 | const ArrayType *ATy = cast<const ArrayType>(Ty); |
Chris Lattner | 8a939c9 | 2002-04-13 21:11:04 +0000 | [diff] [blame] | 159 | Result = "[" + utostr(ATy->getNumElements()) + " x "; |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 160 | Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]"; |
| 161 | break; |
| 162 | } |
| 163 | default: |
| 164 | assert(0 && "Unhandled case in getTypeProps!"); |
| 165 | Result = "<error>"; |
| 166 | } |
| 167 | |
| 168 | TypeStack.pop_back(); // Remove self from stack... |
| 169 | return Result; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | // printTypeInt - The internal guts of printing out a type that has a |
| 174 | // potentially named portion. |
| 175 | // |
| 176 | static ostream &printTypeInt(ostream &Out, const Type *Ty, |
| 177 | map<const Type *, string> &TypeNames) { |
| 178 | // Primitive types always print out their description, regardless of whether |
| 179 | // they have been named or not. |
| 180 | // |
| 181 | if (Ty->isPrimitiveType()) return Out << Ty->getDescription(); |
| 182 | |
| 183 | // Check to see if the type is named. |
| 184 | map<const Type *, string>::iterator I = TypeNames.find(Ty); |
| 185 | if (I != TypeNames.end()) return Out << I->second; |
| 186 | |
| 187 | // Otherwise we have a type that has not been named but is a derived type. |
| 188 | // Carefully recurse the type hierarchy to print out any contained symbolic |
| 189 | // names. |
| 190 | // |
| 191 | vector<const Type *> TypeStack; |
| 192 | string TypeName = calcTypeName(Ty, TypeStack, TypeNames); |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 193 | 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] | 194 | return Out << TypeName; |
| 195 | } |
| 196 | |
Chris Lattner | 34b9518 | 2001-10-31 04:33:19 +0000 | [diff] [blame] | 197 | |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 198 | // WriteTypeSymbolic - This attempts to write the specified type as a symbolic |
| 199 | // type, iff there is an entry in the modules symbol table for the specified |
| 200 | // type or one of it's component types. This is slower than a simple x << Type; |
| 201 | // |
| 202 | ostream &WriteTypeSymbolic(ostream &Out, const Type *Ty, const Module *M) { |
| 203 | Out << " "; |
| 204 | |
| 205 | // If they want us to print out a type, attempt to make it symbolic if there |
| 206 | // is a symbol table in the module... |
| 207 | if (M && M->hasSymbolTable()) { |
| 208 | map<const Type *, string> TypeNames; |
| 209 | fillTypeNameTable(M, TypeNames); |
| 210 | |
Chris Lattner | 72f866e | 2001-10-29 16:40:32 +0000 | [diff] [blame] | 211 | return printTypeInt(Out, Ty, TypeNames); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 212 | } else { |
Chris Lattner | 72f866e | 2001-10-29 16:40:32 +0000 | [diff] [blame] | 213 | return Out << Ty->getDescription(); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame^] | 217 | static void WriteConstantInt(ostream &Out, const Constant *CV, bool PrintName, |
| 218 | map<const Type *, string> &TypeTable, |
| 219 | SlotCalculator *Table) { |
| 220 | if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) { |
| 221 | const Type *SubType = CA->getType()->getElementType(); |
| 222 | if (SubType == Type::SByteTy) { |
| 223 | Out << CV->getStrValue(); // Output string format if possible... |
| 224 | } else { |
| 225 | Out << "["; |
| 226 | if (CA->getNumOperands()) { |
| 227 | Out << " "; |
| 228 | printTypeInt(Out, SubType, TypeTable); |
| 229 | WriteAsOperandInternal(Out, CA->getOperand(0), |
| 230 | PrintName, TypeTable, Table); |
| 231 | for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) { |
| 232 | Out << ", "; |
| 233 | printTypeInt(Out, SubType, TypeTable); |
| 234 | WriteAsOperandInternal(Out, CA->getOperand(i), PrintName, |
| 235 | TypeTable, Table); |
| 236 | } |
| 237 | } |
| 238 | Out << " ]"; |
| 239 | } |
| 240 | } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) { |
| 241 | Out << "{"; |
| 242 | if (CS->getNumOperands()) { |
| 243 | Out << " "; |
| 244 | printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable); |
| 245 | |
| 246 | WriteAsOperandInternal(Out, CS->getOperand(0), |
| 247 | PrintName, TypeTable, Table); |
| 248 | |
| 249 | for (unsigned i = 1; i < CS->getNumOperands(); i++) { |
| 250 | Out << ", "; |
| 251 | printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable); |
| 252 | |
| 253 | WriteAsOperandInternal(Out, CS->getOperand(i), |
| 254 | PrintName, TypeTable, Table); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | Out << " }"; |
| 259 | } else if (isa<ConstantPointerNull>(CV)) { |
| 260 | Out << "null"; |
| 261 | |
| 262 | // FIXME: Handle ConstantPointerRef + lots of others... |
| 263 | } else { |
| 264 | Out << CV->getStrValue(); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | |
| 269 | // WriteAsOperand - Write the name of the specified value out to the specified |
| 270 | // ostream. This can be useful when you just want to print int %reg126, not the |
| 271 | // whole instruction that generated it. |
| 272 | // |
| 273 | static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName, |
| 274 | map<const Type *, string> &TypeTable, |
| 275 | SlotCalculator *Table) { |
| 276 | Out << " "; |
| 277 | if (PrintName && V->hasName()) { |
| 278 | Out << "%" << V->getName(); |
| 279 | } else { |
| 280 | if (const Constant *CV = dyn_cast<const Constant>(V)) { |
| 281 | WriteConstantInt(Out, CV, PrintName, TypeTable, Table); |
| 282 | } else { |
| 283 | int Slot; |
| 284 | if (Table) { |
| 285 | Slot = Table->getValSlot(V); |
| 286 | } else { |
| 287 | if (const Type *Ty = dyn_cast<const Type>(V)) { |
| 288 | Out << Ty->getDescription(); |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | Table = createSlotCalculator(V); |
| 293 | if (Table == 0) { Out << "BAD VALUE TYPE!"; return; } |
| 294 | |
| 295 | Slot = Table->getValSlot(V); |
| 296 | delete Table; |
| 297 | } |
| 298 | if (Slot >= 0) Out << "%" << Slot; |
| 299 | else if (PrintName) |
| 300 | Out << "<badref>"; // Not embeded into a location? |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 306 | |
| 307 | // WriteAsOperand - Write the name of the specified value out to the specified |
| 308 | // ostream. This can be useful when you just want to print int %reg126, not the |
| 309 | // whole instruction that generated it. |
| 310 | // |
| 311 | ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType, |
| 312 | bool PrintName, SlotCalculator *Table) { |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame^] | 313 | map<const Type *, string> TypeNames; |
| 314 | const Module *M = getModuleFromVal(V); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 315 | |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame^] | 316 | if (M && M->hasSymbolTable()) |
| 317 | fillTypeNameTable(M, TypeNames); |
| 318 | |
| 319 | if (PrintType) |
| 320 | printTypeInt(Out, V->getType(), TypeNames); |
| 321 | |
| 322 | WriteAsOperandInternal(Out, V, PrintName, TypeNames, Table); |
Chris Lattner | 5e5abe3 | 2001-07-20 19:15:21 +0000 | [diff] [blame] | 323 | return Out; |
| 324 | } |
| 325 | |
| 326 | |
Chris Lattner | 2e9fee4 | 2001-07-12 23:35:26 +0000 | [diff] [blame] | 327 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 328 | class AssemblyWriter { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 329 | ostream &Out; |
| 330 | SlotCalculator &Table; |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 331 | const Module *TheModule; |
| 332 | map<const Type *, string> TypeNames; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 333 | public: |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 334 | inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M) |
| 335 | : Out(o), Table(Tab), TheModule(M) { |
| 336 | |
| 337 | // If the module has a symbol table, take all global types and stuff their |
| 338 | // names into the TypeNames map. |
| 339 | // |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 340 | fillTypeNameTable(M, TypeNames); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 343 | inline void write(const Module *M) { printModule(M); } |
| 344 | inline void write(const GlobalVariable *G) { printGlobal(G); } |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 345 | inline void write(const Function *F) { printFunction(F); } |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 346 | inline void write(const BasicBlock *BB) { printBasicBlock(BB); } |
| 347 | inline void write(const Instruction *I) { printInstruction(I); } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 348 | inline void write(const Constant *CPV) { printConstant(CPV); } |
Chris Lattner | 7db7958 | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 349 | inline void write(const Type *Ty) { printType(Ty); } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 350 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 351 | private : |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 352 | void printModule(const Module *M); |
| 353 | void printSymbolTable(const SymbolTable &ST); |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 354 | void printConstant(const Constant *CPV); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 355 | void printGlobal(const GlobalVariable *GV); |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 356 | void printFunction(const Function *F); |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 357 | void printArgument(const Argument *FA); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 358 | void printBasicBlock(const BasicBlock *BB); |
| 359 | void printInstruction(const Instruction *I); |
Chris Lattner | d816b53 | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 360 | |
| 361 | // printType - Go to extreme measures to attempt to print out a short, |
| 362 | // symbolic version of a type name. |
| 363 | // |
| 364 | ostream &printType(const Type *Ty) { |
| 365 | return printTypeInt(Out, Ty, TypeNames); |
| 366 | } |
| 367 | |
| 368 | // printTypeAtLeastOneLevel - Print out one level of the possibly complex type |
| 369 | // without considering any symbolic types that we may have equal to it. |
| 370 | // |
| 371 | ostream &printTypeAtLeastOneLevel(const Type *Ty); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 372 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 373 | void writeOperand(const Value *Op, bool PrintType, bool PrintName = true); |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 374 | |
| 375 | // printInfoComment - Print a little comment after the instruction indicating |
| 376 | // which slot it occupies. |
| 377 | void printInfoComment(const Value *V); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 378 | }; |
| 379 | |
| 380 | |
Chris Lattner | d816b53 | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 381 | // printTypeAtLeastOneLevel - Print out one level of the possibly complex type |
| 382 | // without considering any symbolic types that we may have equal to it. |
| 383 | // |
| 384 | ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) { |
| 385 | if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) { |
| 386 | printType(FTy->getReturnType()) << " ("; |
| 387 | for (FunctionType::ParamTypes::const_iterator |
| 388 | I = FTy->getParamTypes().begin(), |
| 389 | E = FTy->getParamTypes().end(); I != E; ++I) { |
| 390 | if (I != FTy->getParamTypes().begin()) |
| 391 | Out << ", "; |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame^] | 392 | printType(*I); |
Chris Lattner | d816b53 | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 393 | } |
| 394 | if (FTy->isVarArg()) { |
| 395 | if (!FTy->getParamTypes().empty()) Out << ", "; |
| 396 | Out << "..."; |
| 397 | } |
| 398 | Out << ")"; |
| 399 | } else if (StructType *STy = dyn_cast<StructType>(Ty)) { |
| 400 | Out << "{ "; |
| 401 | for (StructType::ElementTypes::const_iterator |
| 402 | I = STy->getElementTypes().begin(), |
| 403 | E = STy->getElementTypes().end(); I != E; ++I) { |
| 404 | if (I != STy->getElementTypes().begin()) |
| 405 | Out << ", "; |
| 406 | printType(*I); |
| 407 | } |
| 408 | Out << " }"; |
| 409 | } else if (PointerType *PTy = dyn_cast<PointerType>(Ty)) { |
| 410 | printType(PTy->getElementType()) << "*"; |
| 411 | } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 412 | Out << "[" << ATy->getNumElements() << " x "; |
| 413 | printType(ATy->getElementType()) << "]"; |
| 414 | } else { |
| 415 | assert(Ty->isPrimitiveType() && "Unknown derived type!"); |
| 416 | printType(Ty); |
| 417 | } |
| 418 | return Out; |
| 419 | } |
| 420 | |
| 421 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 422 | void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType, |
| 423 | bool PrintName) { |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 424 | if (PrintType) { Out << " "; printType(Operand->getType()); } |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame^] | 425 | WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Table); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 428 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 429 | void AssemblyWriter::printModule(const Module *M) { |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 430 | // Loop over the symbol table, emitting all named constants... |
| 431 | if (M->hasSymbolTable()) |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 432 | printSymbolTable(*M->getSymbolTable()); |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 433 | |
| 434 | for_each(M->gbegin(), M->gend(), |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 435 | bind_obj(this, &AssemblyWriter::printGlobal)); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 436 | |
Vikram S. Adve | 13ba19a | 2001-09-18 12:48:16 +0000 | [diff] [blame] | 437 | Out << "implementation\n"; |
| 438 | |
Chris Lattner | 6915f8f | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 439 | // Output all of the functions... |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 440 | for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::printFunction)); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 443 | void AssemblyWriter::printGlobal(const GlobalVariable *GV) { |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 444 | if (GV->hasName()) Out << "%" << GV->getName() << " = "; |
Chris Lattner | 3779864 | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 445 | |
Chris Lattner | 841d8b9 | 2001-11-26 18:54:16 +0000 | [diff] [blame] | 446 | if (GV->hasInternalLinkage()) Out << "internal "; |
Chris Lattner | 3779864 | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 447 | if (!GV->hasInitializer()) Out << "uninitialized "; |
| 448 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 449 | Out << (GV->isConstant() ? "constant " : "global "); |
Chris Lattner | 2413b16 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 450 | printType(GV->getType()->getElementType()); |
Chris Lattner | 3779864 | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 451 | |
| 452 | if (GV->hasInitializer()) |
| 453 | writeOperand(GV->getInitializer(), false, false); |
| 454 | |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 455 | printInfoComment(GV); |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 456 | Out << "\n"; |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 459 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 460 | // printSymbolTable - Run through symbol table looking for named constants |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 461 | // if a named constant is found, emit it's declaration... |
| 462 | // |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 463 | void AssemblyWriter::printSymbolTable(const SymbolTable &ST) { |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 464 | for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) { |
| 465 | SymbolTable::type_const_iterator I = ST.type_begin(TI->first); |
| 466 | SymbolTable::type_const_iterator End = ST.type_end(TI->first); |
| 467 | |
| 468 | for (; I != End; ++I) { |
| 469 | const Value *V = I->second; |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 470 | if (const Constant *CPV = dyn_cast<const Constant>(V)) { |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 471 | printConstant(CPV); |
Chris Lattner | 3856934 | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 472 | } else if (const Type *Ty = dyn_cast<const Type>(V)) { |
Chris Lattner | d816b53 | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 473 | Out << "\t%" << I->first << " = type "; |
| 474 | |
| 475 | // Make sure we print out at least one level of the type structure, so |
| 476 | // that we do not get %FILE = type %FILE |
| 477 | // |
| 478 | printTypeAtLeastOneLevel(Ty) << "\n"; |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 479 | } |
| 480 | } |
Chris Lattner | a7620d9 | 2001-07-15 06:35:59 +0000 | [diff] [blame] | 481 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 485 | // printConstant - Print out a constant pool entry... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 486 | // |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 487 | void AssemblyWriter::printConstant(const Constant *CPV) { |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 488 | // Don't print out unnamed constants, they will be inlined |
| 489 | if (!CPV->hasName()) return; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 490 | |
Chris Lattner | ee998be | 2001-07-26 16:29:38 +0000 | [diff] [blame] | 491 | // Print out name... |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame^] | 492 | Out << "\t%" << CPV->getName() << " ="; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 493 | |
| 494 | // Write the value out now... |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame^] | 495 | writeOperand(CPV, true, false); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 496 | |
| 497 | if (!CPV->hasName() && CPV->getType() != Type::VoidTy) { |
| 498 | int Slot = Table.getValSlot(CPV); // Print out the def slot taken... |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 499 | Out << "\t\t; <"; |
| 500 | printType(CPV->getType()) << ">:"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 501 | if (Slot >= 0) Out << Slot; |
| 502 | else Out << "<badref>"; |
| 503 | } |
| 504 | |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 505 | Out << "\n"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Chris Lattner | 6915f8f | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 508 | // printFunction - Print all aspects of a function. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 509 | // |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 510 | void AssemblyWriter::printFunction(const Function *M) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 511 | // Print out the return type and name... |
Chris Lattner | 841d8b9 | 2001-11-26 18:54:16 +0000 | [diff] [blame] | 512 | Out << "\n" << (M->isExternal() ? "declare " : "") |
| 513 | << (M->hasInternalLinkage() ? "internal " : ""); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 514 | printType(M->getReturnType()) << " \"" << M->getName() << "\"("; |
Chris Lattner | 6915f8f | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 515 | Table.incorporateFunction(M); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 516 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 517 | // Loop over the arguments, printing them... |
Chris Lattner | 91db582 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 518 | const FunctionType *MT = M->getFunctionType(); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 519 | |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 520 | if (!M->isExternal()) { |
| 521 | for_each(M->getArgumentList().begin(), M->getArgumentList().end(), |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 522 | bind_obj(this, &AssemblyWriter::printArgument)); |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 523 | } else { |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 524 | // Loop over the arguments, printing them... |
Chris Lattner | 91db582 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 525 | const FunctionType *MT = M->getFunctionType(); |
| 526 | for (FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin(), |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 527 | E = MT->getParamTypes().end(); I != E; ++I) { |
| 528 | if (I != MT->getParamTypes().begin()) Out << ", "; |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 529 | printType(*I); |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 530 | } |
| 531 | } |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 532 | |
| 533 | // Finish printing arguments... |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 534 | if (MT->isVarArg()) { |
| 535 | if (MT->getParamTypes().size()) Out << ", "; |
| 536 | Out << "..."; // Output varargs portion of signature! |
| 537 | } |
| 538 | Out << ")\n"; |
| 539 | |
| 540 | if (!M->isExternal()) { |
| 541 | // Loop over the symbol table, emitting all named constants... |
| 542 | if (M->hasSymbolTable()) |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 543 | printSymbolTable(*M->getSymbolTable()); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 544 | |
| 545 | Out << "begin"; |
| 546 | |
Chris Lattner | 6915f8f | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 547 | // Output all of its basic blocks... for the function |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 548 | for_each(M->begin(), M->end(), |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 549 | bind_obj(this, &AssemblyWriter::printBasicBlock)); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 550 | |
Chris Lattner | a7620d9 | 2001-07-15 06:35:59 +0000 | [diff] [blame] | 551 | Out << "end\n"; |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Chris Lattner | 6915f8f | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 554 | Table.purgeFunction(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 557 | // printArgument - This member is called for every argument that |
Chris Lattner | 6915f8f | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 558 | // is passed into the function. Simply print it out |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 559 | // |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 560 | void AssemblyWriter::printArgument(const Argument *Arg) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 561 | // Insert commas as we go... the first arg doesn't get a comma |
| 562 | if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", "; |
| 563 | |
| 564 | // Output type... |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 565 | printType(Arg->getType()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 566 | |
| 567 | // Output name, if available... |
| 568 | if (Arg->hasName()) |
| 569 | Out << " %" << Arg->getName(); |
| 570 | else if (Table.getValSlot(Arg) < 0) |
| 571 | Out << "<badref>"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 574 | // printBasicBlock - This member is called for each basic block in a methd. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 575 | // |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 576 | void AssemblyWriter::printBasicBlock(const BasicBlock *BB) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 577 | if (BB->hasName()) { // Print out the label if it exists... |
Chris Lattner | a2f0187 | 2001-06-07 16:58:55 +0000 | [diff] [blame] | 578 | Out << "\n" << BB->getName() << ":"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 579 | } else { |
| 580 | int Slot = Table.getValSlot(BB); |
Chris Lattner | a2f0187 | 2001-06-07 16:58:55 +0000 | [diff] [blame] | 581 | Out << "\n; <label>:"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 582 | if (Slot >= 0) |
Chris Lattner | a2f0187 | 2001-06-07 16:58:55 +0000 | [diff] [blame] | 583 | Out << Slot; // Extra newline seperates out label's |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 584 | else |
Chris Lattner | a2f0187 | 2001-06-07 16:58:55 +0000 | [diff] [blame] | 585 | Out << "<badref>"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 586 | } |
Chris Lattner | a2f0187 | 2001-06-07 16:58:55 +0000 | [diff] [blame] | 587 | Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 588 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 589 | // Output all of the instructions in the basic block... |
| 590 | for_each(BB->begin(), BB->end(), |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 591 | bind_obj(this, &AssemblyWriter::printInstruction)); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 594 | |
| 595 | // printInfoComment - Print a little comment after the instruction indicating |
| 596 | // which slot it occupies. |
| 597 | // |
| 598 | void AssemblyWriter::printInfoComment(const Value *V) { |
| 599 | if (V->getType() != Type::VoidTy) { |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 600 | Out << "\t\t; <"; |
| 601 | printType(V->getType()) << ">"; |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 602 | |
| 603 | if (!V->hasName()) { |
| 604 | int Slot = Table.getValSlot(V); // Print out the def slot taken... |
| 605 | if (Slot >= 0) Out << ":" << Slot; |
| 606 | else Out << ":<badref>"; |
| 607 | } |
Chris Lattner | 3cb3a1f | 2001-12-14 16:29:12 +0000 | [diff] [blame] | 608 | Out << " [#uses=" << V->use_size() << "]"; // Output # uses |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 612 | // printInstruction - This member is called for each Instruction in a methd. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 613 | // |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 614 | void AssemblyWriter::printInstruction(const Instruction *I) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 615 | Out << "\t"; |
| 616 | |
| 617 | // Print out name if it exists... |
| 618 | if (I && I->hasName()) |
| 619 | Out << "%" << I->getName() << " = "; |
| 620 | |
| 621 | // Print out the opcode... |
Chris Lattner | b1ca9cb | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 622 | Out << I->getOpcodeName(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 623 | |
| 624 | // Print out the type of the operands... |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 625 | const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 626 | |
| 627 | // Special case conditional branches to swizzle the condition out to the front |
Chris Lattner | 8d48df2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 628 | if (isa<BranchInst>(I) && I->getNumOperands() > 1) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 629 | writeOperand(I->getOperand(2), true); |
| 630 | Out << ","; |
| 631 | writeOperand(Operand, true); |
| 632 | Out << ","; |
| 633 | writeOperand(I->getOperand(1), true); |
| 634 | |
Chris Lattner | 8d48df2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 635 | } else if (isa<SwitchInst>(I)) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 636 | // Special case switch statement to get formatting nice and correct... |
| 637 | writeOperand(Operand , true); Out << ","; |
| 638 | writeOperand(I->getOperand(1), true); Out << " ["; |
| 639 | |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 640 | for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 641 | Out << "\n\t\t"; |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 642 | writeOperand(I->getOperand(op ), true); Out << ","; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 643 | writeOperand(I->getOperand(op+1), true); |
| 644 | } |
| 645 | Out << "\n\t]"; |
Chris Lattner | da55810 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 646 | } else if (isa<PHINode>(I)) { |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 647 | Out << " "; |
Chris Lattner | 29644b3 | 2001-11-06 01:48:45 +0000 | [diff] [blame] | 648 | printType(I->getType()); |
Chris Lattner | 9d3292d | 2001-11-06 08:33:46 +0000 | [diff] [blame] | 649 | Out << " "; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 650 | |
Chris Lattner | 29644b3 | 2001-11-06 01:48:45 +0000 | [diff] [blame] | 651 | for (unsigned op = 0, Eop = I->getNumOperands(); op < Eop; op += 2) { |
| 652 | if (op) Out << ", "; |
| 653 | Out << "["; |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 654 | writeOperand(I->getOperand(op ), false); Out << ","; |
Chris Lattner | 4b94e23 | 2001-06-21 05:29:56 +0000 | [diff] [blame] | 655 | writeOperand(I->getOperand(op+1), false); Out << " ]"; |
Chris Lattner | 931ef3b | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 656 | } |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 657 | } else if (isa<ReturnInst>(I) && !Operand) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 658 | Out << " void"; |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 659 | } else if (isa<CallInst>(I)) { |
Chris Lattner | 2f2d947 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 660 | const PointerType *PTy = dyn_cast<PointerType>(Operand->getType()); |
Chris Lattner | 91db582 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 661 | const FunctionType*MTy = PTy ? dyn_cast<FunctionType>(PTy->getElementType()):0; |
Chris Lattner | 2f2d947 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 662 | const Type *RetTy = MTy ? MTy->getReturnType() : 0; |
| 663 | |
| 664 | // If possible, print out the short form of the call instruction, but we can |
Chris Lattner | 6915f8f | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 665 | // only do this if the first argument is a pointer to a nonvararg function, |
| 666 | // and if the value returned is not a pointer to a function. |
Chris Lattner | 2f2d947 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 667 | // |
Chris Lattner | 8d48df2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 668 | if (RetTy && MTy && !MTy->isVarArg() && |
| 669 | (!isa<PointerType>(RetTy) || |
| 670 | !isa<FunctionType>(cast<PointerType>(RetTy)))) { |
Chris Lattner | 2f2d947 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 671 | Out << " "; printType(RetTy); |
| 672 | writeOperand(Operand, false); |
| 673 | } else { |
| 674 | writeOperand(Operand, true); |
| 675 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 676 | Out << "("; |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 677 | if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true); |
| 678 | for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 679 | Out << ","; |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 680 | writeOperand(I->getOperand(op), true); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | Out << " )"; |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 684 | } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) { |
| 685 | // TODO: Should try to print out short form of the Invoke instruction |
| 686 | writeOperand(Operand, true); |
| 687 | Out << "("; |
| 688 | if (I->getNumOperands() > 3) writeOperand(I->getOperand(3), true); |
| 689 | for (unsigned op = 4, Eop = I->getNumOperands(); op < Eop; ++op) { |
| 690 | Out << ","; |
| 691 | writeOperand(I->getOperand(op), true); |
| 692 | } |
| 693 | |
| 694 | Out << " )\n\t\t\tto"; |
| 695 | writeOperand(II->getNormalDest(), true); |
| 696 | Out << " except"; |
| 697 | writeOperand(II->getExceptionalDest(), true); |
| 698 | |
Chris Lattner | 8d48df2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 699 | } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(I)) { |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 700 | Out << " "; |
Chris Lattner | 8d48df2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 701 | printType(AI->getType()->getElementType()); |
| 702 | if (AI->isArrayAllocation()) { |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 703 | Out << ","; |
Chris Lattner | 8d48df2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 704 | writeOperand(AI->getArraySize(), true); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 705 | } |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 706 | } else if (isa<CastInst>(I)) { |
Chris Lattner | a682182 | 2001-07-08 04:57:15 +0000 | [diff] [blame] | 707 | writeOperand(Operand, true); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 708 | Out << " to "; |
| 709 | printType(I->getType()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 710 | } else if (Operand) { // Print the normal way... |
| 711 | |
| 712 | // PrintAllTypes - Instructions who have operands of all the same type |
| 713 | // omit the type from all but the first operand. If the instruction has |
| 714 | // different type operands (for example br), then they are all printed. |
| 715 | bool PrintAllTypes = false; |
| 716 | const Type *TheType = Operand->getType(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 717 | |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 718 | for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) { |
| 719 | Operand = I->getOperand(i); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 720 | if (Operand->getType() != TheType) { |
| 721 | PrintAllTypes = true; // We have differing types! Print them all! |
| 722 | break; |
| 723 | } |
| 724 | } |
| 725 | |
Chris Lattner | b6fe234 | 2001-10-20 09:33:10 +0000 | [diff] [blame] | 726 | // Shift Left & Right print both types even for Ubyte LHS |
| 727 | if (isa<ShiftInst>(I)) PrintAllTypes = true; |
| 728 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 729 | if (!PrintAllTypes) { |
| 730 | Out << " "; |
| 731 | printType(I->getOperand(0)->getType()); |
| 732 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 733 | |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 734 | for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 735 | if (i) Out << ","; |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 736 | writeOperand(I->getOperand(i), PrintAllTypes); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 740 | printInfoComment(I); |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 741 | Out << "\n"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | |
| 745 | //===----------------------------------------------------------------------===// |
| 746 | // External Interface declarations |
| 747 | //===----------------------------------------------------------------------===// |
| 748 | |
| 749 | |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 750 | void Module::print(std::ostream &o) const { |
| 751 | SlotCalculator SlotTable(this, true); |
| 752 | AssemblyWriter W(o, SlotTable, this); |
| 753 | W.write(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 756 | void GlobalVariable::print(std::ostream &o) const { |
| 757 | SlotCalculator SlotTable(getParent(), true); |
| 758 | AssemblyWriter W(o, SlotTable, getParent()); |
| 759 | W.write(this); |
Chris Lattner | adfe0d1 | 2001-09-10 20:08:19 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 762 | void Function::print(std::ostream &o) const { |
| 763 | SlotCalculator SlotTable(getParent(), true); |
| 764 | AssemblyWriter W(o, SlotTable, getParent()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 765 | |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 766 | W.write(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 769 | void BasicBlock::print(std::ostream &o) const { |
| 770 | SlotCalculator SlotTable(getParent(), true); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 771 | AssemblyWriter W(o, SlotTable, |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 772 | getParent() ? getParent()->getParent() : 0); |
| 773 | W.write(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 776 | void Instruction::print(std::ostream &o) const { |
| 777 | const Function *F = getParent() ? getParent()->getParent() : 0; |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 778 | SlotCalculator SlotTable(F, true); |
| 779 | AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 780 | |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 781 | W.write(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 782 | } |
Chris Lattner | 7db7958 | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 783 | |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 784 | void Constant::print(std::ostream &o) const { |
| 785 | if (this == 0) { o << "<null> constant value\n"; return; } |
| 786 | o << " " << getType()->getDescription() << " " << getStrValue(); |
| 787 | } |
| 788 | |
| 789 | void Type::print(std::ostream &o) const { |
| 790 | if (this == 0) |
| 791 | o << "<null Type>"; |
| 792 | else |
| 793 | o << getDescription(); |
| 794 | } |
| 795 | |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 796 | void Argument::print(std::ostream &o) const { |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 797 | o << getType() << " " << getName(); |
| 798 | } |
| 799 | |
| 800 | void Value::dump() const { print(std::cerr); } |
| 801 | |
| 802 | //===----------------------------------------------------------------------===// |
| 803 | // CachedWriter Class Implementation |
| 804 | //===----------------------------------------------------------------------===// |
| 805 | |
Chris Lattner | 7db7958 | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 806 | void CachedWriter::setModule(const Module *M) { |
| 807 | delete SC; delete AW; |
| 808 | if (M) { |
| 809 | SC = new SlotCalculator(M, true); |
| 810 | AW = new AssemblyWriter(Out, *SC, M); |
| 811 | } else { |
| 812 | SC = 0; AW = 0; |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | CachedWriter::~CachedWriter() { |
| 817 | delete AW; |
| 818 | delete SC; |
| 819 | } |
| 820 | |
| 821 | CachedWriter &CachedWriter::operator<<(const Value *V) { |
| 822 | assert(AW && SC && "CachedWriter does not have a current module!"); |
| 823 | switch (V->getValueType()) { |
| 824 | case Value::ConstantVal: |
| 825 | Out << " "; AW->write(V->getType()); |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 826 | Out << " " << cast<Constant>(V)->getStrValue(); break; |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 827 | case Value::ArgumentVal: |
Chris Lattner | 7db7958 | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 828 | AW->write(V->getType()); Out << " " << V->getName(); break; |
| 829 | case Value::TypeVal: AW->write(cast<const Type>(V)); break; |
| 830 | case Value::InstructionVal: AW->write(cast<Instruction>(V)); break; |
| 831 | case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break; |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 832 | case Value::FunctionVal: AW->write(cast<Function>(V)); break; |
Chris Lattner | 7db7958 | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 833 | case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break; |
| 834 | case Value::ModuleVal: AW->write(cast<Module>(V)); break; |
| 835 | default: Out << "<unknown value type: " << V->getValueType() << ">"; break; |
| 836 | } |
| 837 | return *this; |
| 838 | } |