Chris Lattner | 8da78af | 2002-04-07 22:31:46 +0000 | [diff] [blame] | 1 | //===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
| 10 | // This library implements the functionality defined in llvm/Assembly/Writer.h |
| 11 | // |
Chris Lattner | 02b9399 | 2002-04-12 18:21:53 +0000 | [diff] [blame] | 12 | // Note that these routines must be extremely tolerant of various errors in the |
Chris Lattner | 8f77dae | 2003-05-08 02:44:12 +0000 | [diff] [blame] | 13 | // LLVM code, because it can be used for debugging transformations. |
Chris Lattner | 02b9399 | 2002-04-12 18:21:53 +0000 | [diff] [blame] | 14 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | da1fbcc | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 17 | #include "llvm/Assembly/CachedWriter.h" |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 18 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | f082b80 | 2002-07-23 18:07:49 +0000 | [diff] [blame] | 19 | #include "llvm/Assembly/PrintModulePass.h" |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 20 | #include "llvm/Assembly/AsmAnnotationWriter.h" |
Chris Lattner | f2d577b | 2004-01-20 19:50:34 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" |
Chris Lattner | 3eb59c0 | 2002-04-29 18:46:50 +0000 | [diff] [blame] | 22 | #include "llvm/DerivedTypes.h" |
Vikram S. Adve | b4dbb44 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 23 | #include "llvm/Instruction.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 24 | #include "llvm/iMemory.h" |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 25 | #include "llvm/iTerminators.h" |
Chris Lattner | 7061dc5 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 26 | #include "llvm/iPHINode.h" |
| 27 | #include "llvm/iOther.h" |
Chris Lattner | f2d577b | 2004-01-20 19:50:34 +0000 | [diff] [blame] | 28 | #include "llvm/Module.h" |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 29 | #include "llvm/SymbolTable.h" |
Misha Brukman | 5cf1acf | 2004-04-28 15:31:21 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/SlotCalculator.h" |
| 31 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 061269b | 2002-10-02 19:38:55 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CFG.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 33 | #include "Support/StringExtras.h" |
| 34 | #include "Support/STLExtras.h" |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 35 | #include <algorithm> |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 36 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 37 | |
Chris Lattner | a6275cc | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 38 | static RegisterPass<PrintModulePass> |
| 39 | X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization); |
| 40 | static RegisterPass<PrintFunctionPass> |
| 41 | Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization); |
Chris Lattner | f082b80 | 2002-07-23 18:07:49 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 43 | static void WriteAsOperandInternal(std::ostream &Out, const Value *V, |
| 44 | bool PrintName, |
| 45 | std::map<const Type *, std::string> &TypeTable, |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 46 | SlotCalculator *Table); |
| 47 | |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 48 | static const Module *getModuleFromVal(const Value *V) { |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 49 | if (const Argument *MA = dyn_cast<Argument>(V)) |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 50 | return MA->getParent() ? MA->getParent()->getParent() : 0; |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 51 | else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 52 | return BB->getParent() ? BB->getParent()->getParent() : 0; |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 53 | else if (const Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 54 | const Function *M = I->getParent() ? I->getParent()->getParent() : 0; |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 55 | return M ? M->getParent() : 0; |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 56 | } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 57 | return GV->getParent(); |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 58 | return 0; |
| 59 | } |
| 60 | |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 61 | static SlotCalculator *createSlotCalculator(const Value *V) { |
| 62 | assert(!isa<Type>(V) && "Can't create an SC for a type!"); |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 63 | if (const Argument *FA = dyn_cast<Argument>(V)) { |
Chris Lattner | 8ce7501 | 2004-01-14 02:49:34 +0000 | [diff] [blame] | 64 | return new SlotCalculator(FA->getParent(), false); |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 65 | } else if (const Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | 8ce7501 | 2004-01-14 02:49:34 +0000 | [diff] [blame] | 66 | return new SlotCalculator(I->getParent()->getParent(), false); |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 67 | } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) { |
Chris Lattner | 8ce7501 | 2004-01-14 02:49:34 +0000 | [diff] [blame] | 68 | return new SlotCalculator(BB->getParent(), false); |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 69 | } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){ |
Chris Lattner | 8ce7501 | 2004-01-14 02:49:34 +0000 | [diff] [blame] | 70 | return new SlotCalculator(GV->getParent(), false); |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 71 | } else if (const Function *Func = dyn_cast<Function>(V)) { |
Chris Lattner | 8ce7501 | 2004-01-14 02:49:34 +0000 | [diff] [blame] | 72 | return new SlotCalculator(Func, false); |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 73 | } |
| 74 | return 0; |
| 75 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 24b8a5d | 2003-08-22 05:40:38 +0000 | [diff] [blame] | 77 | // getLLVMName - Turn the specified string into an 'LLVM name', which is either |
| 78 | // prefixed with % (if the string only contains simple characters) or is |
| 79 | // surrounded with ""'s (if it has special chars in it). |
| 80 | static std::string getLLVMName(const std::string &Name) { |
| 81 | assert(!Name.empty() && "Cannot get empty name!"); |
| 82 | |
| 83 | // First character cannot start with a number... |
| 84 | if (Name[0] >= '0' && Name[0] <= '9') |
| 85 | return "\"" + Name + "\""; |
| 86 | |
| 87 | // Scan to see if we have any characters that are not on the "white list" |
| 88 | for (unsigned i = 0, e = Name.size(); i != e; ++i) { |
| 89 | char C = Name[i]; |
| 90 | assert(C != '"' && "Illegal character in LLVM value name!"); |
| 91 | if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') && |
| 92 | C != '-' && C != '.' && C != '_') |
| 93 | return "\"" + Name + "\""; |
| 94 | } |
| 95 | |
| 96 | // If we get here, then the identifier is legal to use as a "VarID". |
| 97 | return "%"+Name; |
| 98 | } |
| 99 | |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 100 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 101 | /// fillTypeNameTable - If the module has a symbol table, take all global types |
| 102 | /// and stuff their names into the TypeNames map. |
| 103 | /// |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 104 | static void fillTypeNameTable(const Module *M, |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 105 | std::map<const Type *, std::string> &TypeNames) { |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 106 | if (!M) return; |
| 107 | const SymbolTable &ST = M->getSymbolTable(); |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame^] | 108 | SymbolTable::type_const_iterator TI = ST.type_begin(); |
| 109 | for (; TI != ST.type_end(); ++TI ) { |
| 110 | // As a heuristic, don't insert pointer to primitive types, because |
| 111 | // they are used too often to have a single useful name. |
| 112 | // |
| 113 | const Type *Ty = cast<Type>(TI->second); |
| 114 | if (!isa<PointerType>(Ty) || |
| 115 | !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() || |
| 116 | isa<OpaqueType>(cast<PointerType>(Ty)->getElementType())) |
| 117 | TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first))); |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | |
| 122 | |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 123 | static std::string calcTypeName(const Type *Ty, |
| 124 | std::vector<const Type *> &TypeStack, |
| 125 | std::map<const Type *, std::string> &TypeNames){ |
Chris Lattner | 88c1738 | 2003-10-30 00:22:33 +0000 | [diff] [blame] | 126 | if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)) |
| 127 | return Ty->getDescription(); // Base case |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 128 | |
| 129 | // Check to see if the type is named. |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 130 | std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty); |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 131 | if (I != TypeNames.end()) return I->second; |
| 132 | |
Chris Lattner | 88c1738 | 2003-10-30 00:22:33 +0000 | [diff] [blame] | 133 | if (isa<OpaqueType>(Ty)) |
| 134 | return "opaque"; |
| 135 | |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 136 | // Check to see if the Type is already on the stack... |
| 137 | unsigned Slot = 0, CurSize = TypeStack.size(); |
| 138 | while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type |
| 139 | |
| 140 | // This is another base case for the recursion. In this case, we know |
| 141 | // that we have looped back to a type that we have previously visited. |
| 142 | // Generate the appropriate upreference to handle this. |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 143 | if (Slot < CurSize) |
| 144 | return "\\" + utostr(CurSize-Slot); // Here's the upreference |
| 145 | |
| 146 | TypeStack.push_back(Ty); // Recursive case: Add us to the stack.. |
| 147 | |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 148 | std::string Result; |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 149 | switch (Ty->getPrimitiveID()) { |
Chris Lattner | 6bfd6a5 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 150 | case Type::FunctionTyID: { |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 151 | const FunctionType *FTy = cast<FunctionType>(Ty); |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 152 | Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " ("; |
Chris Lattner | d5d8996 | 2004-02-09 04:14:01 +0000 | [diff] [blame] | 153 | for (FunctionType::param_iterator I = FTy->param_begin(), |
| 154 | E = FTy->param_end(); I != E; ++I) { |
| 155 | if (I != FTy->param_begin()) |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 156 | Result += ", "; |
| 157 | Result += calcTypeName(*I, TypeStack, TypeNames); |
| 158 | } |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 159 | if (FTy->isVarArg()) { |
Chris Lattner | d5d8996 | 2004-02-09 04:14:01 +0000 | [diff] [blame] | 160 | if (FTy->getNumParams()) Result += ", "; |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 161 | Result += "..."; |
| 162 | } |
| 163 | Result += ")"; |
| 164 | break; |
| 165 | } |
| 166 | case Type::StructTyID: { |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 167 | const StructType *STy = cast<StructType>(Ty); |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 168 | Result = "{ "; |
Chris Lattner | d21cd80 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 169 | for (StructType::element_iterator I = STy->element_begin(), |
| 170 | E = STy->element_end(); I != E; ++I) { |
| 171 | if (I != STy->element_begin()) |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 172 | Result += ", "; |
| 173 | Result += calcTypeName(*I, TypeStack, TypeNames); |
| 174 | } |
| 175 | Result += " }"; |
| 176 | break; |
| 177 | } |
| 178 | case Type::PointerTyID: |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 179 | Result = calcTypeName(cast<PointerType>(Ty)->getElementType(), |
Chris Lattner | 02b9399 | 2002-04-12 18:21:53 +0000 | [diff] [blame] | 180 | TypeStack, TypeNames) + "*"; |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 181 | break; |
| 182 | case Type::ArrayTyID: { |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 183 | const ArrayType *ATy = cast<ArrayType>(Ty); |
Chris Lattner | ff5c296 | 2002-04-13 21:11:04 +0000 | [diff] [blame] | 184 | Result = "[" + utostr(ATy->getNumElements()) + " x "; |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 185 | Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]"; |
| 186 | break; |
| 187 | } |
Chris Lattner | 9e094c4 | 2003-05-14 17:50:47 +0000 | [diff] [blame] | 188 | case Type::OpaqueTyID: |
| 189 | Result = "opaque"; |
| 190 | break; |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 191 | default: |
Vikram S. Adve | b4dbb44 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 192 | Result = "<unrecognized-type>"; |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | TypeStack.pop_back(); // Remove self from stack... |
| 196 | return Result; |
| 197 | } |
| 198 | |
| 199 | |
Misha Brukman | 9d0802e | 2004-03-01 19:48:13 +0000 | [diff] [blame] | 200 | /// printTypeInt - The internal guts of printing out a type that has a |
| 201 | /// potentially named portion. |
| 202 | /// |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 203 | static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty, |
| 204 | std::map<const Type *, std::string> &TypeNames) { |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 205 | // Primitive types always print out their description, regardless of whether |
| 206 | // they have been named or not. |
| 207 | // |
Chris Lattner | daf2a49 | 2003-10-30 00:12:51 +0000 | [diff] [blame] | 208 | if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)) |
| 209 | return Out << Ty->getDescription(); |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 210 | |
| 211 | // Check to see if the type is named. |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 212 | std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty); |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 213 | if (I != TypeNames.end()) return Out << I->second; |
| 214 | |
| 215 | // Otherwise we have a type that has not been named but is a derived type. |
| 216 | // Carefully recurse the type hierarchy to print out any contained symbolic |
| 217 | // names. |
| 218 | // |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 219 | std::vector<const Type *> TypeStack; |
| 220 | std::string TypeName = calcTypeName(Ty, TypeStack, TypeNames); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 221 | TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 222 | return Out << TypeName; |
| 223 | } |
| 224 | |
Chris Lattner | e51e03b | 2001-10-31 04:33:19 +0000 | [diff] [blame] | 225 | |
Misha Brukman | 9d0802e | 2004-03-01 19:48:13 +0000 | [diff] [blame] | 226 | /// WriteTypeSymbolic - This attempts to write the specified type as a symbolic |
| 227 | /// type, iff there is an entry in the modules symbol table for the specified |
| 228 | /// type or one of it's component types. This is slower than a simple x << Type |
| 229 | /// |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 230 | std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty, |
| 231 | const Module *M) { |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 232 | Out << " "; |
| 233 | |
| 234 | // If they want us to print out a type, attempt to make it symbolic if there |
| 235 | // is a symbol table in the module... |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 236 | if (M) { |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 237 | std::map<const Type *, std::string> TypeNames; |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 238 | fillTypeNameTable(M, TypeNames); |
| 239 | |
Chris Lattner | 7b8660d | 2001-10-29 16:40:32 +0000 | [diff] [blame] | 240 | return printTypeInt(Out, Ty, TypeNames); |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 241 | } else { |
Chris Lattner | 7b8660d | 2001-10-29 16:40:32 +0000 | [diff] [blame] | 242 | return Out << Ty->getDescription(); |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 246 | static void WriteConstantInt(std::ostream &Out, const Constant *CV, |
| 247 | bool PrintName, |
| 248 | std::map<const Type *, std::string> &TypeTable, |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 249 | SlotCalculator *Table) { |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 250 | if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) { |
| 251 | Out << (CB == ConstantBool::True ? "true" : "false"); |
| 252 | } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) { |
| 253 | Out << CI->getValue(); |
| 254 | } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) { |
| 255 | Out << CI->getValue(); |
| 256 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
| 257 | // We would like to output the FP constant value in exponential notation, |
| 258 | // but we cannot do this if doing so will lose precision. Check here to |
| 259 | // make sure that we only output it in exponential format if we can parse |
| 260 | // the value back and get the same value. |
| 261 | // |
| 262 | std::string StrVal = ftostr(CFP->getValue()); |
| 263 | |
| 264 | // Check to make sure that the stringized number is not some string like |
| 265 | // "Inf" or NaN, that atof will accept, but the lexer will not. Check that |
| 266 | // the string matches the "[-+]?[0-9]" regex. |
| 267 | // |
| 268 | if ((StrVal[0] >= '0' && StrVal[0] <= '9') || |
| 269 | ((StrVal[0] == '-' || StrVal[0] == '+') && |
Brian Gaeke | b471a23 | 2003-06-17 23:55:35 +0000 | [diff] [blame] | 270 | (StrVal[1] >= '0' && StrVal[1] <= '9'))) |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 271 | // Reparse stringized version! |
| 272 | if (atof(StrVal.c_str()) == CFP->getValue()) { |
| 273 | Out << StrVal; return; |
| 274 | } |
| 275 | |
| 276 | // Otherwise we could not reparse it to exactly the same value, so we must |
| 277 | // output the string in hexadecimal format! |
| 278 | // |
| 279 | // Behave nicely in the face of C TBAA rules... see: |
| 280 | // http://www.nullstone.com/htmls/category/aliastyp.htm |
| 281 | // |
| 282 | double Val = CFP->getValue(); |
| 283 | char *Ptr = (char*)&Val; |
| 284 | assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 && |
| 285 | "assuming that double is 64 bits!"); |
| 286 | Out << "0x" << utohexstr(*(uint64_t*)Ptr); |
| 287 | |
Chris Lattner | de512b5 | 2004-02-15 05:55:15 +0000 | [diff] [blame] | 288 | } else if (isa<ConstantAggregateZero>(CV)) { |
| 289 | Out << "zeroinitializer"; |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 290 | } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) { |
| 291 | // As a special case, print the array as a string if it is an array of |
| 292 | // ubytes or an array of sbytes with positive values. |
| 293 | // |
| 294 | const Type *ETy = CA->getType()->getElementType(); |
| 295 | bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy); |
| 296 | |
| 297 | if (ETy == Type::SByteTy) |
| 298 | for (unsigned i = 0; i < CA->getNumOperands(); ++i) |
| 299 | if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) { |
| 300 | isString = false; |
| 301 | break; |
| 302 | } |
| 303 | |
| 304 | if (isString) { |
| 305 | Out << "c\""; |
| 306 | for (unsigned i = 0; i < CA->getNumOperands(); ++i) { |
Chris Lattner | c07736a | 2003-07-23 15:22:26 +0000 | [diff] [blame] | 307 | unsigned char C = cast<ConstantInt>(CA->getOperand(i))->getRawValue(); |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 308 | |
Chris Lattner | fc94446 | 2002-07-31 23:56:44 +0000 | [diff] [blame] | 309 | if (isprint(C) && C != '"' && C != '\\') { |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 310 | Out << C; |
| 311 | } else { |
| 312 | Out << '\\' |
| 313 | << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A')) |
| 314 | << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A')); |
| 315 | } |
| 316 | } |
| 317 | Out << "\""; |
| 318 | |
| 319 | } else { // Cannot output in string format... |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 320 | Out << "["; |
| 321 | if (CA->getNumOperands()) { |
| 322 | Out << " "; |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 323 | printTypeInt(Out, ETy, TypeTable); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 324 | WriteAsOperandInternal(Out, CA->getOperand(0), |
| 325 | PrintName, TypeTable, Table); |
| 326 | for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) { |
| 327 | Out << ", "; |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 328 | printTypeInt(Out, ETy, TypeTable); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 329 | WriteAsOperandInternal(Out, CA->getOperand(i), PrintName, |
| 330 | TypeTable, Table); |
| 331 | } |
| 332 | } |
| 333 | Out << " ]"; |
| 334 | } |
| 335 | } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) { |
| 336 | Out << "{"; |
| 337 | if (CS->getNumOperands()) { |
| 338 | Out << " "; |
| 339 | printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable); |
| 340 | |
| 341 | WriteAsOperandInternal(Out, CS->getOperand(0), |
| 342 | PrintName, TypeTable, Table); |
| 343 | |
| 344 | for (unsigned i = 1; i < CS->getNumOperands(); i++) { |
| 345 | Out << ", "; |
| 346 | printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable); |
| 347 | |
| 348 | WriteAsOperandInternal(Out, CS->getOperand(i), |
| 349 | PrintName, TypeTable, Table); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | Out << " }"; |
| 354 | } else if (isa<ConstantPointerNull>(CV)) { |
| 355 | Out << "null"; |
| 356 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 357 | } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) { |
Chris Lattner | 2ff95b6 | 2004-01-18 21:03:06 +0000 | [diff] [blame] | 358 | WriteAsOperandInternal(Out, PR->getValue(), true, TypeTable, Table); |
Vikram S. Adve | b4dbb44 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 359 | |
Chris Lattner | c188eeb | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 360 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
Chris Lattner | 38d8773 | 2003-03-06 23:23:32 +0000 | [diff] [blame] | 361 | Out << CE->getOpcodeName() << " ("; |
Vikram S. Adve | b4dbb44 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 362 | |
| 363 | for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) { |
| 364 | printTypeInt(Out, (*OI)->getType(), TypeTable); |
| 365 | WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Table); |
| 366 | if (OI+1 != CE->op_end()) |
Chris Lattner | c188eeb | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 367 | Out << ", "; |
Vikram S. Adve | b4dbb44 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Chris Lattner | 1c93e5b | 2002-08-16 21:17:11 +0000 | [diff] [blame] | 370 | if (CE->getOpcode() == Instruction::Cast) { |
Chris Lattner | 95586b8 | 2002-08-15 19:37:43 +0000 | [diff] [blame] | 371 | Out << " to "; |
| 372 | printTypeInt(Out, CE->getType(), TypeTable); |
| 373 | } |
Chris Lattner | 1c93e5b | 2002-08-16 21:17:11 +0000 | [diff] [blame] | 374 | Out << ")"; |
Chris Lattner | 95586b8 | 2002-08-15 19:37:43 +0000 | [diff] [blame] | 375 | |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 376 | } else { |
Vikram S. Adve | b4dbb44 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 377 | Out << "<placeholder or erroneous Constant>"; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
| 381 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 382 | /// WriteAsOperand - Write the name of the specified value out to the specified |
| 383 | /// ostream. This can be useful when you just want to print int %reg126, not |
| 384 | /// the whole instruction that generated it. |
| 385 | /// |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 386 | static void WriteAsOperandInternal(std::ostream &Out, const Value *V, |
| 387 | bool PrintName, |
| 388 | std::map<const Type*, std::string> &TypeTable, |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 389 | SlotCalculator *Table) { |
| 390 | Out << " "; |
| 391 | if (PrintName && V->hasName()) { |
Chris Lattner | 24b8a5d | 2003-08-22 05:40:38 +0000 | [diff] [blame] | 392 | Out << getLLVMName(V->getName()); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 393 | } else { |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 394 | if (const Constant *CV = dyn_cast<Constant>(V)) { |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 395 | WriteConstantInt(Out, CV, PrintName, TypeTable, Table); |
| 396 | } else { |
| 397 | int Slot; |
| 398 | if (Table) { |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 399 | Slot = Table->getSlot(V); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 400 | } else { |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 401 | if (const Type *Ty = dyn_cast<Type>(V)) { |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 402 | Out << Ty->getDescription(); |
| 403 | return; |
| 404 | } |
| 405 | |
| 406 | Table = createSlotCalculator(V); |
| 407 | if (Table == 0) { Out << "BAD VALUE TYPE!"; return; } |
| 408 | |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 409 | Slot = Table->getSlot(V); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 410 | delete Table; |
| 411 | } |
| 412 | if (Slot >= 0) Out << "%" << Slot; |
| 413 | else if (PrintName) |
Misha Brukman | 9d0802e | 2004-03-01 19:48:13 +0000 | [diff] [blame] | 414 | if (V->hasName()) |
| 415 | Out << "<badref: " << getLLVMName(V->getName()) << ">"; |
| 416 | else |
| 417 | Out << "<badref>"; // Not embedded into a location? |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | |
Misha Brukman | 9d0802e | 2004-03-01 19:48:13 +0000 | [diff] [blame] | 423 | /// WriteAsOperand - Write the name of the specified value out to the specified |
| 424 | /// ostream. This can be useful when you just want to print int %reg126, not |
| 425 | /// the whole instruction that generated it. |
| 426 | /// |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 427 | std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V, |
Misha Brukman | 9d0802e | 2004-03-01 19:48:13 +0000 | [diff] [blame] | 428 | bool PrintType, bool PrintName, |
| 429 | const Module *Context) { |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 430 | std::map<const Type *, std::string> TypeNames; |
Chris Lattner | 607dc68 | 2002-07-10 16:48:17 +0000 | [diff] [blame] | 431 | if (Context == 0) Context = getModuleFromVal(V); |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 432 | |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 433 | if (Context) |
Chris Lattner | 607dc68 | 2002-07-10 16:48:17 +0000 | [diff] [blame] | 434 | fillTypeNameTable(Context, TypeNames); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 435 | |
| 436 | if (PrintType) |
| 437 | printTypeInt(Out, V->getType(), TypeNames); |
| 438 | |
Brian Gaeke | cd4a398 | 2003-11-16 23:08:27 +0000 | [diff] [blame] | 439 | if (const Type *Ty = dyn_cast<Type> (V)) |
| 440 | printTypeInt(Out, Ty, TypeNames); |
| 441 | |
Chris Lattner | 607dc68 | 2002-07-10 16:48:17 +0000 | [diff] [blame] | 442 | WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0); |
Chris Lattner | 622f740 | 2001-07-20 19:15:21 +0000 | [diff] [blame] | 443 | return Out; |
| 444 | } |
| 445 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 446 | namespace llvm { |
Chris Lattner | d8c2e42 | 2001-07-12 23:35:26 +0000 | [diff] [blame] | 447 | |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 448 | class AssemblyWriter { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 449 | std::ostream *Out; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 450 | SlotCalculator &Table; |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 451 | const Module *TheModule; |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 452 | std::map<const Type *, std::string> TypeNames; |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 453 | AssemblyAnnotationWriter *AnnotationWriter; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 454 | public: |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 455 | inline AssemblyWriter(std::ostream &o, SlotCalculator &Tab, const Module *M, |
| 456 | AssemblyAnnotationWriter *AAW) |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 457 | : Out(&o), Table(Tab), TheModule(M), AnnotationWriter(AAW) { |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 458 | |
| 459 | // If the module has a symbol table, take all global types and stuff their |
| 460 | // names into the TypeNames map. |
| 461 | // |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 462 | fillTypeNameTable(M, TypeNames); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 465 | inline void write(const Module *M) { printModule(M); } |
| 466 | inline void write(const GlobalVariable *G) { printGlobal(G); } |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 467 | inline void write(const Function *F) { printFunction(F); } |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 468 | inline void write(const BasicBlock *BB) { printBasicBlock(BB); } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 469 | inline void write(const Instruction *I) { printInstruction(*I); } |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 470 | inline void write(const Constant *CPV) { printConstant(CPV); } |
Chris Lattner | da1fbcc | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 471 | inline void write(const Type *Ty) { printType(Ty); } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 472 | |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 473 | void writeOperand(const Value *Op, bool PrintType, bool PrintName = true); |
| 474 | |
Misha Brukman | 5cf1acf | 2004-04-28 15:31:21 +0000 | [diff] [blame] | 475 | const Module* getModule() { return TheModule; } |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 476 | void setStream(std::ostream &os) { Out = &os; } |
Misha Brukman | 5cf1acf | 2004-04-28 15:31:21 +0000 | [diff] [blame] | 477 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 478 | private : |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 479 | void printModule(const Module *M); |
| 480 | void printSymbolTable(const SymbolTable &ST); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 481 | void printConstant(const Constant *CPV); |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 482 | void printGlobal(const GlobalVariable *GV); |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 483 | void printFunction(const Function *F); |
Chris Lattner | 73e2142 | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 484 | void printArgument(const Argument *FA); |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 485 | void printBasicBlock(const BasicBlock *BB); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 486 | void printInstruction(const Instruction &I); |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 487 | |
| 488 | // printType - Go to extreme measures to attempt to print out a short, |
| 489 | // symbolic version of a type name. |
| 490 | // |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 491 | std::ostream &printType(const Type *Ty) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 492 | return printTypeInt(*Out, Ty, TypeNames); |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | // printTypeAtLeastOneLevel - Print out one level of the possibly complex type |
| 496 | // without considering any symbolic types that we may have equal to it. |
| 497 | // |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 498 | std::ostream &printTypeAtLeastOneLevel(const Type *Ty); |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 499 | |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 500 | // printInfoComment - Print a little comment after the instruction indicating |
| 501 | // which slot it occupies. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 502 | void printInfoComment(const Value &V); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 503 | }; |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 504 | } // end of anonymous namespace |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 505 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 506 | /// printTypeAtLeastOneLevel - Print out one level of the possibly complex type |
| 507 | /// without considering any symbolic types that we may have equal to it. |
| 508 | /// |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 509 | std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 510 | if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) { |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 511 | printType(FTy->getReturnType()) << " ("; |
Chris Lattner | d5d8996 | 2004-02-09 04:14:01 +0000 | [diff] [blame] | 512 | for (FunctionType::param_iterator I = FTy->param_begin(), |
| 513 | E = FTy->param_end(); I != E; ++I) { |
| 514 | if (I != FTy->param_begin()) |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 515 | *Out << ", "; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 516 | printType(*I); |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 517 | } |
| 518 | if (FTy->isVarArg()) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 519 | if (FTy->getNumParams()) *Out << ", "; |
| 520 | *Out << "..."; |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 521 | } |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 522 | *Out << ")"; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 523 | } else if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 524 | *Out << "{ "; |
Chris Lattner | d21cd80 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 525 | for (StructType::element_iterator I = STy->element_begin(), |
| 526 | E = STy->element_end(); I != E; ++I) { |
| 527 | if (I != STy->element_begin()) |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 528 | *Out << ", "; |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 529 | printType(*I); |
| 530 | } |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 531 | *Out << " }"; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 532 | } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) { |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 533 | printType(PTy->getElementType()) << "*"; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 534 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 535 | *Out << "[" << ATy->getNumElements() << " x "; |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 536 | printType(ATy->getElementType()) << "]"; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 537 | } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 538 | *Out << "opaque"; |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 539 | } else { |
Vikram S. Adve | b4dbb44 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 540 | if (!Ty->isPrimitiveType()) |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 541 | *Out << "<unknown derived type>"; |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 542 | printType(Ty); |
| 543 | } |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 544 | return *Out; |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 548 | void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType, |
| 549 | bool PrintName) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 550 | if (PrintType) { *Out << " "; printType(Operand->getType()); } |
| 551 | WriteAsOperandInternal(*Out, Operand, PrintName, TypeNames, &Table); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 554 | |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 555 | void AssemblyWriter::printModule(const Module *M) { |
Chris Lattner | eb5d3a1 | 2003-08-24 13:48:48 +0000 | [diff] [blame] | 556 | switch (M->getEndianness()) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 557 | case Module::LittleEndian: *Out << "target endian = little\n"; break; |
| 558 | case Module::BigEndian: *Out << "target endian = big\n"; break; |
Chris Lattner | eb5d3a1 | 2003-08-24 13:48:48 +0000 | [diff] [blame] | 559 | case Module::AnyEndianness: break; |
| 560 | } |
| 561 | switch (M->getPointerSize()) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 562 | case Module::Pointer32: *Out << "target pointersize = 32\n"; break; |
| 563 | case Module::Pointer64: *Out << "target pointersize = 64\n"; break; |
Chris Lattner | eb5d3a1 | 2003-08-24 13:48:48 +0000 | [diff] [blame] | 564 | case Module::AnyPointerSize: break; |
| 565 | } |
| 566 | |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 567 | // Loop over the symbol table, emitting all named constants... |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 568 | printSymbolTable(M->getSymbolTable()); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 569 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 570 | for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I) |
| 571 | printGlobal(I); |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 572 | |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 573 | *Out << "\nimplementation ; Functions:\n"; |
Vikram S. Adve | 5efa3cc | 2001-09-18 12:48:16 +0000 | [diff] [blame] | 574 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 575 | // Output all of the functions... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 576 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 577 | printFunction(I); |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 580 | void AssemblyWriter::printGlobal(const GlobalVariable *GV) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 581 | if (GV->hasName()) *Out << getLLVMName(GV->getName()) << " = "; |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 582 | |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 583 | if (!GV->hasInitializer()) |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 584 | *Out << "external "; |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 585 | else |
| 586 | switch (GV->getLinkage()) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 587 | case GlobalValue::InternalLinkage: *Out << "internal "; break; |
| 588 | case GlobalValue::LinkOnceLinkage: *Out << "linkonce "; break; |
| 589 | case GlobalValue::WeakLinkage: *Out << "weak "; break; |
| 590 | case GlobalValue::AppendingLinkage: *Out << "appending "; break; |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 591 | case GlobalValue::ExternalLinkage: break; |
| 592 | } |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 593 | |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 594 | *Out << (GV->isConstant() ? "constant " : "global "); |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 595 | printType(GV->getType()->getElementType()); |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 596 | |
| 597 | if (GV->hasInitializer()) |
| 598 | writeOperand(GV->getInitializer(), false, false); |
| 599 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 600 | printInfoComment(*GV); |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 601 | *Out << "\n"; |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 604 | |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame^] | 605 | // printSymbolTable - Run through symbol table looking for constants |
| 606 | // and types. Emit their declarations. |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 607 | void AssemblyWriter::printSymbolTable(const SymbolTable &ST) { |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame^] | 608 | |
| 609 | // Print the types. |
| 610 | for (SymbolTable::type_const_iterator TI = ST.type_begin(); |
| 611 | TI != ST.type_end(); ++TI ) { |
| 612 | *Out << "\t" << getLLVMName(TI->first) << " = type "; |
| 613 | |
| 614 | // Make sure we print out at least one level of the type structure, so |
| 615 | // that we do not get %FILE = type %FILE |
| 616 | // |
| 617 | printTypeAtLeastOneLevel(TI->second) << "\n"; |
| 618 | } |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 619 | |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame^] | 620 | // Print the constants, in type plane order. |
| 621 | for (SymbolTable::plane_const_iterator PI = ST.plane_begin(); |
| 622 | PI != ST.plane_end(); ++PI ) { |
| 623 | SymbolTable::value_const_iterator VI = ST.value_begin(PI->first); |
| 624 | SymbolTable::value_const_iterator VE = ST.value_end(PI->first); |
| 625 | |
| 626 | for (; VI != VE; ++VI) { |
| 627 | const Value *V = VI->second; |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 628 | if (const Constant *CPV = dyn_cast<Constant>(V)) { |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 629 | printConstant(CPV); |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 630 | } |
| 631 | } |
Chris Lattner | 739a56d | 2001-07-15 06:35:59 +0000 | [diff] [blame] | 632 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 636 | /// printConstant - Print out a constant pool entry... |
| 637 | /// |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 638 | void AssemblyWriter::printConstant(const Constant *CPV) { |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 639 | // Don't print out unnamed constants, they will be inlined |
| 640 | if (!CPV->hasName()) return; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 641 | |
Chris Lattner | 1333ed5 | 2001-07-26 16:29:38 +0000 | [diff] [blame] | 642 | // Print out name... |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 643 | *Out << "\t" << getLLVMName(CPV->getName()) << " ="; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 644 | |
| 645 | // Write the value out now... |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 646 | writeOperand(CPV, true, false); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 647 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 648 | printInfoComment(*CPV); |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 649 | *Out << "\n"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 652 | /// printFunction - Print all aspects of a function. |
| 653 | /// |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 654 | void AssemblyWriter::printFunction(const Function *F) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 655 | // Print out the return type and name... |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 656 | *Out << "\n"; |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 657 | |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 658 | if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, *Out); |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 659 | |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 660 | if (F->isExternal()) |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 661 | *Out << "declare "; |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 662 | else |
| 663 | switch (F->getLinkage()) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 664 | case GlobalValue::InternalLinkage: *Out << "internal "; break; |
| 665 | case GlobalValue::LinkOnceLinkage: *Out << "linkonce "; break; |
| 666 | case GlobalValue::WeakLinkage: *Out << "weak "; break; |
| 667 | case GlobalValue::AppendingLinkage: *Out << "appending "; break; |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 668 | case GlobalValue::ExternalLinkage: break; |
| 669 | } |
| 670 | |
Chris Lattner | b8565e3 | 2003-09-03 17:56:43 +0000 | [diff] [blame] | 671 | printType(F->getReturnType()) << " "; |
Chris Lattner | 4d45bd0 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 672 | if (!F->getName().empty()) |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 673 | *Out << getLLVMName(F->getName()); |
Chris Lattner | 4d45bd0 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 674 | else |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 675 | *Out << "\"\""; |
| 676 | *Out << "("; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 677 | Table.incorporateFunction(F); |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 678 | |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 679 | // Loop over the arguments, printing them... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 680 | const FunctionType *FT = F->getFunctionType(); |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 681 | |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 682 | for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I) |
| 683 | printArgument(I); |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 684 | |
| 685 | // Finish printing arguments... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 686 | if (FT->isVarArg()) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 687 | if (FT->getNumParams()) *Out << ", "; |
| 688 | *Out << "..."; // Output varargs portion of signature! |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 689 | } |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 690 | *Out << ")"; |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 691 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 692 | if (F->isExternal()) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 693 | *Out << "\n"; |
Chris Lattner | 03e2acb | 2002-05-06 03:00:40 +0000 | [diff] [blame] | 694 | } else { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 695 | *Out << " {"; |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 696 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 697 | // Output all of its basic blocks... for the function |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 698 | for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I) |
| 699 | printBasicBlock(I); |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 700 | |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 701 | *Out << "}\n"; |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 704 | Table.purgeFunction(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 707 | /// printArgument - This member is called for every argument that is passed into |
| 708 | /// the function. Simply print it out |
| 709 | /// |
Chris Lattner | 73e2142 | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 710 | void AssemblyWriter::printArgument(const Argument *Arg) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 711 | // Insert commas as we go... the first arg doesn't get a comma |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 712 | if (Arg != &Arg->getParent()->afront()) *Out << ", "; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 713 | |
| 714 | // Output type... |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 715 | printType(Arg->getType()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 716 | |
| 717 | // Output name, if available... |
| 718 | if (Arg->hasName()) |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 719 | *Out << " " << getLLVMName(Arg->getName()); |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 720 | else if (Table.getSlot(Arg) < 0) |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 721 | *Out << "<badref>"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 724 | /// printBasicBlock - This member is called for each basic block in a method. |
| 725 | /// |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 726 | void AssemblyWriter::printBasicBlock(const BasicBlock *BB) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 727 | if (BB->hasName()) { // Print out the label if it exists... |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 728 | *Out << "\n" << BB->getName() << ":"; |
Chris Lattner | afc3868 | 2002-05-14 16:02:05 +0000 | [diff] [blame] | 729 | } else if (!BB->use_empty()) { // Don't print block # of no uses... |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 730 | int Slot = Table.getSlot(BB); |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 731 | *Out << "\n; <label>:"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 732 | if (Slot >= 0) |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 733 | *Out << Slot; // Extra newline separates out label's |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 734 | else |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 735 | *Out << "<badref>"; |
Chris Lattner | 061269b | 2002-10-02 19:38:55 +0000 | [diff] [blame] | 736 | } |
Chris Lattner | 4e4d862 | 2003-11-20 00:09:43 +0000 | [diff] [blame] | 737 | |
| 738 | if (BB->getParent() == 0) |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 739 | *Out << "\t\t; Error: Block without parent!"; |
Chris Lattner | 4e4d862 | 2003-11-20 00:09:43 +0000 | [diff] [blame] | 740 | else { |
| 741 | if (BB != &BB->getParent()->front()) { // Not the entry block? |
| 742 | // Output predecessors for the block... |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 743 | *Out << "\t\t;"; |
Chris Lattner | 4e4d862 | 2003-11-20 00:09:43 +0000 | [diff] [blame] | 744 | pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB); |
| 745 | |
| 746 | if (PI == PE) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 747 | *Out << " No predecessors!"; |
Chris Lattner | 4e4d862 | 2003-11-20 00:09:43 +0000 | [diff] [blame] | 748 | } else { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 749 | *Out << " preds ="; |
Chris Lattner | 40efcec | 2003-11-16 22:59:57 +0000 | [diff] [blame] | 750 | writeOperand(*PI, false, true); |
Chris Lattner | 4e4d862 | 2003-11-20 00:09:43 +0000 | [diff] [blame] | 751 | for (++PI; PI != PE; ++PI) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 752 | *Out << ","; |
Chris Lattner | 4e4d862 | 2003-11-20 00:09:43 +0000 | [diff] [blame] | 753 | writeOperand(*PI, false, true); |
| 754 | } |
Chris Lattner | 40efcec | 2003-11-16 22:59:57 +0000 | [diff] [blame] | 755 | } |
Chris Lattner | 061269b | 2002-10-02 19:38:55 +0000 | [diff] [blame] | 756 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 757 | } |
Chris Lattner | afc3868 | 2002-05-14 16:02:05 +0000 | [diff] [blame] | 758 | |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 759 | *Out << "\n"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 760 | |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 761 | if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, *Out); |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 762 | |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 763 | // Output all of the instructions in the basic block... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 764 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 765 | printInstruction(*I); |
Chris Lattner | 9f717ef | 2004-03-08 18:51:45 +0000 | [diff] [blame] | 766 | |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 767 | if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, *Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 770 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 771 | /// printInfoComment - Print a little comment after the instruction indicating |
| 772 | /// which slot it occupies. |
| 773 | /// |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 774 | void AssemblyWriter::printInfoComment(const Value &V) { |
| 775 | if (V.getType() != Type::VoidTy) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 776 | *Out << "\t\t; <"; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 777 | printType(V.getType()) << ">"; |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 778 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 779 | if (!V.hasName()) { |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 780 | int Slot = Table.getSlot(&V); // Print out the def slot taken... |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 781 | if (Slot >= 0) *Out << ":" << Slot; |
| 782 | else *Out << ":<badref>"; |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 783 | } |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 784 | *Out << " [#uses=" << V.use_size() << "]"; // Output # uses |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 788 | /// printInstruction - This member is called for each Instruction in a method. |
| 789 | /// |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 790 | void AssemblyWriter::printInstruction(const Instruction &I) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 791 | if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, *Out); |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 792 | |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 793 | *Out << "\t"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 794 | |
| 795 | // Print out name if it exists... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 796 | if (I.hasName()) |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 797 | *Out << getLLVMName(I.getName()) << " = "; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 798 | |
Chris Lattner | e5e475e | 2003-09-08 17:45:59 +0000 | [diff] [blame] | 799 | // If this is a volatile load or store, print out the volatile marker |
| 800 | if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) || |
| 801 | (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 802 | *Out << "volatile "; |
Chris Lattner | e5e475e | 2003-09-08 17:45:59 +0000 | [diff] [blame] | 803 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 804 | // Print out the opcode... |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 805 | *Out << I.getOpcodeName(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 806 | |
| 807 | // Print out the type of the operands... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 808 | const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 809 | |
| 810 | // Special case conditional branches to swizzle the condition out to the front |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 811 | if (isa<BranchInst>(I) && I.getNumOperands() > 1) { |
| 812 | writeOperand(I.getOperand(2), true); |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 813 | *Out << ","; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 814 | writeOperand(Operand, true); |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 815 | *Out << ","; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 816 | writeOperand(I.getOperand(1), true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 817 | |
Chris Lattner | 94dc1f2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 818 | } else if (isa<SwitchInst>(I)) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 819 | // Special case switch statement to get formatting nice and correct... |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 820 | writeOperand(Operand , true); *Out << ","; |
| 821 | writeOperand(I.getOperand(1), true); *Out << " ["; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 822 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 823 | for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 824 | *Out << "\n\t\t"; |
| 825 | writeOperand(I.getOperand(op ), true); *Out << ","; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 826 | writeOperand(I.getOperand(op+1), true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 827 | } |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 828 | *Out << "\n\t]"; |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 829 | } else if (isa<PHINode>(I)) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 830 | *Out << " "; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 831 | printType(I.getType()); |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 832 | *Out << " "; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 833 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 834 | for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 835 | if (op) *Out << ", "; |
| 836 | *Out << "["; |
| 837 | writeOperand(I.getOperand(op ), false); *Out << ","; |
| 838 | writeOperand(I.getOperand(op+1), false); *Out << " ]"; |
Chris Lattner | c24d208 | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 839 | } |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 840 | } else if (isa<ReturnInst>(I) && !Operand) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 841 | *Out << " void"; |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 842 | } else if (isa<CallInst>(I)) { |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 843 | const PointerType *PTy = cast<PointerType>(Operand->getType()); |
| 844 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 845 | const Type *RetTy = FTy->getReturnType(); |
Chris Lattner | 268de04 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 846 | |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 847 | // If possible, print out the short form of the call instruction. We can |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 848 | // only do this if the first argument is a pointer to a nonvararg function, |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 849 | // and if the return type is not a pointer to a function. |
Chris Lattner | 268de04 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 850 | // |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 851 | if (!FTy->isVarArg() && |
Chris Lattner | 94dc1f2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 852 | (!isa<PointerType>(RetTy) || |
Chris Lattner | c1b2718 | 2002-07-25 20:58:51 +0000 | [diff] [blame] | 853 | !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 854 | *Out << " "; printType(RetTy); |
Chris Lattner | 268de04 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 855 | writeOperand(Operand, false); |
| 856 | } else { |
| 857 | writeOperand(Operand, true); |
| 858 | } |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 859 | *Out << "("; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 860 | if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true); |
| 861 | for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 862 | *Out << ","; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 863 | writeOperand(I.getOperand(op), true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 866 | *Out << " )"; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 867 | } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) { |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 868 | const PointerType *PTy = cast<PointerType>(Operand->getType()); |
| 869 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 870 | const Type *RetTy = FTy->getReturnType(); |
| 871 | |
| 872 | // If possible, print out the short form of the invoke instruction. We can |
| 873 | // only do this if the first argument is a pointer to a nonvararg function, |
| 874 | // and if the return type is not a pointer to a function. |
| 875 | // |
| 876 | if (!FTy->isVarArg() && |
| 877 | (!isa<PointerType>(RetTy) || |
| 878 | !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 879 | *Out << " "; printType(RetTy); |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 880 | writeOperand(Operand, false); |
| 881 | } else { |
| 882 | writeOperand(Operand, true); |
| 883 | } |
| 884 | |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 885 | *Out << "("; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 886 | if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true); |
| 887 | for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 888 | *Out << ","; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 889 | writeOperand(I.getOperand(op), true); |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 892 | *Out << " )\n\t\t\tto"; |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 893 | writeOperand(II->getNormalDest(), true); |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 894 | *Out << " unwind"; |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 895 | writeOperand(II->getUnwindDest(), true); |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 896 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 897 | } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 898 | *Out << " "; |
Chris Lattner | 94dc1f2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 899 | printType(AI->getType()->getElementType()); |
| 900 | if (AI->isArrayAllocation()) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 901 | *Out << ","; |
Chris Lattner | 94dc1f2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 902 | writeOperand(AI->getArraySize(), true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 903 | } |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 904 | } else if (isa<CastInst>(I)) { |
Chris Lattner | 41495a2 | 2003-11-17 01:17:04 +0000 | [diff] [blame] | 905 | if (Operand) writeOperand(Operand, true); // Work with broken code |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 906 | *Out << " to "; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 907 | printType(I.getType()); |
Chris Lattner | 4d45bd0 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 908 | } else if (isa<VAArgInst>(I)) { |
Chris Lattner | 41495a2 | 2003-11-17 01:17:04 +0000 | [diff] [blame] | 909 | if (Operand) writeOperand(Operand, true); // Work with broken code |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 910 | *Out << ", "; |
Chris Lattner | 8f77dae | 2003-05-08 02:44:12 +0000 | [diff] [blame] | 911 | printType(I.getType()); |
Chris Lattner | 4d45bd0 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 912 | } else if (const VANextInst *VAN = dyn_cast<VANextInst>(&I)) { |
Chris Lattner | 41495a2 | 2003-11-17 01:17:04 +0000 | [diff] [blame] | 913 | if (Operand) writeOperand(Operand, true); // Work with broken code |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 914 | *Out << ", "; |
Chris Lattner | 4d45bd0 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 915 | printType(VAN->getArgType()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 916 | } else if (Operand) { // Print the normal way... |
| 917 | |
| 918 | // PrintAllTypes - Instructions who have operands of all the same type |
| 919 | // omit the type from all but the first operand. If the instruction has |
| 920 | // different type operands (for example br), then they are all printed. |
| 921 | bool PrintAllTypes = false; |
| 922 | const Type *TheType = Operand->getType(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 923 | |
Chris Lattner | cfdd148 | 2004-03-12 05:53:14 +0000 | [diff] [blame] | 924 | // Shift Left & Right print both types even for Ubyte LHS, and select prints |
| 925 | // types even if all operands are bools. |
| 926 | if (isa<ShiftInst>(I) || isa<SelectInst>(I)) { |
Chris Lattner | ffd9bf4 | 2003-04-16 20:20:02 +0000 | [diff] [blame] | 927 | PrintAllTypes = true; |
| 928 | } else { |
| 929 | for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) { |
| 930 | Operand = I.getOperand(i); |
| 931 | if (Operand->getType() != TheType) { |
| 932 | PrintAllTypes = true; // We have differing types! Print them all! |
| 933 | break; |
| 934 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 935 | } |
| 936 | } |
Chris Lattner | ffd9bf4 | 2003-04-16 20:20:02 +0000 | [diff] [blame] | 937 | |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 938 | if (!PrintAllTypes) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 939 | *Out << " "; |
Chris Lattner | ffd9bf4 | 2003-04-16 20:20:02 +0000 | [diff] [blame] | 940 | printType(TheType); |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 941 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 942 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 943 | for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) { |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 944 | if (i) *Out << ","; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 945 | writeOperand(I.getOperand(i), PrintAllTypes); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 946 | } |
| 947 | } |
| 948 | |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 949 | printInfoComment(I); |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 950 | *Out << "\n"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | |
| 954 | //===----------------------------------------------------------------------===// |
| 955 | // External Interface declarations |
| 956 | //===----------------------------------------------------------------------===// |
| 957 | |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 958 | void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const { |
Chris Lattner | 2ff95b6 | 2004-01-18 21:03:06 +0000 | [diff] [blame] | 959 | SlotCalculator SlotTable(this, false); |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 960 | AssemblyWriter W(o, SlotTable, this, AAW); |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 961 | W.write(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 964 | void GlobalVariable::print(std::ostream &o) const { |
Chris Lattner | 2ff95b6 | 2004-01-18 21:03:06 +0000 | [diff] [blame] | 965 | SlotCalculator SlotTable(getParent(), false); |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 966 | AssemblyWriter W(o, SlotTable, getParent(), 0); |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 967 | W.write(this); |
Chris Lattner | b0e4523 | 2001-09-10 20:08:19 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 970 | void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const { |
Chris Lattner | 2ff95b6 | 2004-01-18 21:03:06 +0000 | [diff] [blame] | 971 | SlotCalculator SlotTable(getParent(), false); |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 972 | AssemblyWriter W(o, SlotTable, getParent(), AAW); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 973 | |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 974 | W.write(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 977 | void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const { |
Chris Lattner | 2ff95b6 | 2004-01-18 21:03:06 +0000 | [diff] [blame] | 978 | SlotCalculator SlotTable(getParent(), false); |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 979 | AssemblyWriter W(o, SlotTable, |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 980 | getParent() ? getParent()->getParent() : 0, AAW); |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 981 | W.write(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 984 | void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const { |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 985 | const Function *F = getParent() ? getParent()->getParent() : 0; |
Chris Lattner | 2ff95b6 | 2004-01-18 21:03:06 +0000 | [diff] [blame] | 986 | SlotCalculator SlotTable(F, false); |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 987 | AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 988 | |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 989 | W.write(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 990 | } |
Chris Lattner | da1fbcc | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 991 | |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 992 | void Constant::print(std::ostream &o) const { |
| 993 | if (this == 0) { o << "<null> constant value\n"; return; } |
Chris Lattner | 3bc06b3 | 2002-09-10 15:53:49 +0000 | [diff] [blame] | 994 | |
| 995 | // Handle CPR's special, because they have context information... |
| 996 | if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) { |
| 997 | CPR->getValue()->print(o); // Print as a global value, with context info. |
| 998 | return; |
| 999 | } |
| 1000 | |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 1001 | o << " " << getType()->getDescription() << " "; |
| 1002 | |
Chris Lattner | 7b13f56 | 2003-05-08 02:08:14 +0000 | [diff] [blame] | 1003 | std::map<const Type *, std::string> TypeTable; |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 1004 | WriteConstantInt(o, this, false, TypeTable, 0); |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | void Type::print(std::ostream &o) const { |
| 1008 | if (this == 0) |
| 1009 | o << "<null Type>"; |
| 1010 | else |
| 1011 | o << getDescription(); |
| 1012 | } |
| 1013 | |
Chris Lattner | 73e2142 | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 1014 | void Argument::print(std::ostream &o) const { |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1015 | o << getType() << " " << getName(); |
| 1016 | } |
| 1017 | |
| 1018 | void Value::dump() const { print(std::cerr); } |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame^] | 1019 | void Type::dump() const { print(std::cerr); } |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1020 | |
| 1021 | //===----------------------------------------------------------------------===// |
| 1022 | // CachedWriter Class Implementation |
| 1023 | //===----------------------------------------------------------------------===// |
| 1024 | |
Chris Lattner | da1fbcc | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 1025 | void CachedWriter::setModule(const Module *M) { |
| 1026 | delete SC; delete AW; |
| 1027 | if (M) { |
Chris Lattner | 8ce7501 | 2004-01-14 02:49:34 +0000 | [diff] [blame] | 1028 | SC = new SlotCalculator(M, false); |
Misha Brukman | 5cf1acf | 2004-04-28 15:31:21 +0000 | [diff] [blame] | 1029 | AW = new AssemblyWriter(*Out, *SC, M, 0); |
Chris Lattner | da1fbcc | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 1030 | } else { |
| 1031 | SC = 0; AW = 0; |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | CachedWriter::~CachedWriter() { |
| 1036 | delete AW; |
| 1037 | delete SC; |
| 1038 | } |
| 1039 | |
| 1040 | CachedWriter &CachedWriter::operator<<(const Value *V) { |
| 1041 | assert(AW && SC && "CachedWriter does not have a current module!"); |
| 1042 | switch (V->getValueType()) { |
| 1043 | case Value::ConstantVal: |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 1044 | case Value::ArgumentVal: AW->writeOperand(V, true, true); break; |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 1045 | case Value::TypeVal: AW->write(cast<Type>(V)); break; |
Chris Lattner | da1fbcc | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 1046 | case Value::InstructionVal: AW->write(cast<Instruction>(V)); break; |
| 1047 | case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break; |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 1048 | case Value::FunctionVal: AW->write(cast<Function>(V)); break; |
Chris Lattner | da1fbcc | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 1049 | case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break; |
Misha Brukman | 5cf1acf | 2004-04-28 15:31:21 +0000 | [diff] [blame] | 1050 | default: *Out << "<unknown value type: " << V->getValueType() << ">"; break; |
Chris Lattner | da1fbcc | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 1051 | } |
| 1052 | return *this; |
| 1053 | } |
Misha Brukman | 5cf1acf | 2004-04-28 15:31:21 +0000 | [diff] [blame] | 1054 | |
| 1055 | CachedWriter& CachedWriter::operator<<(const Type *X) { |
| 1056 | if (SymbolicTypes) { |
| 1057 | const Module *M = AW->getModule(); |
| 1058 | if (M) WriteTypeSymbolic(*Out, X, M); |
| 1059 | return *this; |
| 1060 | } else |
| 1061 | return *this << (const Value*)X; |
| 1062 | } |
Misha Brukman | e5242de | 2004-04-28 19:24:28 +0000 | [diff] [blame] | 1063 | |
| 1064 | void CachedWriter::setStream(std::ostream &os) { |
| 1065 | Out = &os; |
| 1066 | if (AW) AW->setStream(os); |
| 1067 | } |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame^] | 1068 | |
| 1069 | // vim: sw=2 |