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