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