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