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 | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 5 | // TODO: print out the type name instead of the full type if a particular type |
| 6 | // is in the symbol table... |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Chris Lattner | 7db7958 | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 10 | #include "llvm/Assembly/CachedWriter.h" |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 11 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 6915f8f | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 12 | #include "llvm/SlotCalculator.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 13 | #include "llvm/Module.h" |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 14 | #include "llvm/Function.h" |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 15 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 16 | #include "llvm/BasicBlock.h" |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 17 | #include "llvm/ConstantVals.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 18 | #include "llvm/iMemory.h" |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 19 | #include "llvm/iTerminators.h" |
Chris Lattner | fb5ae02 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 20 | #include "llvm/iPHINode.h" |
| 21 | #include "llvm/iOther.h" |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 22 | #include "llvm/SymbolTable.h" |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 23 | #include "llvm/Argument.h" |
Chris Lattner | 5de2204 | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 24 | #include "Support/StringExtras.h" |
| 25 | #include "Support/STLExtras.h" |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 27 | #include <map> |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 28 | using std::string; |
| 29 | using std::map; |
| 30 | using std::vector; |
| 31 | using std::ostream; |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 32 | |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 33 | static const Module *getModuleFromVal(const Value *V) { |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 34 | if (const Argument *MA = dyn_cast<const Argument>(V)) |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 35 | return MA->getParent() ? MA->getParent()->getParent() : 0; |
| 36 | else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) |
| 37 | return BB->getParent() ? BB->getParent()->getParent() : 0; |
| 38 | else if (const Instruction *I = dyn_cast<const Instruction>(V)) { |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 39 | const Function *M = I->getParent() ? I->getParent()->getParent() : 0; |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 40 | return M ? M->getParent() : 0; |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 41 | } else if (const GlobalValue *GV = dyn_cast<const GlobalValue>(V)) |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 42 | return GV->getParent(); |
| 43 | else if (const Module *Mod = dyn_cast<const Module>(V)) |
| 44 | return Mod; |
| 45 | return 0; |
| 46 | } |
| 47 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 48 | static SlotCalculator *createSlotCalculator(const Value *V) { |
| 49 | assert(!isa<Type>(V) && "Can't create an SC for a type!"); |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 50 | if (const Argument *FA = dyn_cast<const Argument>(V)) { |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 51 | return new SlotCalculator(FA->getParent(), true); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 52 | } else if (const Instruction *I = dyn_cast<const Instruction>(V)) { |
| 53 | return new SlotCalculator(I->getParent()->getParent(), true); |
| 54 | } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) { |
| 55 | return new SlotCalculator(BB->getParent(), true); |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 56 | } else if (const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V)){ |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 57 | return new SlotCalculator(GV->getParent(), true); |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 58 | } else if (const Function *Func = dyn_cast<const Function>(V)) { |
| 59 | return new SlotCalculator(Func, true); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 60 | } else if (const Module *Mod = dyn_cast<const Module>(V)) { |
| 61 | return new SlotCalculator(Mod, true); |
| 62 | } |
| 63 | return 0; |
| 64 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 5e5abe3 | 2001-07-20 19:15:21 +0000 | [diff] [blame] | 66 | // WriteAsOperand - Write the name of the specified value out to the specified |
| 67 | // ostream. This can be useful when you just want to print int %reg126, not the |
| 68 | // whole instruction that generated it. |
| 69 | // |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 70 | static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName, |
| 71 | SlotCalculator *Table) { |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 72 | if (PrintName && V->hasName()) { |
Chris Lattner | 5e5abe3 | 2001-07-20 19:15:21 +0000 | [diff] [blame] | 73 | Out << " %" << V->getName(); |
| 74 | } else { |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 75 | if (const Constant *CPV = dyn_cast<const Constant>(V)) { |
Chris Lattner | 5e5abe3 | 2001-07-20 19:15:21 +0000 | [diff] [blame] | 76 | Out << " " << CPV->getStrValue(); |
| 77 | } else { |
| 78 | int Slot; |
| 79 | if (Table) { |
| 80 | Slot = Table->getValSlot(V); |
| 81 | } else { |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 82 | if (const Type *Ty = dyn_cast<const Type>(V)) { |
| 83 | Out << " " << Ty->getDescription(); |
| 84 | return; |
| 85 | } |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 86 | |
| 87 | Table = createSlotCalculator(V); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 88 | if (Table == 0) { Out << "BAD VALUE TYPE!"; return; } |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 5e5abe3 | 2001-07-20 19:15:21 +0000 | [diff] [blame] | 90 | Slot = Table->getValSlot(V); |
| 91 | delete Table; |
| 92 | } |
| 93 | if (Slot >= 0) Out << " %" << Slot; |
| 94 | else if (PrintName) |
| 95 | Out << "<badref>"; // Not embeded into a location? |
| 96 | } |
| 97 | } |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | |
| 101 | // If the module has a symbol table, take all global types and stuff their |
| 102 | // names into the TypeNames map. |
| 103 | // |
| 104 | static void fillTypeNameTable(const Module *M, |
| 105 | map<const Type *, string> &TypeNames) { |
| 106 | if (M && M->hasSymbolTable()) { |
| 107 | const SymbolTable *ST = M->getSymbolTable(); |
| 108 | SymbolTable::const_iterator PI = ST->find(Type::TypeTy); |
| 109 | if (PI != ST->end()) { |
| 110 | SymbolTable::type_const_iterator I = PI->second.begin(); |
| 111 | for (; I != PI->second.end(); ++I) { |
| 112 | // As a heuristic, don't insert pointer to primitive types, because |
| 113 | // they are used too often to have a single useful name. |
| 114 | // |
| 115 | const Type *Ty = cast<const Type>(I->second); |
| 116 | if (!isa<PointerType>(Ty) || |
Chris Lattner | 2413b16 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 117 | !cast<PointerType>(Ty)->getElementType()->isPrimitiveType()) |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 118 | TypeNames.insert(std::make_pair(Ty, "%"+I->first)); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | |
| 125 | |
| 126 | static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack, |
| 127 | map<const Type *, string> &TypeNames) { |
| 128 | if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case |
| 129 | |
| 130 | // Check to see if the type is named. |
| 131 | map<const Type *, string>::iterator I = TypeNames.find(Ty); |
| 132 | if (I != TypeNames.end()) return I->second; |
| 133 | |
| 134 | // Check to see if the Type is already on the stack... |
| 135 | unsigned Slot = 0, CurSize = TypeStack.size(); |
| 136 | while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type |
| 137 | |
| 138 | // This is another base case for the recursion. In this case, we know |
| 139 | // that we have looped back to a type that we have previously visited. |
| 140 | // Generate the appropriate upreference to handle this. |
| 141 | // |
| 142 | if (Slot < CurSize) |
| 143 | return "\\" + utostr(CurSize-Slot); // Here's the upreference |
| 144 | |
| 145 | TypeStack.push_back(Ty); // Recursive case: Add us to the stack.. |
| 146 | |
| 147 | string Result; |
| 148 | switch (Ty->getPrimitiveID()) { |
Chris Lattner | 91db582 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 149 | case Type::FunctionTyID: { |
| 150 | const FunctionType *MTy = cast<const FunctionType>(Ty); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 151 | Result = calcTypeName(MTy->getReturnType(), TypeStack, TypeNames) + " ("; |
Chris Lattner | 91db582 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 152 | for (FunctionType::ParamTypes::const_iterator |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 153 | I = MTy->getParamTypes().begin(), |
| 154 | E = MTy->getParamTypes().end(); I != E; ++I) { |
| 155 | if (I != MTy->getParamTypes().begin()) |
| 156 | Result += ", "; |
| 157 | Result += calcTypeName(*I, TypeStack, TypeNames); |
| 158 | } |
| 159 | if (MTy->isVarArg()) { |
| 160 | if (!MTy->getParamTypes().empty()) Result += ", "; |
| 161 | Result += "..."; |
| 162 | } |
| 163 | Result += ")"; |
| 164 | break; |
| 165 | } |
| 166 | case Type::StructTyID: { |
| 167 | const StructType *STy = cast<const StructType>(Ty); |
| 168 | Result = "{ "; |
| 169 | for (StructType::ElementTypes::const_iterator |
| 170 | I = STy->getElementTypes().begin(), |
| 171 | E = STy->getElementTypes().end(); I != E; ++I) { |
| 172 | if (I != STy->getElementTypes().begin()) |
| 173 | Result += ", "; |
| 174 | Result += calcTypeName(*I, TypeStack, TypeNames); |
| 175 | } |
| 176 | Result += " }"; |
| 177 | break; |
| 178 | } |
| 179 | case Type::PointerTyID: |
Chris Lattner | 2413b16 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 180 | Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(), |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 181 | TypeStack, TypeNames) + " *"; |
| 182 | break; |
| 183 | case Type::ArrayTyID: { |
| 184 | const ArrayType *ATy = cast<const ArrayType>(Ty); |
| 185 | int NumElements = ATy->getNumElements(); |
| 186 | Result = "["; |
| 187 | if (NumElements != -1) Result += itostr(NumElements) + " x "; |
| 188 | Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]"; |
| 189 | break; |
| 190 | } |
| 191 | default: |
| 192 | assert(0 && "Unhandled case in getTypeProps!"); |
| 193 | Result = "<error>"; |
| 194 | } |
| 195 | |
| 196 | TypeStack.pop_back(); // Remove self from stack... |
| 197 | return Result; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | // printTypeInt - The internal guts of printing out a type that has a |
| 202 | // potentially named portion. |
| 203 | // |
| 204 | static ostream &printTypeInt(ostream &Out, const Type *Ty, |
| 205 | map<const Type *, string> &TypeNames) { |
| 206 | // Primitive types always print out their description, regardless of whether |
| 207 | // they have been named or not. |
| 208 | // |
| 209 | if (Ty->isPrimitiveType()) return Out << Ty->getDescription(); |
| 210 | |
| 211 | // Check to see if the type is named. |
| 212 | map<const Type *, string>::iterator I = TypeNames.find(Ty); |
| 213 | if (I != TypeNames.end()) return Out << I->second; |
| 214 | |
| 215 | // Otherwise we have a type that has not been named but is a derived type. |
| 216 | // Carefully recurse the type hierarchy to print out any contained symbolic |
| 217 | // names. |
| 218 | // |
| 219 | vector<const Type *> TypeStack; |
| 220 | string TypeName = calcTypeName(Ty, TypeStack, TypeNames); |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 221 | 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] | 222 | return Out << TypeName; |
| 223 | } |
| 224 | |
Chris Lattner | 34b9518 | 2001-10-31 04:33:19 +0000 | [diff] [blame] | 225 | |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 226 | // WriteTypeSymbolic - This attempts to write the specified type as a symbolic |
| 227 | // type, iff there is an entry in the modules symbol table for the specified |
| 228 | // type or one of it's component types. This is slower than a simple x << Type; |
| 229 | // |
| 230 | ostream &WriteTypeSymbolic(ostream &Out, const Type *Ty, const Module *M) { |
| 231 | Out << " "; |
| 232 | |
| 233 | // If they want us to print out a type, attempt to make it symbolic if there |
| 234 | // is a symbol table in the module... |
| 235 | if (M && M->hasSymbolTable()) { |
| 236 | map<const Type *, string> TypeNames; |
| 237 | fillTypeNameTable(M, TypeNames); |
| 238 | |
Chris Lattner | 72f866e | 2001-10-29 16:40:32 +0000 | [diff] [blame] | 239 | return printTypeInt(Out, Ty, TypeNames); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 240 | } else { |
Chris Lattner | 72f866e | 2001-10-29 16:40:32 +0000 | [diff] [blame] | 241 | return Out << Ty->getDescription(); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
| 245 | |
| 246 | // WriteAsOperand - Write the name of the specified value out to the specified |
| 247 | // ostream. This can be useful when you just want to print int %reg126, not the |
| 248 | // whole instruction that generated it. |
| 249 | // |
| 250 | ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType, |
| 251 | bool PrintName, SlotCalculator *Table) { |
Chris Lattner | 72f866e | 2001-10-29 16:40:32 +0000 | [diff] [blame] | 252 | if (PrintType) |
| 253 | WriteTypeSymbolic(Out, V->getType(), getModuleFromVal(V)); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 254 | |
| 255 | WriteAsOperandInternal(Out, V, PrintName, Table); |
Chris Lattner | 5e5abe3 | 2001-07-20 19:15:21 +0000 | [diff] [blame] | 256 | return Out; |
| 257 | } |
| 258 | |
| 259 | |
Chris Lattner | 2e9fee4 | 2001-07-12 23:35:26 +0000 | [diff] [blame] | 260 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 261 | class AssemblyWriter { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 262 | ostream &Out; |
| 263 | SlotCalculator &Table; |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 264 | const Module *TheModule; |
| 265 | map<const Type *, string> TypeNames; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 266 | public: |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 267 | inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M) |
| 268 | : Out(o), Table(Tab), TheModule(M) { |
| 269 | |
| 270 | // If the module has a symbol table, take all global types and stuff their |
| 271 | // names into the TypeNames map. |
| 272 | // |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 273 | fillTypeNameTable(M, TypeNames); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 276 | inline void write(const Module *M) { printModule(M); } |
| 277 | inline void write(const GlobalVariable *G) { printGlobal(G); } |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 278 | inline void write(const Function *F) { printFunction(F); } |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 279 | inline void write(const BasicBlock *BB) { printBasicBlock(BB); } |
| 280 | inline void write(const Instruction *I) { printInstruction(I); } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 281 | inline void write(const Constant *CPV) { printConstant(CPV); } |
Chris Lattner | 7db7958 | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 282 | inline void write(const Type *Ty) { printType(Ty); } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 283 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 284 | private : |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 285 | void printModule(const Module *M); |
| 286 | void printSymbolTable(const SymbolTable &ST); |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 287 | void printConstant(const Constant *CPV); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 288 | void printGlobal(const GlobalVariable *GV); |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 289 | void printFunction(const Function *F); |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 290 | void printArgument(const Argument *FA); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 291 | void printBasicBlock(const BasicBlock *BB); |
| 292 | void printInstruction(const Instruction *I); |
| 293 | ostream &printType(const Type *Ty); |
| 294 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 295 | void writeOperand(const Value *Op, bool PrintType, bool PrintName = true); |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 296 | |
| 297 | // printInfoComment - Print a little comment after the instruction indicating |
| 298 | // which slot it occupies. |
| 299 | void printInfoComment(const Value *V); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 300 | }; |
| 301 | |
| 302 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 303 | void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType, |
| 304 | bool PrintName) { |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 305 | if (PrintType) { Out << " "; printType(Operand->getType()); } |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 306 | WriteAsOperandInternal(Out, Operand, PrintName, &Table); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 309 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 310 | void AssemblyWriter::printModule(const Module *M) { |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 311 | // Loop over the symbol table, emitting all named constants... |
| 312 | if (M->hasSymbolTable()) |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 313 | printSymbolTable(*M->getSymbolTable()); |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 314 | |
| 315 | for_each(M->gbegin(), M->gend(), |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 316 | bind_obj(this, &AssemblyWriter::printGlobal)); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 317 | |
Vikram S. Adve | 13ba19a | 2001-09-18 12:48:16 +0000 | [diff] [blame] | 318 | Out << "implementation\n"; |
| 319 | |
Chris Lattner | 6915f8f | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 320 | // Output all of the functions... |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 321 | for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::printFunction)); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 324 | void AssemblyWriter::printGlobal(const GlobalVariable *GV) { |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 325 | if (GV->hasName()) Out << "%" << GV->getName() << " = "; |
Chris Lattner | 3779864 | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 326 | |
Chris Lattner | 841d8b9 | 2001-11-26 18:54:16 +0000 | [diff] [blame] | 327 | if (GV->hasInternalLinkage()) Out << "internal "; |
Chris Lattner | 3779864 | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 328 | if (!GV->hasInitializer()) Out << "uninitialized "; |
| 329 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 330 | Out << (GV->isConstant() ? "constant " : "global "); |
Chris Lattner | 2413b16 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 331 | printType(GV->getType()->getElementType()); |
Chris Lattner | 3779864 | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 332 | |
| 333 | if (GV->hasInitializer()) |
| 334 | writeOperand(GV->getInitializer(), false, false); |
| 335 | |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 336 | printInfoComment(GV); |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 337 | Out << "\n"; |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 340 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 341 | // printSymbolTable - Run through symbol table looking for named constants |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 342 | // if a named constant is found, emit it's declaration... |
| 343 | // |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 344 | void AssemblyWriter::printSymbolTable(const SymbolTable &ST) { |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 345 | for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) { |
| 346 | SymbolTable::type_const_iterator I = ST.type_begin(TI->first); |
| 347 | SymbolTable::type_const_iterator End = ST.type_end(TI->first); |
| 348 | |
| 349 | for (; I != End; ++I) { |
| 350 | const Value *V = I->second; |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 351 | if (const Constant *CPV = dyn_cast<const Constant>(V)) { |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 352 | printConstant(CPV); |
Chris Lattner | 3856934 | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 353 | } else if (const Type *Ty = dyn_cast<const Type>(V)) { |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 354 | Out << "\t%" << I->first << " = type " << Ty->getDescription() << "\n"; |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
Chris Lattner | a7620d9 | 2001-07-15 06:35:59 +0000 | [diff] [blame] | 357 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 361 | // printConstant - Print out a constant pool entry... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 362 | // |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 363 | void AssemblyWriter::printConstant(const Constant *CPV) { |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 364 | // Don't print out unnamed constants, they will be inlined |
| 365 | if (!CPV->hasName()) return; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 366 | |
Chris Lattner | ee998be | 2001-07-26 16:29:38 +0000 | [diff] [blame] | 367 | // Print out name... |
| 368 | Out << "\t%" << CPV->getName() << " = "; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 369 | |
Chris Lattner | ee998be | 2001-07-26 16:29:38 +0000 | [diff] [blame] | 370 | // Print out the constant type... |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 371 | printType(CPV->getType()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 372 | |
| 373 | // Write the value out now... |
| 374 | writeOperand(CPV, false, false); |
| 375 | |
| 376 | if (!CPV->hasName() && CPV->getType() != Type::VoidTy) { |
| 377 | int Slot = Table.getValSlot(CPV); // Print out the def slot taken... |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 378 | Out << "\t\t; <"; |
| 379 | printType(CPV->getType()) << ">:"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 380 | if (Slot >= 0) Out << Slot; |
| 381 | else Out << "<badref>"; |
| 382 | } |
| 383 | |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 384 | Out << "\n"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Chris Lattner | 6915f8f | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 387 | // printFunction - Print all aspects of a function. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 388 | // |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 389 | void AssemblyWriter::printFunction(const Function *M) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 390 | // Print out the return type and name... |
Chris Lattner | 841d8b9 | 2001-11-26 18:54:16 +0000 | [diff] [blame] | 391 | Out << "\n" << (M->isExternal() ? "declare " : "") |
| 392 | << (M->hasInternalLinkage() ? "internal " : ""); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 393 | printType(M->getReturnType()) << " \"" << M->getName() << "\"("; |
Chris Lattner | 6915f8f | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 394 | Table.incorporateFunction(M); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 395 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 396 | // Loop over the arguments, printing them... |
Chris Lattner | 91db582 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 397 | const FunctionType *MT = M->getFunctionType(); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 398 | |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 399 | if (!M->isExternal()) { |
| 400 | for_each(M->getArgumentList().begin(), M->getArgumentList().end(), |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 401 | bind_obj(this, &AssemblyWriter::printArgument)); |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 402 | } else { |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 403 | // Loop over the arguments, printing them... |
Chris Lattner | 91db582 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 404 | const FunctionType *MT = M->getFunctionType(); |
| 405 | for (FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin(), |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 406 | E = MT->getParamTypes().end(); I != E; ++I) { |
| 407 | if (I != MT->getParamTypes().begin()) Out << ", "; |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 408 | printType(*I); |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 411 | |
| 412 | // Finish printing arguments... |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 413 | if (MT->isVarArg()) { |
| 414 | if (MT->getParamTypes().size()) Out << ", "; |
| 415 | Out << "..."; // Output varargs portion of signature! |
| 416 | } |
| 417 | Out << ")\n"; |
| 418 | |
| 419 | if (!M->isExternal()) { |
| 420 | // Loop over the symbol table, emitting all named constants... |
| 421 | if (M->hasSymbolTable()) |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 422 | printSymbolTable(*M->getSymbolTable()); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 423 | |
| 424 | Out << "begin"; |
| 425 | |
Chris Lattner | 6915f8f | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 426 | // Output all of its basic blocks... for the function |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 427 | for_each(M->begin(), M->end(), |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 428 | bind_obj(this, &AssemblyWriter::printBasicBlock)); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 429 | |
Chris Lattner | a7620d9 | 2001-07-15 06:35:59 +0000 | [diff] [blame] | 430 | Out << "end\n"; |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Chris Lattner | 6915f8f | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 433 | Table.purgeFunction(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 436 | // printArgument - This member is called for every argument that |
Chris Lattner | 6915f8f | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 437 | // is passed into the function. Simply print it out |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 438 | // |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 439 | void AssemblyWriter::printArgument(const Argument *Arg) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 440 | // Insert commas as we go... the first arg doesn't get a comma |
| 441 | if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", "; |
| 442 | |
| 443 | // Output type... |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 444 | printType(Arg->getType()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 445 | |
| 446 | // Output name, if available... |
| 447 | if (Arg->hasName()) |
| 448 | Out << " %" << Arg->getName(); |
| 449 | else if (Table.getValSlot(Arg) < 0) |
| 450 | Out << "<badref>"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 453 | // printBasicBlock - This member is called for each basic block in a methd. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 454 | // |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 455 | void AssemblyWriter::printBasicBlock(const BasicBlock *BB) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 456 | if (BB->hasName()) { // Print out the label if it exists... |
Chris Lattner | a2f0187 | 2001-06-07 16:58:55 +0000 | [diff] [blame] | 457 | Out << "\n" << BB->getName() << ":"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 458 | } else { |
| 459 | int Slot = Table.getValSlot(BB); |
Chris Lattner | a2f0187 | 2001-06-07 16:58:55 +0000 | [diff] [blame] | 460 | Out << "\n; <label>:"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 461 | if (Slot >= 0) |
Chris Lattner | a2f0187 | 2001-06-07 16:58:55 +0000 | [diff] [blame] | 462 | Out << Slot; // Extra newline seperates out label's |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 463 | else |
Chris Lattner | a2f0187 | 2001-06-07 16:58:55 +0000 | [diff] [blame] | 464 | Out << "<badref>"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 465 | } |
Chris Lattner | a2f0187 | 2001-06-07 16:58:55 +0000 | [diff] [blame] | 466 | 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] | 467 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 468 | // Output all of the instructions in the basic block... |
| 469 | for_each(BB->begin(), BB->end(), |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 470 | bind_obj(this, &AssemblyWriter::printInstruction)); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 473 | |
| 474 | // printInfoComment - Print a little comment after the instruction indicating |
| 475 | // which slot it occupies. |
| 476 | // |
| 477 | void AssemblyWriter::printInfoComment(const Value *V) { |
| 478 | if (V->getType() != Type::VoidTy) { |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 479 | Out << "\t\t; <"; |
| 480 | printType(V->getType()) << ">"; |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 481 | |
| 482 | if (!V->hasName()) { |
| 483 | int Slot = Table.getValSlot(V); // Print out the def slot taken... |
| 484 | if (Slot >= 0) Out << ":" << Slot; |
| 485 | else Out << ":<badref>"; |
| 486 | } |
Chris Lattner | 3cb3a1f | 2001-12-14 16:29:12 +0000 | [diff] [blame] | 487 | Out << " [#uses=" << V->use_size() << "]"; // Output # uses |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 491 | // printInstruction - This member is called for each Instruction in a methd. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 492 | // |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 493 | void AssemblyWriter::printInstruction(const Instruction *I) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 494 | Out << "\t"; |
| 495 | |
| 496 | // Print out name if it exists... |
| 497 | if (I && I->hasName()) |
| 498 | Out << "%" << I->getName() << " = "; |
| 499 | |
| 500 | // Print out the opcode... |
Chris Lattner | b1ca9cb | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 501 | Out << I->getOpcodeName(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 502 | |
| 503 | // Print out the type of the operands... |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 504 | const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 505 | |
| 506 | // Special case conditional branches to swizzle the condition out to the front |
Chris Lattner | b1ca9cb | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 507 | if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 508 | writeOperand(I->getOperand(2), true); |
| 509 | Out << ","; |
| 510 | writeOperand(Operand, true); |
| 511 | Out << ","; |
| 512 | writeOperand(I->getOperand(1), true); |
| 513 | |
Chris Lattner | b1ca9cb | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 514 | } else if (I->getOpcode() == Instruction::Switch) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 515 | // Special case switch statement to get formatting nice and correct... |
| 516 | writeOperand(Operand , true); Out << ","; |
| 517 | writeOperand(I->getOperand(1), true); Out << " ["; |
| 518 | |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 519 | for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 520 | Out << "\n\t\t"; |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 521 | writeOperand(I->getOperand(op ), true); Out << ","; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 522 | writeOperand(I->getOperand(op+1), true); |
| 523 | } |
| 524 | Out << "\n\t]"; |
Chris Lattner | da55810 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 525 | } else if (isa<PHINode>(I)) { |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 526 | Out << " "; |
Chris Lattner | 29644b3 | 2001-11-06 01:48:45 +0000 | [diff] [blame] | 527 | printType(I->getType()); |
Chris Lattner | 9d3292d | 2001-11-06 08:33:46 +0000 | [diff] [blame] | 528 | Out << " "; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 529 | |
Chris Lattner | 29644b3 | 2001-11-06 01:48:45 +0000 | [diff] [blame] | 530 | for (unsigned op = 0, Eop = I->getNumOperands(); op < Eop; op += 2) { |
| 531 | if (op) Out << ", "; |
| 532 | Out << "["; |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 533 | writeOperand(I->getOperand(op ), false); Out << ","; |
Chris Lattner | 4b94e23 | 2001-06-21 05:29:56 +0000 | [diff] [blame] | 534 | writeOperand(I->getOperand(op+1), false); Out << " ]"; |
Chris Lattner | 931ef3b | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 535 | } |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 536 | } else if (isa<ReturnInst>(I) && !Operand) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 537 | Out << " void"; |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 538 | } else if (isa<CallInst>(I)) { |
Chris Lattner | 2f2d947 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 539 | const PointerType *PTy = dyn_cast<PointerType>(Operand->getType()); |
Chris Lattner | 91db582 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 540 | const FunctionType*MTy = PTy ? dyn_cast<FunctionType>(PTy->getElementType()):0; |
Chris Lattner | 2f2d947 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 541 | const Type *RetTy = MTy ? MTy->getReturnType() : 0; |
| 542 | |
| 543 | // 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] | 544 | // only do this if the first argument is a pointer to a nonvararg function, |
| 545 | // and if the value returned is not a pointer to a function. |
Chris Lattner | 2f2d947 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 546 | // |
| 547 | if (RetTy && !MTy->isVarArg() && |
Chris Lattner | 91db582 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 548 | (!isa<PointerType>(RetTy)||!isa<FunctionType>(cast<PointerType>(RetTy)))){ |
Chris Lattner | 2f2d947 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 549 | Out << " "; printType(RetTy); |
| 550 | writeOperand(Operand, false); |
| 551 | } else { |
| 552 | writeOperand(Operand, true); |
| 553 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 554 | Out << "("; |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 555 | if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true); |
| 556 | for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 557 | Out << ","; |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 558 | writeOperand(I->getOperand(op), true); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | Out << " )"; |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 562 | } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) { |
| 563 | // TODO: Should try to print out short form of the Invoke instruction |
| 564 | writeOperand(Operand, true); |
| 565 | Out << "("; |
| 566 | if (I->getNumOperands() > 3) writeOperand(I->getOperand(3), true); |
| 567 | for (unsigned op = 4, Eop = I->getNumOperands(); op < Eop; ++op) { |
| 568 | Out << ","; |
| 569 | writeOperand(I->getOperand(op), true); |
| 570 | } |
| 571 | |
| 572 | Out << " )\n\t\t\tto"; |
| 573 | writeOperand(II->getNormalDest(), true); |
| 574 | Out << " except"; |
| 575 | writeOperand(II->getExceptionalDest(), true); |
| 576 | |
Chris Lattner | b1ca9cb | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 577 | } else if (I->getOpcode() == Instruction::Malloc || |
| 578 | I->getOpcode() == Instruction::Alloca) { |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 579 | Out << " "; |
Chris Lattner | 2413b16 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 580 | printType(cast<const PointerType>(I->getType())->getElementType()); |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 581 | if (I->getNumOperands()) { |
| 582 | Out << ","; |
| 583 | writeOperand(I->getOperand(0), true); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 584 | } |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 585 | } else if (isa<CastInst>(I)) { |
Chris Lattner | a682182 | 2001-07-08 04:57:15 +0000 | [diff] [blame] | 586 | writeOperand(Operand, true); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 587 | Out << " to "; |
| 588 | printType(I->getType()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 589 | } else if (Operand) { // Print the normal way... |
| 590 | |
| 591 | // PrintAllTypes - Instructions who have operands of all the same type |
| 592 | // omit the type from all but the first operand. If the instruction has |
| 593 | // different type operands (for example br), then they are all printed. |
| 594 | bool PrintAllTypes = false; |
| 595 | const Type *TheType = Operand->getType(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 596 | |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 597 | for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) { |
| 598 | Operand = I->getOperand(i); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 599 | if (Operand->getType() != TheType) { |
| 600 | PrintAllTypes = true; // We have differing types! Print them all! |
| 601 | break; |
| 602 | } |
| 603 | } |
| 604 | |
Chris Lattner | b6fe234 | 2001-10-20 09:33:10 +0000 | [diff] [blame] | 605 | // Shift Left & Right print both types even for Ubyte LHS |
| 606 | if (isa<ShiftInst>(I)) PrintAllTypes = true; |
| 607 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 608 | if (!PrintAllTypes) { |
| 609 | Out << " "; |
| 610 | printType(I->getOperand(0)->getType()); |
| 611 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 612 | |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 613 | for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 614 | if (i) Out << ","; |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 615 | writeOperand(I->getOperand(i), PrintAllTypes); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 619 | printInfoComment(I); |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 620 | Out << "\n"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 624 | // printType - Go to extreme measures to attempt to print out a short, symbolic |
| 625 | // version of a type name. |
| 626 | // |
| 627 | ostream &AssemblyWriter::printType(const Type *Ty) { |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 628 | return printTypeInt(Out, Ty, TypeNames); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 632 | //===----------------------------------------------------------------------===// |
| 633 | // External Interface declarations |
| 634 | //===----------------------------------------------------------------------===// |
| 635 | |
| 636 | |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 637 | void Module::print(std::ostream &o) const { |
| 638 | SlotCalculator SlotTable(this, true); |
| 639 | AssemblyWriter W(o, SlotTable, this); |
| 640 | W.write(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 643 | void GlobalVariable::print(std::ostream &o) const { |
| 644 | SlotCalculator SlotTable(getParent(), true); |
| 645 | AssemblyWriter W(o, SlotTable, getParent()); |
| 646 | W.write(this); |
Chris Lattner | adfe0d1 | 2001-09-10 20:08:19 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 649 | void Function::print(std::ostream &o) const { |
| 650 | SlotCalculator SlotTable(getParent(), true); |
| 651 | AssemblyWriter W(o, SlotTable, getParent()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 652 | |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 653 | W.write(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 656 | void BasicBlock::print(std::ostream &o) const { |
| 657 | SlotCalculator SlotTable(getParent(), true); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 658 | AssemblyWriter W(o, SlotTable, |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 659 | getParent() ? getParent()->getParent() : 0); |
| 660 | W.write(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 663 | void Instruction::print(std::ostream &o) const { |
| 664 | const Function *F = getParent() ? getParent()->getParent() : 0; |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 665 | SlotCalculator SlotTable(F, true); |
| 666 | AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 667 | |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 668 | W.write(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 669 | } |
Chris Lattner | 7db7958 | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 670 | |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 671 | void Constant::print(std::ostream &o) const { |
| 672 | if (this == 0) { o << "<null> constant value\n"; return; } |
| 673 | o << " " << getType()->getDescription() << " " << getStrValue(); |
| 674 | } |
| 675 | |
| 676 | void Type::print(std::ostream &o) const { |
| 677 | if (this == 0) |
| 678 | o << "<null Type>"; |
| 679 | else |
| 680 | o << getDescription(); |
| 681 | } |
| 682 | |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 683 | void Argument::print(std::ostream &o) const { |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 684 | o << getType() << " " << getName(); |
| 685 | } |
| 686 | |
| 687 | void Value::dump() const { print(std::cerr); } |
| 688 | |
| 689 | //===----------------------------------------------------------------------===// |
| 690 | // CachedWriter Class Implementation |
| 691 | //===----------------------------------------------------------------------===// |
| 692 | |
Chris Lattner | 7db7958 | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 693 | void CachedWriter::setModule(const Module *M) { |
| 694 | delete SC; delete AW; |
| 695 | if (M) { |
| 696 | SC = new SlotCalculator(M, true); |
| 697 | AW = new AssemblyWriter(Out, *SC, M); |
| 698 | } else { |
| 699 | SC = 0; AW = 0; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | CachedWriter::~CachedWriter() { |
| 704 | delete AW; |
| 705 | delete SC; |
| 706 | } |
| 707 | |
| 708 | CachedWriter &CachedWriter::operator<<(const Value *V) { |
| 709 | assert(AW && SC && "CachedWriter does not have a current module!"); |
| 710 | switch (V->getValueType()) { |
| 711 | case Value::ConstantVal: |
| 712 | Out << " "; AW->write(V->getType()); |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 713 | Out << " " << cast<Constant>(V)->getStrValue(); break; |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 714 | case Value::ArgumentVal: |
Chris Lattner | 7db7958 | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 715 | AW->write(V->getType()); Out << " " << V->getName(); break; |
| 716 | case Value::TypeVal: AW->write(cast<const Type>(V)); break; |
| 717 | case Value::InstructionVal: AW->write(cast<Instruction>(V)); break; |
| 718 | case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break; |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 719 | case Value::FunctionVal: AW->write(cast<Function>(V)); break; |
Chris Lattner | 7db7958 | 2001-11-07 04:21:57 +0000 | [diff] [blame] | 720 | case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break; |
| 721 | case Value::ModuleVal: AW->write(cast<Module>(V)); break; |
| 722 | default: Out << "<unknown value type: " << V->getValueType() << ">"; break; |
| 723 | } |
| 724 | return *this; |
| 725 | } |