Chris Lattner | 3af3ba8 | 2002-05-09 21:31:18 +0000 | [diff] [blame] | 1 | //===-- Writer.cpp - Library for converting LLVM code to C ----------------===// |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | d1cf1b4 | 2002-09-20 23:26:33 +0000 | [diff] [blame] | 3 | // This library converts LLVM code to C code, compilable by GCC. |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 4 | // |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 5 | //===-----------------------------------------------------------------------==// |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 6 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 7 | #include "llvm/Assembly/CWriter.h" |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 8 | #include "llvm/Constants.h" |
Chris Lattner | 2f5f51a | 2002-05-09 03:28:37 +0000 | [diff] [blame] | 9 | #include "llvm/DerivedTypes.h" |
| 10 | #include "llvm/Module.h" |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 11 | #include "llvm/iMemory.h" |
| 12 | #include "llvm/iTerminators.h" |
| 13 | #include "llvm/iPHINode.h" |
| 14 | #include "llvm/iOther.h" |
Chris Lattner | 2f5f51a | 2002-05-09 03:28:37 +0000 | [diff] [blame] | 15 | #include "llvm/iOperators.h" |
Chris Lattner | 0e9f93e | 2002-08-31 00:29:16 +0000 | [diff] [blame] | 16 | #include "llvm/Pass.h" |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 17 | #include "llvm/SymbolTable.h" |
Chris Lattner | 3af3ba8 | 2002-05-09 21:31:18 +0000 | [diff] [blame] | 18 | #include "llvm/SlotCalculator.h" |
Chris Lattner | 0e9f93e | 2002-08-31 00:29:16 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/FindUsedTypes.h" |
Chris Lattner | d1cf1b4 | 2002-09-20 23:26:33 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/ConstantsScanner.h" |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 21 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | 7683a12 | 2002-05-09 20:33:35 +0000 | [diff] [blame] | 22 | #include "llvm/Support/InstIterator.h" |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 23 | #include "Support/StringExtras.h" |
| 24 | #include "Support/STLExtras.h" |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Chris Lattner | 78771c8 | 2002-05-17 04:55:35 +0000 | [diff] [blame] | 26 | #include <set> |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 27 | using std::string; |
| 28 | using std::map; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 29 | using std::ostream; |
| 30 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 31 | namespace { |
Chris Lattner | 0e9f93e | 2002-08-31 00:29:16 +0000 | [diff] [blame] | 32 | class CWriter : public Pass, public InstVisitor<CWriter> { |
| 33 | ostream &Out; |
| 34 | SlotCalculator *Table; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 35 | const Module *TheModule; |
| 36 | map<const Type *, string> TypeNames; |
Chris Lattner | 78771c8 | 2002-05-17 04:55:35 +0000 | [diff] [blame] | 37 | std::set<const Value*> MangledGlobals; |
Nick Hildenbrandt | 67aa2e2 | 2002-09-14 21:36:24 +0000 | [diff] [blame] | 38 | |
Chris Lattner | d1cf1b4 | 2002-09-20 23:26:33 +0000 | [diff] [blame] | 39 | map<const ConstantFP *, unsigned> FPConstantMap; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 40 | public: |
Chris Lattner | 0e9f93e | 2002-08-31 00:29:16 +0000 | [diff] [blame] | 41 | CWriter(ostream &o) : Out(o) {} |
| 42 | |
| 43 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 44 | AU.setPreservesAll(); |
| 45 | AU.addRequired<FindUsedTypes>(); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 46 | } |
Chris Lattner | 0e9f93e | 2002-08-31 00:29:16 +0000 | [diff] [blame] | 47 | |
| 48 | virtual bool run(Module &M) { |
| 49 | // Initialize |
| 50 | Table = new SlotCalculator(&M, false); |
| 51 | TheModule = &M; |
| 52 | |
| 53 | // Ensure that all structure types have names... |
| 54 | bool Changed = nameAllUsedStructureTypes(M); |
| 55 | |
| 56 | // Run... |
| 57 | printModule(&M); |
| 58 | |
| 59 | // Free memory... |
| 60 | delete Table; |
| 61 | TypeNames.clear(); |
| 62 | MangledGlobals.clear(); |
| 63 | return false; |
| 64 | } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame] | 66 | ostream &printType(const Type *Ty, const string &VariableName = "", |
Vikram S. Adve | 969c4ad | 2002-08-25 20:00:08 +0000 | [diff] [blame] | 67 | bool IgnoreName = false, bool namedContext = true); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 68 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 69 | void writeOperand(Value *Operand); |
| 70 | void writeOperandInternal(Value *Operand); |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 71 | |
| 72 | string getValueName(const Value *V); |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 74 | private : |
Chris Lattner | 0e9f93e | 2002-08-31 00:29:16 +0000 | [diff] [blame] | 75 | bool nameAllUsedStructureTypes(Module &M); |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 76 | void printModule(Module *M); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 77 | void printSymbolTable(const SymbolTable &ST); |
Chris Lattner | 2c601a7 | 2002-09-20 15:05:40 +0000 | [diff] [blame] | 78 | void printContainedStructs(const Type *Ty, std::set<const StructType *> &); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 79 | void printGlobal(const GlobalVariable *GV); |
Chris Lattner | 4cda835 | 2002-08-20 16:55:48 +0000 | [diff] [blame] | 80 | void printFunctionSignature(const Function *F, bool Prototype); |
| 81 | |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 82 | void printFunction(Function *); |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 83 | |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 84 | void printConstant(Constant *CPV); |
| 85 | void printConstantArray(ConstantArray *CPA); |
| 86 | |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 87 | // isInlinableInst - Attempt to inline instructions into their uses to build |
| 88 | // trees as much as possible. To do this, we have to consistently decide |
| 89 | // what is acceptable to inline, so that variable declarations don't get |
| 90 | // printed and an extra copy of the expr is not emitted. |
| 91 | // |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 92 | static bool isInlinableInst(const Instruction &I) { |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 93 | // Must be an expression, must be used exactly once. If it is dead, we |
| 94 | // emit it inline where it would go. |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 95 | if (I.getType() == Type::VoidTy || I.use_size() != 1 || |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 96 | isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I)) |
| 97 | return false; |
| 98 | |
| 99 | // Only inline instruction it it's use is in the same BB as the inst. |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 100 | return I.getParent() == cast<Instruction>(I.use_back())->getParent(); |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 103 | // Instruction visitation functions |
| 104 | friend class InstVisitor<CWriter>; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 105 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 106 | void visitReturnInst(ReturnInst &I); |
| 107 | void visitBranchInst(BranchInst &I); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 108 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 109 | void visitPHINode(PHINode &I) {} |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 110 | void visitBinaryOperator(Instruction &I); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 111 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 112 | void visitCastInst (CastInst &I); |
| 113 | void visitCallInst (CallInst &I); |
| 114 | void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); } |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 115 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 116 | void visitMallocInst(MallocInst &I); |
| 117 | void visitAllocaInst(AllocaInst &I); |
| 118 | void visitFreeInst (FreeInst &I); |
| 119 | void visitLoadInst (LoadInst &I); |
| 120 | void visitStoreInst (StoreInst &I); |
| 121 | void visitGetElementPtrInst(GetElementPtrInst &I); |
Chris Lattner | 2f5f51a | 2002-05-09 03:28:37 +0000 | [diff] [blame] | 122 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 123 | void visitInstruction(Instruction &I) { |
Chris Lattner | e7f65d3b | 2002-06-30 16:07:20 +0000 | [diff] [blame] | 124 | std::cerr << "C Writer does not know about " << I; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 125 | abort(); |
| 126 | } |
| 127 | |
| 128 | void outputLValue(Instruction *I) { |
| 129 | Out << " " << getValueName(I) << " = "; |
| 130 | } |
| 131 | void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock, |
| 132 | unsigned Indent); |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 133 | void printIndexingExpression(Value *Ptr, User::op_iterator I, |
| 134 | User::op_iterator E); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 135 | }; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Chris Lattner | 2f49902 | 2002-05-09 04:21:21 +0000 | [diff] [blame] | 138 | // We dont want identifier names with ., space, - in them. |
| 139 | // So we replace them with _ |
| 140 | static string makeNameProper(string x) { |
| 141 | string tmp; |
| 142 | for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++) |
| 143 | switch (*sI) { |
Chris Lattner | deed7a5 | 2002-05-09 15:18:52 +0000 | [diff] [blame] | 144 | case '.': tmp += "d_"; break; |
| 145 | case ' ': tmp += "s_"; break; |
| 146 | case '-': tmp += "D_"; break; |
Chris Lattner | 2f49902 | 2002-05-09 04:21:21 +0000 | [diff] [blame] | 147 | default: tmp += *sI; |
| 148 | } |
| 149 | |
| 150 | return tmp; |
| 151 | } |
| 152 | |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 153 | string CWriter::getValueName(const Value *V) { |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 154 | if (V->hasName()) { // Print out the label if it exists... |
| 155 | if (isa<GlobalValue>(V) && // Do not mangle globals... |
| 156 | cast<GlobalValue>(V)->hasExternalLinkage() && // Unless it's internal or |
| 157 | !MangledGlobals.count(V)) // Unless the name would collide if we don't |
Chris Lattner | 2f49902 | 2002-05-09 04:21:21 +0000 | [diff] [blame] | 158 | return makeNameProper(V->getName()); |
| 159 | |
Chris Lattner | 2d05a1a | 2002-05-09 05:16:40 +0000 | [diff] [blame] | 160 | return "l" + utostr(V->getType()->getUniqueID()) + "_" + |
| 161 | makeNameProper(V->getName()); |
Chris Lattner | 2f49902 | 2002-05-09 04:21:21 +0000 | [diff] [blame] | 162 | } |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 163 | |
Chris Lattner | 0e9f93e | 2002-08-31 00:29:16 +0000 | [diff] [blame] | 164 | int Slot = Table->getValSlot(V); |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 165 | assert(Slot >= 0 && "Invalid value!"); |
Chris Lattner | 2d05a1a | 2002-05-09 05:16:40 +0000 | [diff] [blame] | 166 | return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID()); |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Vikram S. Adve | 969c4ad | 2002-08-25 20:00:08 +0000 | [diff] [blame] | 169 | // A pointer type should not use parens around *'s alone, e.g., (**) |
| 170 | inline bool ptrTypeNameNeedsParens(const string &NameSoFar) { |
| 171 | return (NameSoFar.find_last_not_of('*') != std::string::npos); |
| 172 | } |
| 173 | |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame] | 174 | // Pass the Type* and the variable name and this prints out the variable |
| 175 | // declaration. |
| 176 | // |
| 177 | ostream &CWriter::printType(const Type *Ty, const string &NameSoFar, |
Vikram S. Adve | 969c4ad | 2002-08-25 20:00:08 +0000 | [diff] [blame] | 178 | bool IgnoreName, bool namedContext) { |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame] | 179 | if (Ty->isPrimitiveType()) |
| 180 | switch (Ty->getPrimitiveID()) { |
| 181 | case Type::VoidTyID: return Out << "void " << NameSoFar; |
| 182 | case Type::BoolTyID: return Out << "bool " << NameSoFar; |
| 183 | case Type::UByteTyID: return Out << "unsigned char " << NameSoFar; |
| 184 | case Type::SByteTyID: return Out << "signed char " << NameSoFar; |
| 185 | case Type::UShortTyID: return Out << "unsigned short " << NameSoFar; |
| 186 | case Type::ShortTyID: return Out << "short " << NameSoFar; |
| 187 | case Type::UIntTyID: return Out << "unsigned " << NameSoFar; |
| 188 | case Type::IntTyID: return Out << "int " << NameSoFar; |
| 189 | case Type::ULongTyID: return Out << "unsigned long long " << NameSoFar; |
| 190 | case Type::LongTyID: return Out << "signed long long " << NameSoFar; |
| 191 | case Type::FloatTyID: return Out << "float " << NameSoFar; |
| 192 | case Type::DoubleTyID: return Out << "double " << NameSoFar; |
| 193 | default : |
| 194 | std::cerr << "Unknown primitive type: " << Ty << "\n"; |
| 195 | abort(); |
| 196 | } |
| 197 | |
| 198 | // Check to see if the type is named. |
| 199 | if (!IgnoreName) { |
| 200 | map<const Type *, string>::iterator I = TypeNames.find(Ty); |
| 201 | if (I != TypeNames.end()) { |
| 202 | return Out << I->second << " " << NameSoFar; |
| 203 | } |
| 204 | } |
| 205 | |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame] | 206 | switch (Ty->getPrimitiveID()) { |
| 207 | case Type::FunctionTyID: { |
| 208 | const FunctionType *MTy = cast<FunctionType>(Ty); |
| 209 | printType(MTy->getReturnType(), ""); |
| 210 | Out << " " << NameSoFar << " ("; |
| 211 | |
| 212 | for (FunctionType::ParamTypes::const_iterator |
| 213 | I = MTy->getParamTypes().begin(), |
| 214 | E = MTy->getParamTypes().end(); I != E; ++I) { |
| 215 | if (I != MTy->getParamTypes().begin()) |
| 216 | Out << ", "; |
| 217 | printType(*I, ""); |
| 218 | } |
| 219 | if (MTy->isVarArg()) { |
| 220 | if (!MTy->getParamTypes().empty()) |
| 221 | Out << ", "; |
| 222 | Out << "..."; |
| 223 | } |
| 224 | return Out << ")"; |
| 225 | } |
| 226 | case Type::StructTyID: { |
| 227 | const StructType *STy = cast<StructType>(Ty); |
| 228 | Out << NameSoFar + " {\n"; |
| 229 | unsigned Idx = 0; |
| 230 | for (StructType::ElementTypes::const_iterator |
| 231 | I = STy->getElementTypes().begin(), |
| 232 | E = STy->getElementTypes().end(); I != E; ++I) { |
| 233 | Out << " "; |
| 234 | printType(*I, "field" + utostr(Idx++)); |
| 235 | Out << ";\n"; |
| 236 | } |
| 237 | return Out << "}"; |
| 238 | } |
| 239 | |
| 240 | case Type::PointerTyID: { |
| 241 | const PointerType *PTy = cast<PointerType>(Ty); |
Vikram S. Adve | 969c4ad | 2002-08-25 20:00:08 +0000 | [diff] [blame] | 242 | std::string ptrName = "*" + NameSoFar; |
| 243 | |
| 244 | // Do not need parens around "* NameSoFar" if NameSoFar consists only |
| 245 | // of zero or more '*' chars *and* this is not an unnamed pointer type |
| 246 | // such as the result type in a cast statement. Otherwise, enclose in ( ). |
Nick Hildenbrandt | c14ded4 | 2002-09-23 21:02:50 +0000 | [diff] [blame^] | 247 | if (ptrTypeNameNeedsParens(NameSoFar) || !namedContext || |
| 248 | PTy->getElementType()->getPrimitiveID() == Type::ArrayTyID) |
Vikram S. Adve | 969c4ad | 2002-08-25 20:00:08 +0000 | [diff] [blame] | 249 | ptrName = "(" + ptrName + ")"; // |
| 250 | |
Vikram S. Adve | 42eb2ba | 2002-08-24 14:44:23 +0000 | [diff] [blame] | 251 | return printType(PTy->getElementType(), ptrName); |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | case Type::ArrayTyID: { |
| 255 | const ArrayType *ATy = cast<ArrayType>(Ty); |
| 256 | unsigned NumElements = ATy->getNumElements(); |
| 257 | return printType(ATy->getElementType(), |
| 258 | NameSoFar + "[" + utostr(NumElements) + "]"); |
| 259 | } |
| 260 | default: |
| 261 | assert(0 && "Unhandled case in getTypeProps!"); |
| 262 | abort(); |
| 263 | } |
| 264 | |
| 265 | return Out; |
| 266 | } |
| 267 | |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 268 | void CWriter::printConstantArray(ConstantArray *CPA) { |
| 269 | |
| 270 | // As a special case, print the array as a string if it is an array of |
| 271 | // ubytes or an array of sbytes with positive values. |
| 272 | // |
| 273 | const Type *ETy = CPA->getType()->getElementType(); |
| 274 | bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy); |
| 275 | |
| 276 | // Make sure the last character is a null char, as automatically added by C |
| 277 | if (CPA->getNumOperands() == 0 || |
| 278 | !cast<Constant>(*(CPA->op_end()-1))->isNullValue()) |
| 279 | isString = false; |
| 280 | |
| 281 | if (isString) { |
| 282 | Out << "\""; |
| 283 | // Do not include the last character, which we know is null |
| 284 | for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) { |
| 285 | unsigned char C = (ETy == Type::SByteTy) ? |
| 286 | (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() : |
| 287 | (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue(); |
| 288 | |
| 289 | if (isprint(C)) { |
| 290 | Out << C; |
| 291 | } else { |
| 292 | switch (C) { |
| 293 | case '\n': Out << "\\n"; break; |
| 294 | case '\t': Out << "\\t"; break; |
| 295 | case '\r': Out << "\\r"; break; |
| 296 | case '\v': Out << "\\v"; break; |
| 297 | case '\a': Out << "\\a"; break; |
| 298 | default: |
| 299 | Out << "\\x"; |
| 300 | Out << ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'); |
| 301 | Out << ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'); |
| 302 | break; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | Out << "\""; |
| 307 | } else { |
| 308 | Out << "{"; |
| 309 | if (CPA->getNumOperands()) { |
| 310 | Out << " "; |
| 311 | printConstant(cast<Constant>(CPA->getOperand(0))); |
| 312 | for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) { |
| 313 | Out << ", "; |
| 314 | printConstant(cast<Constant>(CPA->getOperand(i))); |
| 315 | } |
| 316 | } |
| 317 | Out << " }"; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | |
| 322 | // printConstant - The LLVM Constant to C Constant converter. |
| 323 | void CWriter::printConstant(Constant *CPV) { |
| 324 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) { |
| 325 | switch (CE->getOpcode()) { |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 326 | case Instruction::Cast: |
| 327 | Out << "(("; |
| 328 | printType(CPV->getType()); |
| 329 | Out << ")"; |
| 330 | printConstant(cast<Constant>(CPV->getOperand(0))); |
| 331 | Out << ")"; |
| 332 | return; |
| 333 | |
| 334 | case Instruction::GetElementPtr: |
| 335 | Out << "&("; |
| 336 | printIndexingExpression(CPV->getOperand(0), |
| 337 | CPV->op_begin()+1, CPV->op_end()); |
| 338 | Out << ")"; |
| 339 | return; |
| 340 | case Instruction::Add: |
| 341 | Out << "("; |
| 342 | printConstant(cast<Constant>(CPV->getOperand(0))); |
| 343 | Out << " + "; |
| 344 | printConstant(cast<Constant>(CPV->getOperand(1))); |
| 345 | Out << ")"; |
| 346 | return; |
| 347 | case Instruction::Sub: |
| 348 | Out << "("; |
| 349 | printConstant(cast<Constant>(CPV->getOperand(0))); |
| 350 | Out << " - "; |
| 351 | printConstant(cast<Constant>(CPV->getOperand(1))); |
| 352 | Out << ")"; |
| 353 | return; |
| 354 | |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 355 | default: |
| 356 | std::cerr << "CWriter Error: Unhandled constant expression: " |
| 357 | << CE << "\n"; |
| 358 | abort(); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | switch (CPV->getType()->getPrimitiveID()) { |
| 363 | case Type::BoolTyID: |
| 364 | Out << (CPV == ConstantBool::False ? "0" : "1"); break; |
| 365 | case Type::SByteTyID: |
| 366 | case Type::ShortTyID: |
| 367 | case Type::IntTyID: |
| 368 | Out << cast<ConstantSInt>(CPV)->getValue(); break; |
| 369 | case Type::LongTyID: |
| 370 | Out << cast<ConstantSInt>(CPV)->getValue() << "ll"; break; |
| 371 | |
| 372 | case Type::UByteTyID: |
| 373 | case Type::UShortTyID: |
| 374 | Out << cast<ConstantUInt>(CPV)->getValue(); break; |
| 375 | case Type::UIntTyID: |
| 376 | Out << cast<ConstantUInt>(CPV)->getValue() << "u"; break; |
| 377 | case Type::ULongTyID: |
| 378 | Out << cast<ConstantUInt>(CPV)->getValue() << "ull"; break; |
| 379 | |
| 380 | case Type::FloatTyID: |
Chris Lattner | d1cf1b4 | 2002-09-20 23:26:33 +0000 | [diff] [blame] | 381 | case Type::DoubleTyID: { |
| 382 | ConstantFP *FPC = cast<ConstantFP>(CPV); |
| 383 | map<const ConstantFP *, unsigned>::iterator I = FPConstantMap.find(FPC); |
| 384 | if (I != FPConstantMap.end()) { |
| 385 | // Because of FP precision problems we must load from a stack allocated |
| 386 | // value that holds the value in hex. |
| 387 | Out << "(*(" << (FPC->getType() == Type::FloatTy ? "float" : "double") |
| 388 | << "*)&FloatConstant" << I->second << ")"; |
| 389 | } else { |
| 390 | Out << FPC->getValue(); |
| 391 | } |
| 392 | break; |
| 393 | } |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 394 | |
| 395 | case Type::ArrayTyID: |
| 396 | printConstantArray(cast<ConstantArray>(CPV)); |
| 397 | break; |
| 398 | |
| 399 | case Type::StructTyID: { |
| 400 | Out << "{"; |
| 401 | if (CPV->getNumOperands()) { |
| 402 | Out << " "; |
| 403 | printConstant(cast<Constant>(CPV->getOperand(0))); |
| 404 | for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) { |
| 405 | Out << ", "; |
| 406 | printConstant(cast<Constant>(CPV->getOperand(i))); |
| 407 | } |
| 408 | } |
| 409 | Out << " }"; |
| 410 | break; |
| 411 | } |
| 412 | |
| 413 | case Type::PointerTyID: |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 414 | if (isa<ConstantPointerNull>(CPV)) { |
| 415 | Out << "(("; |
Chris Lattner | 3af3ba8 | 2002-05-09 21:31:18 +0000 | [diff] [blame] | 416 | printType(CPV->getType(), ""); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 417 | Out << ")NULL)"; |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 418 | break; |
| 419 | } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) { |
| 420 | writeOperand(CPR->getValue()); |
| 421 | break; |
| 422 | } |
| 423 | // FALL THROUGH |
| 424 | default: |
| 425 | std::cerr << "Unknown constant type: " << CPV << "\n"; |
| 426 | abort(); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | void CWriter::writeOperandInternal(Value *Operand) { |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 431 | if (Instruction *I = dyn_cast<Instruction>(Operand)) |
| 432 | if (isInlinableInst(*I)) { |
| 433 | // Should we inline this instruction to build a tree? |
| 434 | Out << "("; |
| 435 | visit(*I); |
| 436 | Out << ")"; |
| 437 | return; |
| 438 | } |
| 439 | |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 440 | if (Operand->hasName()) { |
| 441 | Out << getValueName(Operand); |
| 442 | } else if (Constant *CPV = dyn_cast<Constant>(Operand)) { |
| 443 | printConstant(CPV); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 444 | } else { |
Chris Lattner | 0e9f93e | 2002-08-31 00:29:16 +0000 | [diff] [blame] | 445 | int Slot = Table->getValSlot(Operand); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 446 | assert(Slot >= 0 && "Malformed LLVM!"); |
| 447 | Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID(); |
| 448 | } |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 451 | void CWriter::writeOperand(Value *Operand) { |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 452 | if (isa<GlobalVariable>(Operand)) |
| 453 | Out << "(&"; // Global variables are references as their addresses by llvm |
| 454 | |
| 455 | writeOperandInternal(Operand); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 456 | |
| 457 | if (isa<GlobalVariable>(Operand)) |
| 458 | Out << ")"; |
| 459 | } |
| 460 | |
Chris Lattner | 0e9f93e | 2002-08-31 00:29:16 +0000 | [diff] [blame] | 461 | // nameAllUsedStructureTypes - If there are structure types in the module that |
| 462 | // are used but do not have names assigned to them in the symbol table yet then |
| 463 | // we assign them names now. |
| 464 | // |
| 465 | bool CWriter::nameAllUsedStructureTypes(Module &M) { |
| 466 | // Get a set of types that are used by the program... |
| 467 | std::set<const Type *> UT = getAnalysis<FindUsedTypes>().getTypes(); |
| 468 | |
| 469 | // Loop over the module symbol table, removing types from UT that are already |
| 470 | // named. |
| 471 | // |
| 472 | SymbolTable *MST = M.getSymbolTableSure(); |
| 473 | if (MST->find(Type::TypeTy) != MST->end()) |
| 474 | for (SymbolTable::type_iterator I = MST->type_begin(Type::TypeTy), |
| 475 | E = MST->type_end(Type::TypeTy); I != E; ++I) |
| 476 | UT.erase(cast<Type>(I->second)); |
| 477 | |
| 478 | // UT now contains types that are not named. Loop over it, naming structure |
| 479 | // types. |
| 480 | // |
| 481 | bool Changed = false; |
| 482 | for (std::set<const Type *>::const_iterator I = UT.begin(), E = UT.end(); |
| 483 | I != E; ++I) |
| 484 | if (const StructType *ST = dyn_cast<StructType>(*I)) { |
| 485 | ((Value*)ST)->setName("unnamed", MST); |
| 486 | Changed = true; |
| 487 | } |
| 488 | return Changed; |
| 489 | } |
| 490 | |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 491 | void CWriter::printModule(Module *M) { |
Chris Lattner | 78771c8 | 2002-05-17 04:55:35 +0000 | [diff] [blame] | 492 | // Calculate which global values have names that will collide when we throw |
| 493 | // away type information. |
Chris Lattner | 594b9fa | 2002-05-21 21:10:04 +0000 | [diff] [blame] | 494 | { // Scope to delete the FoundNames set when we are done with it... |
Chris Lattner | 78771c8 | 2002-05-17 04:55:35 +0000 | [diff] [blame] | 495 | std::set<string> FoundNames; |
| 496 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 497 | if (I->hasName()) // If the global has a name... |
| 498 | if (FoundNames.count(I->getName())) // And the name is already used |
| 499 | MangledGlobals.insert(I); // Mangle the name |
Chris Lattner | 78771c8 | 2002-05-17 04:55:35 +0000 | [diff] [blame] | 500 | else |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 501 | FoundNames.insert(I->getName()); // Otherwise, keep track of name |
Chris Lattner | 78771c8 | 2002-05-17 04:55:35 +0000 | [diff] [blame] | 502 | |
| 503 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 504 | if (I->hasName()) // If the global has a name... |
| 505 | if (FoundNames.count(I->getName())) // And the name is already used |
| 506 | MangledGlobals.insert(I); // Mangle the name |
Chris Lattner | 78771c8 | 2002-05-17 04:55:35 +0000 | [diff] [blame] | 507 | else |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 508 | FoundNames.insert(I->getName()); // Otherwise, keep track of name |
Chris Lattner | 78771c8 | 2002-05-17 04:55:35 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 511 | // printing stdlib inclusion |
| 512 | // Out << "#include <stdlib.h>\n"; |
| 513 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 514 | // get declaration for alloca |
| 515 | Out << "/* Provide Declarations */\n" |
Chris Lattner | 594b9fa | 2002-05-21 21:10:04 +0000 | [diff] [blame] | 516 | << "#include <malloc.h>\n" |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 517 | << "#include <alloca.h>\n\n" |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 518 | |
Vikram S. Adve | 969c4ad | 2002-08-25 20:00:08 +0000 | [diff] [blame] | 519 | // Provide a definition for null if one does not already exist, |
| 520 | // and for `bool' if not compiling with a C++ compiler. |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 521 | << "#ifndef NULL\n#define NULL 0\n#endif\n\n" |
Vikram S. Adve | 969c4ad | 2002-08-25 20:00:08 +0000 | [diff] [blame] | 522 | << "#ifndef __cplusplus\ntypedef unsigned char bool;\n#endif\n" |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 523 | |
Chris Lattner | d1cf1b4 | 2002-09-20 23:26:33 +0000 | [diff] [blame] | 524 | << "\n\n/* Support for floating point constants */\n" |
| 525 | << "typedef unsigned long long ConstantDoubleTy;\n" |
| 526 | |
Chris Lattner | a4d4a85 | 2002-08-19 21:48:40 +0000 | [diff] [blame] | 527 | << "\n\n/* Global Declarations */\n"; |
| 528 | |
| 529 | // First output all the declarations for the program, because C requires |
| 530 | // Functions & globals to be declared before they are used. |
| 531 | // |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 532 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 533 | // Loop over the symbol table, emitting all named constants... |
| 534 | if (M->hasSymbolTable()) |
| 535 | printSymbolTable(*M->getSymbolTable()); |
| 536 | |
Chris Lattner | a4d4a85 | 2002-08-19 21:48:40 +0000 | [diff] [blame] | 537 | // Global variable declarations... |
| 538 | if (!M->gempty()) { |
Vikram S. Adve | 913bfbc | 2002-09-17 11:50:38 +0000 | [diff] [blame] | 539 | Out << "\n/* External Global Variable Declarations */\n"; |
Chris Lattner | a4d4a85 | 2002-08-19 21:48:40 +0000 | [diff] [blame] | 540 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) { |
Vikram S. Adve | 913bfbc | 2002-09-17 11:50:38 +0000 | [diff] [blame] | 541 | if (I->hasExternalLinkage()) { |
| 542 | Out << "extern "; |
| 543 | printType(I->getType()->getElementType(), getValueName(I)); |
| 544 | Out << ";\n"; |
| 545 | } |
Chris Lattner | deed7a5 | 2002-05-09 15:18:52 +0000 | [diff] [blame] | 546 | } |
Chris Lattner | deed7a5 | 2002-05-09 15:18:52 +0000 | [diff] [blame] | 547 | } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 548 | |
Chris Lattner | a4d4a85 | 2002-08-19 21:48:40 +0000 | [diff] [blame] | 549 | // Function declarations |
| 550 | if (!M->empty()) { |
| 551 | Out << "\n/* Function Declarations */\n"; |
Chris Lattner | 4cda835 | 2002-08-20 16:55:48 +0000 | [diff] [blame] | 552 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) { |
| 553 | printFunctionSignature(I, true); |
| 554 | Out << ";\n"; |
| 555 | } |
Chris Lattner | a4d4a85 | 2002-08-19 21:48:40 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Vikram S. Adve | 913bfbc | 2002-09-17 11:50:38 +0000 | [diff] [blame] | 558 | // Output the global variable definitions and contents... |
Chris Lattner | a4d4a85 | 2002-08-19 21:48:40 +0000 | [diff] [blame] | 559 | if (!M->gempty()) { |
Vikram S. Adve | 913bfbc | 2002-09-17 11:50:38 +0000 | [diff] [blame] | 560 | Out << "\n\n/* Global Variable Definitions and Initialization */\n"; |
Chris Lattner | a4d4a85 | 2002-08-19 21:48:40 +0000 | [diff] [blame] | 561 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) { |
Vikram S. Adve | 913bfbc | 2002-09-17 11:50:38 +0000 | [diff] [blame] | 562 | if (I->hasExternalLinkage()) |
| 563 | continue; // printed above! |
| 564 | Out << "static "; |
Chris Lattner | a4d4a85 | 2002-08-19 21:48:40 +0000 | [diff] [blame] | 565 | printType(I->getType()->getElementType(), getValueName(I)); |
| 566 | |
| 567 | if (I->hasInitializer()) { |
| 568 | Out << " = " ; |
| 569 | writeOperand(I->getInitializer()); |
| 570 | } |
| 571 | Out << ";\n"; |
| 572 | } |
| 573 | } |
| 574 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 575 | // Output all of the functions... |
Chris Lattner | a4d4a85 | 2002-08-19 21:48:40 +0000 | [diff] [blame] | 576 | if (!M->empty()) { |
| 577 | Out << "\n\n/* Function Bodies */\n"; |
| 578 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 579 | printFunction(I); |
| 580 | } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 583 | |
Chris Lattner | 2db41cd | 2002-09-20 15:12:13 +0000 | [diff] [blame] | 584 | /// printSymbolTable - Run through symbol table looking for type names. If a |
| 585 | /// type name is found, emit it's declaration... |
Chris Lattner | a4c047e | 2002-09-20 14:56:54 +0000 | [diff] [blame] | 586 | /// |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 587 | void CWriter::printSymbolTable(const SymbolTable &ST) { |
Chris Lattner | 2db41cd | 2002-09-20 15:12:13 +0000 | [diff] [blame] | 588 | // If there are no type names, exit early. |
| 589 | if (ST.find(Type::TypeTy) == ST.end()) |
| 590 | return; |
| 591 | |
| 592 | // We are only interested in the type plane of the symbol table... |
| 593 | SymbolTable::type_const_iterator I = ST.type_begin(Type::TypeTy); |
| 594 | SymbolTable::type_const_iterator End = ST.type_end(Type::TypeTy); |
| 595 | |
Chris Lattner | 58d04d4 | 2002-09-20 15:18:30 +0000 | [diff] [blame] | 596 | // Print out forward declarations for structure types before anything else! |
| 597 | Out << "/* Structure forward decls */\n"; |
| 598 | for (; I != End; ++I) |
| 599 | if (const Type *STy = dyn_cast<StructType>(I->second)) { |
| 600 | string Name = "struct l_" + makeNameProper(I->first); |
| 601 | Out << Name << ";\n"; |
| 602 | TypeNames.insert(std::make_pair(STy, Name)); |
| 603 | } |
| 604 | |
| 605 | Out << "\n"; |
| 606 | |
| 607 | // Now we can print out typedefs... |
| 608 | Out << "/* Typedefs */\n"; |
| 609 | for (I = ST.type_begin(Type::TypeTy); I != End; ++I) { |
| 610 | const Type *Ty = cast<Type>(I->second); |
| 611 | string Name = "l_" + makeNameProper(I->first); |
| 612 | Out << "typedef "; |
Chris Lattner | 270d78a | 2002-09-20 15:20:24 +0000 | [diff] [blame] | 613 | printType(Ty, Name); |
Chris Lattner | 58d04d4 | 2002-09-20 15:18:30 +0000 | [diff] [blame] | 614 | Out << ";\n"; |
Chris Lattner | 3ef6dc7 | 2002-05-09 14:40:11 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | Out << "\n"; |
| 618 | |
Chris Lattner | 2c601a7 | 2002-09-20 15:05:40 +0000 | [diff] [blame] | 619 | // Keep track of which structures have been printed so far... |
| 620 | std::set<const StructType *> StructPrinted; |
| 621 | |
Nick Hildenbrandt | 67aa2e2 | 2002-09-14 21:36:24 +0000 | [diff] [blame] | 622 | // Loop over all structures then push them into the stack so they are |
| 623 | // printed in the correct order. |
Chris Lattner | 2db41cd | 2002-09-20 15:12:13 +0000 | [diff] [blame] | 624 | // |
Chris Lattner | 58d04d4 | 2002-09-20 15:18:30 +0000 | [diff] [blame] | 625 | Out << "/* Structure contents */\n"; |
Chris Lattner | 2db41cd | 2002-09-20 15:12:13 +0000 | [diff] [blame] | 626 | for (I = ST.type_begin(Type::TypeTy); I != End; ++I) |
| 627 | if (const StructType *STy = dyn_cast<StructType>(I->second)) |
| 628 | printContainedStructs(STy, StructPrinted); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Nick Hildenbrandt | 67aa2e2 | 2002-09-14 21:36:24 +0000 | [diff] [blame] | 631 | // Push the struct onto the stack and recursively push all structs |
| 632 | // this one depends on. |
Chris Lattner | 2c601a7 | 2002-09-20 15:05:40 +0000 | [diff] [blame] | 633 | void CWriter::printContainedStructs(const Type *Ty, |
| 634 | std::set<const StructType*> &StructPrinted){ |
Nick Hildenbrandt | 67aa2e2 | 2002-09-14 21:36:24 +0000 | [diff] [blame] | 635 | if (const StructType *STy = dyn_cast<StructType>(Ty)){ |
| 636 | //Check to see if we have already printed this struct |
Chris Lattner | 2c601a7 | 2002-09-20 15:05:40 +0000 | [diff] [blame] | 637 | if (StructPrinted.count(STy) == 0) { |
| 638 | // Print all contained types first... |
Chris Lattner | a4c047e | 2002-09-20 14:56:54 +0000 | [diff] [blame] | 639 | for (StructType::ElementTypes::const_iterator |
| 640 | I = STy->getElementTypes().begin(), |
| 641 | E = STy->getElementTypes().end(); I != E; ++I) { |
Chris Lattner | 2c601a7 | 2002-09-20 15:05:40 +0000 | [diff] [blame] | 642 | const Type *Ty1 = I->get(); |
Chris Lattner | a4c047e | 2002-09-20 14:56:54 +0000 | [diff] [blame] | 643 | if (isa<StructType>(Ty1) || isa<ArrayType>(Ty1)) |
Chris Lattner | 2c601a7 | 2002-09-20 15:05:40 +0000 | [diff] [blame] | 644 | printContainedStructs(Ty1, StructPrinted); |
Chris Lattner | a4c047e | 2002-09-20 14:56:54 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Chris Lattner | 2c601a7 | 2002-09-20 15:05:40 +0000 | [diff] [blame] | 647 | //Print structure type out.. |
Nick Hildenbrandt | 67aa2e2 | 2002-09-14 21:36:24 +0000 | [diff] [blame] | 648 | StructPrinted.insert(STy); |
| 649 | string Name = TypeNames[STy]; |
| 650 | printType(STy, Name, true); |
Chris Lattner | 58d04d4 | 2002-09-20 15:18:30 +0000 | [diff] [blame] | 651 | Out << ";\n\n"; |
Nick Hildenbrandt | 67aa2e2 | 2002-09-14 21:36:24 +0000 | [diff] [blame] | 652 | } |
Chris Lattner | a4c047e | 2002-09-20 14:56:54 +0000 | [diff] [blame] | 653 | |
Chris Lattner | 2c601a7 | 2002-09-20 15:05:40 +0000 | [diff] [blame] | 654 | // If it is an array, check contained types and continue |
| 655 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)){ |
Nick Hildenbrandt | 67aa2e2 | 2002-09-14 21:36:24 +0000 | [diff] [blame] | 656 | const Type *Ty1 = ATy->getElementType(); |
| 657 | if (isa<StructType>(Ty1) || isa<ArrayType>(Ty1)) |
Chris Lattner | 2c601a7 | 2002-09-20 15:05:40 +0000 | [diff] [blame] | 658 | printContainedStructs(Ty1, StructPrinted); |
Nick Hildenbrandt | 67aa2e2 | 2002-09-14 21:36:24 +0000 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
Chris Lattner | 3ef6dc7 | 2002-05-09 14:40:11 +0000 | [diff] [blame] | 662 | |
Chris Lattner | 4cda835 | 2002-08-20 16:55:48 +0000 | [diff] [blame] | 663 | void CWriter::printFunctionSignature(const Function *F, bool Prototype) { |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 664 | if (F->hasInternalLinkage()) Out << "static "; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 665 | |
| 666 | // Loop over the arguments, printing them... |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 667 | const FunctionType *FT = cast<FunctionType>(F->getFunctionType()); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 668 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 669 | // Print out the return type and name... |
Chris Lattner | 2a7ab2e | 2002-05-09 04:39:00 +0000 | [diff] [blame] | 670 | printType(F->getReturnType()); |
Chris Lattner | 1f02c89 | 2002-05-09 20:14:10 +0000 | [diff] [blame] | 671 | Out << getValueName(F) << "("; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 672 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 673 | if (!F->isExternal()) { |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 674 | if (!F->aempty()) { |
Chris Lattner | 4cda835 | 2002-08-20 16:55:48 +0000 | [diff] [blame] | 675 | string ArgName; |
| 676 | if (F->abegin()->hasName() || !Prototype) |
| 677 | ArgName = getValueName(F->abegin()); |
| 678 | |
| 679 | printType(F->afront().getType(), ArgName); |
Chris Lattner | deed7a5 | 2002-05-09 15:18:52 +0000 | [diff] [blame] | 680 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 681 | for (Function::const_aiterator I = ++F->abegin(), E = F->aend(); |
| 682 | I != E; ++I) { |
Chris Lattner | deed7a5 | 2002-05-09 15:18:52 +0000 | [diff] [blame] | 683 | Out << ", "; |
Chris Lattner | 4cda835 | 2002-08-20 16:55:48 +0000 | [diff] [blame] | 684 | if (I->hasName() || !Prototype) |
| 685 | ArgName = getValueName(I); |
| 686 | else |
| 687 | ArgName = ""; |
| 688 | printType(I->getType(), ArgName); |
Chris Lattner | deed7a5 | 2002-05-09 15:18:52 +0000 | [diff] [blame] | 689 | } |
| 690 | } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 691 | } else { |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 692 | // Loop over the arguments, printing them... |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 693 | for (FunctionType::ParamTypes::const_iterator I = |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 694 | FT->getParamTypes().begin(), |
| 695 | E = FT->getParamTypes().end(); I != E; ++I) { |
| 696 | if (I != FT->getParamTypes().begin()) Out << ", "; |
Chris Lattner | 2a7ab2e | 2002-05-09 04:39:00 +0000 | [diff] [blame] | 697 | printType(*I); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | |
Chris Lattner | 1f8c4a1 | 2002-09-20 22:32:30 +0000 | [diff] [blame] | 701 | // Finish printing arguments... if this is a vararg function, print the ..., |
| 702 | // unless there are no known types, in which case, we just emit (). |
| 703 | // |
| 704 | if (FT->isVarArg() && !FT->getParamTypes().empty()) { |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 705 | if (FT->getParamTypes().size()) Out << ", "; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 706 | Out << "..."; // Output varargs portion of signature! |
| 707 | } |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 708 | Out << ")"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 711 | |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 712 | void CWriter::printFunction(Function *F) { |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 713 | if (F->isExternal()) return; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 714 | |
Chris Lattner | 0e9f93e | 2002-08-31 00:29:16 +0000 | [diff] [blame] | 715 | Table->incorporateFunction(F); |
Chris Lattner | f34ee81 | 2002-05-09 03:12:34 +0000 | [diff] [blame] | 716 | |
Chris Lattner | 4cda835 | 2002-08-20 16:55:48 +0000 | [diff] [blame] | 717 | printFunctionSignature(F, false); |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 718 | Out << " {\n"; |
| 719 | |
Chris Lattner | 497e19a | 2002-05-09 20:39:03 +0000 | [diff] [blame] | 720 | // print local variable information for the function |
Chris Lattner | 7683a12 | 2002-05-09 20:33:35 +0000 | [diff] [blame] | 721 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 722 | if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) { |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 723 | Out << " "; |
Chris Lattner | 3af3ba8 | 2002-05-09 21:31:18 +0000 | [diff] [blame] | 724 | printType((*I)->getType(), getValueName(*I)); |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 725 | Out << ";\n"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 726 | } |
Chris Lattner | d1cf1b4 | 2002-09-20 23:26:33 +0000 | [diff] [blame] | 727 | |
| 728 | Out << "\n"; |
| 729 | |
| 730 | // Scan the function for floating point constants. If any FP constant is used |
| 731 | // in the function, we want to redirect it here so that we do not depend on |
| 732 | // the precision of the printed form. |
| 733 | // |
| 734 | unsigned FPCounter = 0; |
| 735 | for (constant_iterator I = constant_begin(F), E = constant_end(F); I != E;++I) |
| 736 | if (const ConstantFP *FPC = dyn_cast<ConstantFP>(*I)) |
| 737 | if (FPConstantMap.find(FPC) == FPConstantMap.end()) { |
| 738 | double Val = FPC->getValue(); |
| 739 | |
| 740 | FPConstantMap[FPC] = FPCounter; // Number the FP constants |
| 741 | Out << " const ConstantDoubleTy FloatConstant" << FPCounter++ |
| 742 | << " = 0x" << std::hex << *(unsigned long long*)&Val << std::dec |
| 743 | << "; /* " << Val << " */\n"; |
| 744 | } |
| 745 | |
| 746 | Out << "\n"; |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 747 | |
| 748 | // print the basic blocks |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 749 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 750 | BasicBlock *Prev = BB->getPrev(); |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 751 | |
| 752 | // Don't print the label for the basic block if there are no uses, or if the |
| 753 | // only terminator use is the precessor basic block's terminator. We have |
| 754 | // to scan the use list because PHI nodes use basic blocks too but do not |
| 755 | // require a label to be generated. |
| 756 | // |
| 757 | bool NeedsLabel = false; |
| 758 | for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end(); |
| 759 | UI != UE; ++UI) |
| 760 | if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI)) |
| 761 | if (TI != Prev->getTerminator()) { |
| 762 | NeedsLabel = true; |
| 763 | break; |
| 764 | } |
| 765 | |
| 766 | if (NeedsLabel) Out << getValueName(BB) << ":\n"; |
| 767 | |
| 768 | // Output all of the instructions in the basic block... |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 769 | for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II){ |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 770 | if (!isInlinableInst(*II) && !isa<PHINode>(*II)) { |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 771 | if (II->getType() != Type::VoidTy) |
| 772 | outputLValue(II); |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 773 | else |
| 774 | Out << " "; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 775 | visit(*II); |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 776 | Out << ";\n"; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | // Don't emit prefix or suffix for the terminator... |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 781 | visit(*BB->getTerminator()); |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 782 | } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 783 | |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 784 | Out << "}\n\n"; |
Chris Lattner | 0e9f93e | 2002-08-31 00:29:16 +0000 | [diff] [blame] | 785 | Table->purgeFunction(); |
Chris Lattner | d1cf1b4 | 2002-09-20 23:26:33 +0000 | [diff] [blame] | 786 | FPConstantMap.clear(); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 789 | // Specific Instruction type classes... note that all of the casts are |
| 790 | // neccesary because we use the instruction classes as opaque types... |
| 791 | // |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 792 | void CWriter::visitReturnInst(ReturnInst &I) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 793 | // Don't output a void return if this is the last basic block in the function |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 794 | if (I.getNumOperands() == 0 && |
| 795 | &*--I.getParent()->getParent()->end() == I.getParent() && |
| 796 | !I.getParent()->size() == 1) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 797 | return; |
Chris Lattner | 963b70b | 2002-05-21 18:05:19 +0000 | [diff] [blame] | 798 | } |
Chris Lattner | 4440826 | 2002-05-09 03:50:42 +0000 | [diff] [blame] | 799 | |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 800 | Out << " return"; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 801 | if (I.getNumOperands()) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 802 | Out << " "; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 803 | writeOperand(I.getOperand(0)); |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 804 | } |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 805 | Out << ";\n"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 808 | static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) { |
| 809 | // If PHI nodes need copies, we need the copy code... |
| 810 | if (isa<PHINode>(To->front()) || |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 811 | From->getNext() != To) // Not directly successor, need goto |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 812 | return true; |
| 813 | |
| 814 | // Otherwise we don't need the code. |
| 815 | return false; |
| 816 | } |
| 817 | |
| 818 | void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ, |
| 819 | unsigned Indent) { |
| 820 | for (BasicBlock::iterator I = Succ->begin(); |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 821 | PHINode *PN = dyn_cast<PHINode>(&*I); ++I) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 822 | // now we have to do the printing |
| 823 | Out << string(Indent, ' '); |
| 824 | outputLValue(PN); |
| 825 | writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB))); |
| 826 | Out << "; /* for PHI node */\n"; |
| 827 | } |
| 828 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 829 | if (CurBB->getNext() != Succ) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 830 | Out << string(Indent, ' ') << " goto "; |
| 831 | writeOperand(Succ); |
| 832 | Out << ";\n"; |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | // Brach instruction printing - Avoid printing out a brach to a basic block that |
| 837 | // immediately succeeds the current one. |
| 838 | // |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 839 | void CWriter::visitBranchInst(BranchInst &I) { |
| 840 | if (I.isConditional()) { |
| 841 | if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(0))) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 842 | Out << " if ("; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 843 | writeOperand(I.getCondition()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 844 | Out << ") {\n"; |
| 845 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 846 | printBranchToBlock(I.getParent(), I.getSuccessor(0), 2); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 847 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 848 | if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(1))) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 849 | Out << " } else {\n"; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 850 | printBranchToBlock(I.getParent(), I.getSuccessor(1), 2); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 851 | } |
| 852 | } else { |
| 853 | // First goto not neccesary, assume second one is... |
| 854 | Out << " if (!"; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 855 | writeOperand(I.getCondition()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 856 | Out << ") {\n"; |
| 857 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 858 | printBranchToBlock(I.getParent(), I.getSuccessor(1), 2); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | Out << " }\n"; |
| 862 | } else { |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 863 | printBranchToBlock(I.getParent(), I.getSuccessor(0), 0); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 864 | } |
| 865 | Out << "\n"; |
| 866 | } |
| 867 | |
| 868 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 869 | void CWriter::visitBinaryOperator(Instruction &I) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 870 | // binary instructions, shift instructions, setCond instructions. |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 871 | if (isa<PointerType>(I.getType())) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 872 | Out << "("; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 873 | printType(I.getType()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 874 | Out << ")"; |
| 875 | } |
| 876 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 877 | if (isa<PointerType>(I.getType())) Out << "(long long)"; |
| 878 | writeOperand(I.getOperand(0)); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 879 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 880 | switch (I.getOpcode()) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 881 | case Instruction::Add: Out << " + "; break; |
| 882 | case Instruction::Sub: Out << " - "; break; |
| 883 | case Instruction::Mul: Out << "*"; break; |
| 884 | case Instruction::Div: Out << "/"; break; |
| 885 | case Instruction::Rem: Out << "%"; break; |
| 886 | case Instruction::And: Out << " & "; break; |
| 887 | case Instruction::Or: Out << " | "; break; |
| 888 | case Instruction::Xor: Out << " ^ "; break; |
| 889 | case Instruction::SetEQ: Out << " == "; break; |
| 890 | case Instruction::SetNE: Out << " != "; break; |
| 891 | case Instruction::SetLE: Out << " <= "; break; |
| 892 | case Instruction::SetGE: Out << " >= "; break; |
| 893 | case Instruction::SetLT: Out << " < "; break; |
| 894 | case Instruction::SetGT: Out << " > "; break; |
| 895 | case Instruction::Shl : Out << " << "; break; |
| 896 | case Instruction::Shr : Out << " >> "; break; |
Chris Lattner | e7f65d3b | 2002-06-30 16:07:20 +0000 | [diff] [blame] | 897 | default: std::cerr << "Invalid operator type!" << I; abort(); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 900 | if (isa<PointerType>(I.getType())) Out << "(long long)"; |
| 901 | writeOperand(I.getOperand(1)); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 904 | void CWriter::visitCastInst(CastInst &I) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 905 | Out << "("; |
Vikram S. Adve | 969c4ad | 2002-08-25 20:00:08 +0000 | [diff] [blame] | 906 | printType(I.getType(), string(""),/*ignoreName*/false, /*namedContext*/false); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 907 | Out << ")"; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 908 | writeOperand(I.getOperand(0)); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 911 | void CWriter::visitCallInst(CallInst &I) { |
| 912 | const PointerType *PTy = cast<PointerType>(I.getCalledValue()->getType()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 913 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 914 | const Type *RetTy = FTy->getReturnType(); |
| 915 | |
Chris Lattner | fabc880 | 2002-08-26 20:50:09 +0000 | [diff] [blame] | 916 | writeOperand(I.getOperand(0)); |
| 917 | Out << "("; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 918 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 919 | if (I.getNumOperands() > 1) { |
| 920 | writeOperand(I.getOperand(1)); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 921 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 922 | for (unsigned op = 2, Eop = I.getNumOperands(); op != Eop; ++op) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 923 | Out << ", "; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 924 | writeOperand(I.getOperand(op)); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 925 | } |
| 926 | } |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 927 | Out << ")"; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 930 | void CWriter::visitMallocInst(MallocInst &I) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 931 | Out << "("; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 932 | printType(I.getType()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 933 | Out << ")malloc(sizeof("; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 934 | printType(I.getType()->getElementType()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 935 | Out << ")"; |
| 936 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 937 | if (I.isArrayAllocation()) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 938 | Out << " * " ; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 939 | writeOperand(I.getOperand(0)); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 940 | } |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 941 | Out << ")"; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 944 | void CWriter::visitAllocaInst(AllocaInst &I) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 945 | Out << "("; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 946 | printType(I.getType()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 947 | Out << ") alloca(sizeof("; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 948 | printType(I.getType()->getElementType()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 949 | Out << ")"; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 950 | if (I.isArrayAllocation()) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 951 | Out << " * " ; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 952 | writeOperand(I.getOperand(0)); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 953 | } |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 954 | Out << ")"; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 957 | void CWriter::visitFreeInst(FreeInst &I) { |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 958 | Out << "free("; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 959 | writeOperand(I.getOperand(0)); |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 960 | Out << ")"; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 963 | void CWriter::printIndexingExpression(Value *Ptr, User::op_iterator I, |
| 964 | User::op_iterator E) { |
| 965 | bool HasImplicitAddress = false; |
| 966 | // If accessing a global value with no indexing, avoid *(&GV) syndrome |
| 967 | if (GlobalValue *V = dyn_cast<GlobalValue>(Ptr)) { |
| 968 | HasImplicitAddress = true; |
| 969 | } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Ptr)) { |
| 970 | HasImplicitAddress = true; |
| 971 | Ptr = CPR->getValue(); // Get to the global... |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 972 | } |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 973 | |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 974 | if (I == E) { |
| 975 | if (!HasImplicitAddress) |
| 976 | Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]' |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 977 | |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 978 | writeOperandInternal(Ptr); |
| 979 | return; |
| 980 | } |
| 981 | |
| 982 | const Constant *CI = dyn_cast<Constant>(I->get()); |
| 983 | if (HasImplicitAddress && (!CI || !CI->isNullValue())) |
| 984 | Out << "(&"; |
| 985 | |
| 986 | writeOperandInternal(Ptr); |
| 987 | |
| 988 | if (HasImplicitAddress && (!CI || !CI->isNullValue())) |
| 989 | Out << ")"; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 990 | |
| 991 | // Print out the -> operator if possible... |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 992 | if (CI && CI->isNullValue() && I+1 != E) { |
| 993 | if ((*(I+1))->getType() == Type::UByteTy) { |
| 994 | Out << (HasImplicitAddress ? "." : "->"); |
| 995 | Out << "field" << cast<ConstantUInt>(*(I+1))->getValue(); |
| 996 | I += 2; |
Vikram S. Adve | 42eb2ba | 2002-08-24 14:44:23 +0000 | [diff] [blame] | 997 | } else { // First array index of 0: Just skip it |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 998 | ++I; |
| 999 | } |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1000 | } |
Chris Lattner | 5dfe767 | 2002-08-22 22:48:55 +0000 | [diff] [blame] | 1001 | |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1002 | for (; I != E; ++I) |
Chris Lattner | 106ff45 | 2002-09-11 01:21:29 +0000 | [diff] [blame] | 1003 | if ((*I)->getType() == Type::LongTy) { |
Vikram S. Adve | 9d6f13f | 2002-09-15 21:51:04 +0000 | [diff] [blame] | 1004 | Out << "["; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1005 | writeOperand(*I); |
Vikram S. Adve | 9d6f13f | 2002-09-15 21:51:04 +0000 | [diff] [blame] | 1006 | Out << "]"; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1007 | } else { |
Chris Lattner | 59afbf3 | 2002-09-12 20:34:47 +0000 | [diff] [blame] | 1008 | Out << ".field" << cast<ConstantUInt>(*I)->getValue(); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1012 | void CWriter::visitLoadInst(LoadInst &I) { |
Chris Lattner | 5dfe767 | 2002-08-22 22:48:55 +0000 | [diff] [blame] | 1013 | Out << "*"; |
| 1014 | writeOperand(I.getOperand(0)); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1017 | void CWriter::visitStoreInst(StoreInst &I) { |
Chris Lattner | 5dfe767 | 2002-08-22 22:48:55 +0000 | [diff] [blame] | 1018 | Out << "*"; |
| 1019 | writeOperand(I.getPointerOperand()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1020 | Out << " = "; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1021 | writeOperand(I.getOperand(0)); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1024 | void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1025 | Out << "&"; |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 1026 | printIndexingExpression(I.getPointerOperand(), I.idx_begin(), I.idx_end()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1027 | } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1028 | |
| 1029 | //===----------------------------------------------------------------------===// |
| 1030 | // External Interface declaration |
| 1031 | //===----------------------------------------------------------------------===// |
| 1032 | |
Chris Lattner | 0e9f93e | 2002-08-31 00:29:16 +0000 | [diff] [blame] | 1033 | Pass *createWriteToCPass(std::ostream &o) { return new CWriter(o); } |