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