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