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