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