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