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 | // |
| 3 | // This library implements the functionality defined in llvm/Assembly/CWriter.h |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 4 | // |
| 5 | // TODO : Recursive types. |
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 | //===-----------------------------------------------------------------------==// |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 8 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 9 | #include "llvm/Assembly/CWriter.h" |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 10 | #include "llvm/Constants.h" |
Chris Lattner | 2f5f51a | 2002-05-09 03:28:37 +0000 | [diff] [blame] | 11 | #include "llvm/DerivedTypes.h" |
| 12 | #include "llvm/Module.h" |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 13 | #include "llvm/iMemory.h" |
| 14 | #include "llvm/iTerminators.h" |
| 15 | #include "llvm/iPHINode.h" |
| 16 | #include "llvm/iOther.h" |
Chris Lattner | 2f5f51a | 2002-05-09 03:28:37 +0000 | [diff] [blame] | 17 | #include "llvm/iOperators.h" |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 18 | #include "llvm/SymbolTable.h" |
Chris Lattner | 3af3ba8 | 2002-05-09 21:31:18 +0000 | [diff] [blame] | 19 | #include "llvm/SlotCalculator.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 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 30 | namespace { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 31 | class CWriter : public InstVisitor<CWriter> { |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 32 | ostream& Out; |
| 33 | SlotCalculator &Table; |
| 34 | const Module *TheModule; |
| 35 | map<const Type *, string> TypeNames; |
Chris Lattner | 78771c8 | 2002-05-17 04:55:35 +0000 | [diff] [blame] | 36 | std::set<const Value*> MangledGlobals; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 37 | public: |
| 38 | inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M) |
| 39 | : Out(o), Table(Tab), TheModule(M) { |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 42 | inline void write(Module *M) { printModule(M); } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame^] | 44 | ostream &printType(const Type *Ty, const string &VariableName = "", |
| 45 | bool IgnoreName = false); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 46 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 47 | void writeOperand(Value *Operand); |
| 48 | void writeOperandInternal(Value *Operand); |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 49 | |
| 50 | string getValueName(const Value *V); |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 52 | private : |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 53 | void printModule(Module *M); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 54 | void printSymbolTable(const SymbolTable &ST); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 55 | void printGlobal(const GlobalVariable *GV); |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 56 | void printFunctionSignature(const Function *F); |
| 57 | void printFunctionDecl(const Function *F); // Print just the forward decl |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 59 | void printFunction(Function *); |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 61 | void printConstant(Constant *CPV); |
| 62 | void printConstantArray(ConstantArray *CPA); |
| 63 | |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 64 | // isInlinableInst - Attempt to inline instructions into their uses to build |
| 65 | // trees as much as possible. To do this, we have to consistently decide |
| 66 | // what is acceptable to inline, so that variable declarations don't get |
| 67 | // printed and an extra copy of the expr is not emitted. |
| 68 | // |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 69 | static bool isInlinableInst(const Instruction &I) { |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 70 | // Must be an expression, must be used exactly once. If it is dead, we |
| 71 | // emit it inline where it would go. |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 72 | if (I.getType() == Type::VoidTy || I.use_size() != 1 || |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 73 | isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I)) |
| 74 | return false; |
| 75 | |
| 76 | // 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] | 77 | return I.getParent() == cast<Instruction>(I.use_back())->getParent(); |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 80 | // Instruction visitation functions |
| 81 | friend class InstVisitor<CWriter>; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 82 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 83 | void visitReturnInst(ReturnInst &I); |
| 84 | void visitBranchInst(BranchInst &I); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 85 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 86 | void visitPHINode(PHINode &I) {} |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 87 | void visitBinaryOperator(Instruction &I); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 88 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 89 | void visitCastInst (CastInst &I); |
| 90 | void visitCallInst (CallInst &I); |
| 91 | void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); } |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 92 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 93 | void visitMallocInst(MallocInst &I); |
| 94 | void visitAllocaInst(AllocaInst &I); |
| 95 | void visitFreeInst (FreeInst &I); |
| 96 | void visitLoadInst (LoadInst &I); |
| 97 | void visitStoreInst (StoreInst &I); |
| 98 | void visitGetElementPtrInst(GetElementPtrInst &I); |
Chris Lattner | 2f5f51a | 2002-05-09 03:28:37 +0000 | [diff] [blame] | 99 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 100 | void visitInstruction(Instruction &I) { |
Chris Lattner | e7f65d3b | 2002-06-30 16:07:20 +0000 | [diff] [blame] | 101 | std::cerr << "C Writer does not know about " << I; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 102 | abort(); |
| 103 | } |
| 104 | |
| 105 | void outputLValue(Instruction *I) { |
| 106 | Out << " " << getValueName(I) << " = "; |
| 107 | } |
| 108 | void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock, |
| 109 | unsigned Indent); |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 110 | void printIndexingExpr(MemAccessInst &MAI); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 111 | }; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Chris Lattner | 2f49902 | 2002-05-09 04:21:21 +0000 | [diff] [blame] | 114 | // We dont want identifier names with ., space, - in them. |
| 115 | // So we replace them with _ |
| 116 | static string makeNameProper(string x) { |
| 117 | string tmp; |
| 118 | for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++) |
| 119 | switch (*sI) { |
Chris Lattner | deed7a5 | 2002-05-09 15:18:52 +0000 | [diff] [blame] | 120 | case '.': tmp += "d_"; break; |
| 121 | case ' ': tmp += "s_"; break; |
| 122 | case '-': tmp += "D_"; break; |
Chris Lattner | 2f49902 | 2002-05-09 04:21:21 +0000 | [diff] [blame] | 123 | default: tmp += *sI; |
| 124 | } |
| 125 | |
| 126 | return tmp; |
| 127 | } |
| 128 | |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 129 | string CWriter::getValueName(const Value *V) { |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 130 | if (V->hasName()) { // Print out the label if it exists... |
| 131 | if (isa<GlobalValue>(V) && // Do not mangle globals... |
| 132 | cast<GlobalValue>(V)->hasExternalLinkage() && // Unless it's internal or |
| 133 | !MangledGlobals.count(V)) // Unless the name would collide if we don't |
Chris Lattner | 2f49902 | 2002-05-09 04:21:21 +0000 | [diff] [blame] | 134 | return makeNameProper(V->getName()); |
| 135 | |
Chris Lattner | 2d05a1a | 2002-05-09 05:16:40 +0000 | [diff] [blame] | 136 | return "l" + utostr(V->getType()->getUniqueID()) + "_" + |
| 137 | makeNameProper(V->getName()); |
Chris Lattner | 2f49902 | 2002-05-09 04:21:21 +0000 | [diff] [blame] | 138 | } |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 139 | |
| 140 | int Slot = Table.getValSlot(V); |
| 141 | assert(Slot >= 0 && "Invalid value!"); |
Chris Lattner | 2d05a1a | 2002-05-09 05:16:40 +0000 | [diff] [blame] | 142 | return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID()); |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame^] | 145 | // Pass the Type* and the variable name and this prints out the variable |
| 146 | // declaration. |
| 147 | // |
| 148 | ostream &CWriter::printType(const Type *Ty, const string &NameSoFar, |
| 149 | bool IgnoreName = false) { |
| 150 | if (Ty->isPrimitiveType()) |
| 151 | switch (Ty->getPrimitiveID()) { |
| 152 | case Type::VoidTyID: return Out << "void " << NameSoFar; |
| 153 | case Type::BoolTyID: return Out << "bool " << NameSoFar; |
| 154 | case Type::UByteTyID: return Out << "unsigned char " << NameSoFar; |
| 155 | case Type::SByteTyID: return Out << "signed char " << NameSoFar; |
| 156 | case Type::UShortTyID: return Out << "unsigned short " << NameSoFar; |
| 157 | case Type::ShortTyID: return Out << "short " << NameSoFar; |
| 158 | case Type::UIntTyID: return Out << "unsigned " << NameSoFar; |
| 159 | case Type::IntTyID: return Out << "int " << NameSoFar; |
| 160 | case Type::ULongTyID: return Out << "unsigned long long " << NameSoFar; |
| 161 | case Type::LongTyID: return Out << "signed long long " << NameSoFar; |
| 162 | case Type::FloatTyID: return Out << "float " << NameSoFar; |
| 163 | case Type::DoubleTyID: return Out << "double " << NameSoFar; |
| 164 | default : |
| 165 | std::cerr << "Unknown primitive type: " << Ty << "\n"; |
| 166 | abort(); |
| 167 | } |
| 168 | |
| 169 | // Check to see if the type is named. |
| 170 | if (!IgnoreName) { |
| 171 | map<const Type *, string>::iterator I = TypeNames.find(Ty); |
| 172 | if (I != TypeNames.end()) { |
| 173 | return Out << I->second << " " << NameSoFar; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | string Result; |
| 178 | switch (Ty->getPrimitiveID()) { |
| 179 | case Type::FunctionTyID: { |
| 180 | const FunctionType *MTy = cast<FunctionType>(Ty); |
| 181 | printType(MTy->getReturnType(), ""); |
| 182 | Out << " " << NameSoFar << " ("; |
| 183 | |
| 184 | for (FunctionType::ParamTypes::const_iterator |
| 185 | I = MTy->getParamTypes().begin(), |
| 186 | E = MTy->getParamTypes().end(); I != E; ++I) { |
| 187 | if (I != MTy->getParamTypes().begin()) |
| 188 | Out << ", "; |
| 189 | printType(*I, ""); |
| 190 | } |
| 191 | if (MTy->isVarArg()) { |
| 192 | if (!MTy->getParamTypes().empty()) |
| 193 | Out << ", "; |
| 194 | Out << "..."; |
| 195 | } |
| 196 | return Out << ")"; |
| 197 | } |
| 198 | case Type::StructTyID: { |
| 199 | const StructType *STy = cast<StructType>(Ty); |
| 200 | Out << NameSoFar + " {\n"; |
| 201 | unsigned Idx = 0; |
| 202 | for (StructType::ElementTypes::const_iterator |
| 203 | I = STy->getElementTypes().begin(), |
| 204 | E = STy->getElementTypes().end(); I != E; ++I) { |
| 205 | Out << " "; |
| 206 | printType(*I, "field" + utostr(Idx++)); |
| 207 | Out << ";\n"; |
| 208 | } |
| 209 | return Out << "}"; |
| 210 | } |
| 211 | |
| 212 | case Type::PointerTyID: { |
| 213 | const PointerType *PTy = cast<PointerType>(Ty); |
| 214 | // If this is a pointer to a function, we need parens. In all other cases, |
| 215 | // we don't though, and adding them all the time clutters stuff up a ton, so |
| 216 | // special case this. |
| 217 | // |
| 218 | if (isa<FunctionType>(PTy->getElementType())) |
| 219 | return printType(PTy->getElementType(), "(*" + NameSoFar + ")"); |
| 220 | else |
| 221 | return printType(PTy->getElementType(), "*" + NameSoFar); |
| 222 | } |
| 223 | |
| 224 | case Type::ArrayTyID: { |
| 225 | const ArrayType *ATy = cast<ArrayType>(Ty); |
| 226 | unsigned NumElements = ATy->getNumElements(); |
| 227 | return printType(ATy->getElementType(), |
| 228 | NameSoFar + "[" + utostr(NumElements) + "]"); |
| 229 | } |
| 230 | default: |
| 231 | assert(0 && "Unhandled case in getTypeProps!"); |
| 232 | abort(); |
| 233 | } |
| 234 | |
| 235 | return Out; |
| 236 | } |
| 237 | |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 238 | void CWriter::printConstantArray(ConstantArray *CPA) { |
| 239 | |
| 240 | // As a special case, print the array as a string if it is an array of |
| 241 | // ubytes or an array of sbytes with positive values. |
| 242 | // |
| 243 | const Type *ETy = CPA->getType()->getElementType(); |
| 244 | bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy); |
| 245 | |
| 246 | // Make sure the last character is a null char, as automatically added by C |
| 247 | if (CPA->getNumOperands() == 0 || |
| 248 | !cast<Constant>(*(CPA->op_end()-1))->isNullValue()) |
| 249 | isString = false; |
| 250 | |
| 251 | if (isString) { |
| 252 | Out << "\""; |
| 253 | // Do not include the last character, which we know is null |
| 254 | for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) { |
| 255 | unsigned char C = (ETy == Type::SByteTy) ? |
| 256 | (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() : |
| 257 | (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue(); |
| 258 | |
| 259 | if (isprint(C)) { |
| 260 | Out << C; |
| 261 | } else { |
| 262 | switch (C) { |
| 263 | case '\n': Out << "\\n"; break; |
| 264 | case '\t': Out << "\\t"; break; |
| 265 | case '\r': Out << "\\r"; break; |
| 266 | case '\v': Out << "\\v"; break; |
| 267 | case '\a': Out << "\\a"; break; |
| 268 | default: |
| 269 | Out << "\\x"; |
| 270 | Out << ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'); |
| 271 | Out << ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'); |
| 272 | break; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | Out << "\""; |
| 277 | } else { |
| 278 | Out << "{"; |
| 279 | if (CPA->getNumOperands()) { |
| 280 | Out << " "; |
| 281 | printConstant(cast<Constant>(CPA->getOperand(0))); |
| 282 | for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) { |
| 283 | Out << ", "; |
| 284 | printConstant(cast<Constant>(CPA->getOperand(i))); |
| 285 | } |
| 286 | } |
| 287 | Out << " }"; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | |
| 292 | // printConstant - The LLVM Constant to C Constant converter. |
| 293 | void CWriter::printConstant(Constant *CPV) { |
| 294 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) { |
| 295 | switch (CE->getOpcode()) { |
| 296 | default: |
| 297 | std::cerr << "CWriter Error: Unhandled constant expression: " |
| 298 | << CE << "\n"; |
| 299 | abort(); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | switch (CPV->getType()->getPrimitiveID()) { |
| 304 | case Type::BoolTyID: |
| 305 | Out << (CPV == ConstantBool::False ? "0" : "1"); break; |
| 306 | case Type::SByteTyID: |
| 307 | case Type::ShortTyID: |
| 308 | case Type::IntTyID: |
| 309 | Out << cast<ConstantSInt>(CPV)->getValue(); break; |
| 310 | case Type::LongTyID: |
| 311 | Out << cast<ConstantSInt>(CPV)->getValue() << "ll"; break; |
| 312 | |
| 313 | case Type::UByteTyID: |
| 314 | case Type::UShortTyID: |
| 315 | Out << cast<ConstantUInt>(CPV)->getValue(); break; |
| 316 | case Type::UIntTyID: |
| 317 | Out << cast<ConstantUInt>(CPV)->getValue() << "u"; break; |
| 318 | case Type::ULongTyID: |
| 319 | Out << cast<ConstantUInt>(CPV)->getValue() << "ull"; break; |
| 320 | |
| 321 | case Type::FloatTyID: |
| 322 | case Type::DoubleTyID: |
| 323 | Out << cast<ConstantFP>(CPV)->getValue(); break; |
| 324 | |
| 325 | case Type::ArrayTyID: |
| 326 | printConstantArray(cast<ConstantArray>(CPV)); |
| 327 | break; |
| 328 | |
| 329 | case Type::StructTyID: { |
| 330 | Out << "{"; |
| 331 | if (CPV->getNumOperands()) { |
| 332 | Out << " "; |
| 333 | printConstant(cast<Constant>(CPV->getOperand(0))); |
| 334 | for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) { |
| 335 | Out << ", "; |
| 336 | printConstant(cast<Constant>(CPV->getOperand(i))); |
| 337 | } |
| 338 | } |
| 339 | Out << " }"; |
| 340 | break; |
| 341 | } |
| 342 | |
| 343 | case Type::PointerTyID: |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 344 | if (isa<ConstantPointerNull>(CPV)) { |
| 345 | Out << "(("; |
Chris Lattner | 3af3ba8 | 2002-05-09 21:31:18 +0000 | [diff] [blame] | 346 | printType(CPV->getType(), ""); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 347 | Out << ")NULL)"; |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 348 | break; |
| 349 | } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) { |
| 350 | writeOperand(CPR->getValue()); |
| 351 | break; |
| 352 | } |
| 353 | // FALL THROUGH |
| 354 | default: |
| 355 | std::cerr << "Unknown constant type: " << CPV << "\n"; |
| 356 | abort(); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | void CWriter::writeOperandInternal(Value *Operand) { |
| 361 | if (Operand->hasName()) { |
| 362 | Out << getValueName(Operand); |
| 363 | } else if (Constant *CPV = dyn_cast<Constant>(Operand)) { |
| 364 | printConstant(CPV); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 365 | } else { |
| 366 | int Slot = Table.getValSlot(Operand); |
| 367 | assert(Slot >= 0 && "Malformed LLVM!"); |
| 368 | Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID(); |
| 369 | } |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 372 | void CWriter::writeOperand(Value *Operand) { |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 373 | if (Instruction *I = dyn_cast<Instruction>(Operand)) |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 374 | if (isInlinableInst(*I)) { |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 375 | // Should we inline this instruction to build a tree? |
| 376 | Out << "("; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 377 | visit(*I); |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 378 | Out << ")"; |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | if (isa<GlobalVariable>(Operand)) |
| 383 | Out << "(&"; // Global variables are references as their addresses by llvm |
| 384 | |
| 385 | writeOperandInternal(Operand); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 386 | |
| 387 | if (isa<GlobalVariable>(Operand)) |
| 388 | Out << ")"; |
| 389 | } |
| 390 | |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 391 | void CWriter::printModule(Module *M) { |
Chris Lattner | 78771c8 | 2002-05-17 04:55:35 +0000 | [diff] [blame] | 392 | // Calculate which global values have names that will collide when we throw |
| 393 | // away type information. |
Chris Lattner | 594b9fa | 2002-05-21 21:10:04 +0000 | [diff] [blame] | 394 | { // Scope to delete the FoundNames set when we are done with it... |
Chris Lattner | 78771c8 | 2002-05-17 04:55:35 +0000 | [diff] [blame] | 395 | std::set<string> FoundNames; |
| 396 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 397 | if (I->hasName()) // If the global has a name... |
| 398 | if (FoundNames.count(I->getName())) // And the name is already used |
| 399 | MangledGlobals.insert(I); // Mangle the name |
Chris Lattner | 78771c8 | 2002-05-17 04:55:35 +0000 | [diff] [blame] | 400 | else |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 401 | FoundNames.insert(I->getName()); // Otherwise, keep track of name |
Chris Lattner | 78771c8 | 2002-05-17 04:55:35 +0000 | [diff] [blame] | 402 | |
| 403 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 404 | if (I->hasName()) // If the global has a name... |
| 405 | if (FoundNames.count(I->getName())) // And the name is already used |
| 406 | MangledGlobals.insert(I); // Mangle the name |
Chris Lattner | 78771c8 | 2002-05-17 04:55:35 +0000 | [diff] [blame] | 407 | else |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 408 | FoundNames.insert(I->getName()); // Otherwise, keep track of name |
Chris Lattner | 78771c8 | 2002-05-17 04:55:35 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 412 | // printing stdlib inclusion |
| 413 | // Out << "#include <stdlib.h>\n"; |
| 414 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 415 | // get declaration for alloca |
| 416 | Out << "/* Provide Declarations */\n" |
Chris Lattner | 594b9fa | 2002-05-21 21:10:04 +0000 | [diff] [blame] | 417 | << "#include <malloc.h>\n" |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 418 | << "#include <alloca.h>\n\n" |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 419 | |
| 420 | // Provide a definition for null if one does not already exist. |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 421 | << "#ifndef NULL\n#define NULL 0\n#endif\n\n" |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 422 | << "typedef unsigned char bool;\n" |
| 423 | |
Chris Lattner | a4d4a85 | 2002-08-19 21:48:40 +0000 | [diff] [blame] | 424 | << "\n\n/* Global Declarations */\n"; |
| 425 | |
| 426 | // First output all the declarations for the program, because C requires |
| 427 | // Functions & globals to be declared before they are used. |
| 428 | // |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 429 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 430 | // Loop over the symbol table, emitting all named constants... |
| 431 | if (M->hasSymbolTable()) |
| 432 | printSymbolTable(*M->getSymbolTable()); |
| 433 | |
Chris Lattner | a4d4a85 | 2002-08-19 21:48:40 +0000 | [diff] [blame] | 434 | // Global variable declarations... |
| 435 | if (!M->gempty()) { |
| 436 | Out << "\n/* Global Variable Declarations */\n"; |
| 437 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) { |
| 438 | Out << (I->hasExternalLinkage() ? "extern " : "static "); |
| 439 | printType(I->getType()->getElementType(), getValueName(I)); |
| 440 | Out << ";\n"; |
Chris Lattner | deed7a5 | 2002-05-09 15:18:52 +0000 | [diff] [blame] | 441 | } |
Chris Lattner | deed7a5 | 2002-05-09 15:18:52 +0000 | [diff] [blame] | 442 | } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 443 | |
Chris Lattner | a4d4a85 | 2002-08-19 21:48:40 +0000 | [diff] [blame] | 444 | // Function declarations |
| 445 | if (!M->empty()) { |
| 446 | Out << "\n/* Function Declarations */\n"; |
| 447 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 448 | printFunctionDecl(I); |
| 449 | } |
| 450 | |
| 451 | // Output the global variable contents... |
| 452 | if (!M->gempty()) { |
| 453 | Out << "\n\n/* Global Data */\n"; |
| 454 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) { |
| 455 | if (I->hasInternalLinkage()) Out << "static "; |
| 456 | printType(I->getType()->getElementType(), getValueName(I)); |
| 457 | |
| 458 | if (I->hasInitializer()) { |
| 459 | Out << " = " ; |
| 460 | writeOperand(I->getInitializer()); |
| 461 | } |
| 462 | Out << ";\n"; |
| 463 | } |
| 464 | } |
| 465 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 466 | // Output all of the functions... |
Chris Lattner | a4d4a85 | 2002-08-19 21:48:40 +0000 | [diff] [blame] | 467 | if (!M->empty()) { |
| 468 | Out << "\n\n/* Function Bodies */\n"; |
| 469 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 470 | printFunction(I); |
| 471 | } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 474 | |
| 475 | // printSymbolTable - Run through symbol table looking for named constants |
| 476 | // if a named constant is found, emit it's declaration... |
| 477 | // Assuming that symbol table has only types and constants. |
| 478 | void CWriter::printSymbolTable(const SymbolTable &ST) { |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 479 | for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) { |
| 480 | SymbolTable::type_const_iterator I = ST.type_begin(TI->first); |
| 481 | SymbolTable::type_const_iterator End = ST.type_end(TI->first); |
| 482 | |
Chris Lattner | 3ef6dc7 | 2002-05-09 14:40:11 +0000 | [diff] [blame] | 483 | for (; I != End; ++I) |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 484 | if (const Type *Ty = dyn_cast<StructType>(I->second)) { |
| 485 | string Name = "struct l_" + makeNameProper(I->first); |
Chris Lattner | 3ef6dc7 | 2002-05-09 14:40:11 +0000 | [diff] [blame] | 486 | Out << Name << ";\n"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 487 | |
Chris Lattner | 3ef6dc7 | 2002-05-09 14:40:11 +0000 | [diff] [blame] | 488 | TypeNames.insert(std::make_pair(Ty, Name)); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | Out << "\n"; |
| 493 | |
| 494 | for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) { |
| 495 | SymbolTable::type_const_iterator I = ST.type_begin(TI->first); |
| 496 | SymbolTable::type_const_iterator End = ST.type_end(TI->first); |
| 497 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 498 | for (; I != End; ++I) { |
| 499 | const Value *V = I->second; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 500 | if (const Type *Ty = dyn_cast<Type>(V)) { |
| 501 | string Name = "l_" + makeNameProper(I->first); |
Chris Lattner | 1f02c89 | 2002-05-09 20:14:10 +0000 | [diff] [blame] | 502 | if (isa<StructType>(Ty)) |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 503 | Name = "struct " + makeNameProper(Name); |
Chris Lattner | 1f02c89 | 2002-05-09 20:14:10 +0000 | [diff] [blame] | 504 | else |
| 505 | Out << "typedef "; |
| 506 | |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame^] | 507 | printType(Ty, Name, true); |
| 508 | Out << ";\n"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
Chris Lattner | 3ef6dc7 | 2002-05-09 14:40:11 +0000 | [diff] [blame] | 514 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 515 | // printFunctionDecl - Print function declaration |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 516 | // |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 517 | void CWriter::printFunctionDecl(const Function *F) { |
| 518 | printFunctionSignature(F); |
| 519 | Out << ";\n"; |
| 520 | } |
| 521 | |
| 522 | void CWriter::printFunctionSignature(const Function *F) { |
| 523 | if (F->hasInternalLinkage()) Out << "static "; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 524 | |
| 525 | // Loop over the arguments, printing them... |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 526 | const FunctionType *FT = cast<FunctionType>(F->getFunctionType()); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 527 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 528 | // Print out the return type and name... |
Chris Lattner | 2a7ab2e | 2002-05-09 04:39:00 +0000 | [diff] [blame] | 529 | printType(F->getReturnType()); |
Chris Lattner | 1f02c89 | 2002-05-09 20:14:10 +0000 | [diff] [blame] | 530 | Out << getValueName(F) << "("; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 531 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 532 | if (!F->isExternal()) { |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 533 | if (!F->aempty()) { |
| 534 | printType(F->afront().getType(), getValueName(F->abegin())); |
Chris Lattner | deed7a5 | 2002-05-09 15:18:52 +0000 | [diff] [blame] | 535 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 536 | for (Function::const_aiterator I = ++F->abegin(), E = F->aend(); |
| 537 | I != E; ++I) { |
Chris Lattner | deed7a5 | 2002-05-09 15:18:52 +0000 | [diff] [blame] | 538 | Out << ", "; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 539 | printType(I->getType(), getValueName(I)); |
Chris Lattner | deed7a5 | 2002-05-09 15:18:52 +0000 | [diff] [blame] | 540 | } |
| 541 | } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 542 | } else { |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 543 | // Loop over the arguments, printing them... |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 544 | for (FunctionType::ParamTypes::const_iterator I = |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 545 | FT->getParamTypes().begin(), |
| 546 | E = FT->getParamTypes().end(); I != E; ++I) { |
| 547 | if (I != FT->getParamTypes().begin()) Out << ", "; |
Chris Lattner | 2a7ab2e | 2002-05-09 04:39:00 +0000 | [diff] [blame] | 548 | printType(*I); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | |
| 552 | // Finish printing arguments... |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 553 | if (FT->isVarArg()) { |
| 554 | if (FT->getParamTypes().size()) Out << ", "; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 555 | Out << "..."; // Output varargs portion of signature! |
| 556 | } |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 557 | Out << ")"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 560 | |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 561 | void CWriter::printFunction(Function *F) { |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 562 | if (F->isExternal()) return; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 563 | |
Chris Lattner | f34ee81 | 2002-05-09 03:12:34 +0000 | [diff] [blame] | 564 | Table.incorporateFunction(F); |
| 565 | |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 566 | printFunctionSignature(F); |
| 567 | Out << " {\n"; |
| 568 | |
Chris Lattner | 497e19a | 2002-05-09 20:39:03 +0000 | [diff] [blame] | 569 | // print local variable information for the function |
Chris Lattner | 7683a12 | 2002-05-09 20:33:35 +0000 | [diff] [blame] | 570 | 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] | 571 | if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) { |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 572 | Out << " "; |
Chris Lattner | 3af3ba8 | 2002-05-09 21:31:18 +0000 | [diff] [blame] | 573 | printType((*I)->getType(), getValueName(*I)); |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 574 | Out << ";\n"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 575 | } |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 576 | |
| 577 | // print the basic blocks |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 578 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 579 | BasicBlock *Prev = BB->getPrev(); |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 580 | |
| 581 | // Don't print the label for the basic block if there are no uses, or if the |
| 582 | // only terminator use is the precessor basic block's terminator. We have |
| 583 | // to scan the use list because PHI nodes use basic blocks too but do not |
| 584 | // require a label to be generated. |
| 585 | // |
| 586 | bool NeedsLabel = false; |
| 587 | for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end(); |
| 588 | UI != UE; ++UI) |
| 589 | if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI)) |
| 590 | if (TI != Prev->getTerminator()) { |
| 591 | NeedsLabel = true; |
| 592 | break; |
| 593 | } |
| 594 | |
| 595 | if (NeedsLabel) Out << getValueName(BB) << ":\n"; |
| 596 | |
| 597 | // Output all of the instructions in the basic block... |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 598 | for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II){ |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 599 | if (!isInlinableInst(*II) && !isa<PHINode>(*II)) { |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 600 | if (II->getType() != Type::VoidTy) |
| 601 | outputLValue(II); |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 602 | else |
| 603 | Out << " "; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 604 | visit(*II); |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 605 | Out << ";\n"; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | // Don't emit prefix or suffix for the terminator... |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 610 | visit(*BB->getTerminator()); |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 611 | } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 612 | |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 613 | Out << "}\n\n"; |
Chris Lattner | f34ee81 | 2002-05-09 03:12:34 +0000 | [diff] [blame] | 614 | Table.purgeFunction(); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 617 | // Specific Instruction type classes... note that all of the casts are |
| 618 | // neccesary because we use the instruction classes as opaque types... |
| 619 | // |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 620 | void CWriter::visitReturnInst(ReturnInst &I) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 621 | // 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] | 622 | if (I.getNumOperands() == 0 && |
| 623 | &*--I.getParent()->getParent()->end() == I.getParent() && |
| 624 | !I.getParent()->size() == 1) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 625 | return; |
Chris Lattner | 963b70b | 2002-05-21 18:05:19 +0000 | [diff] [blame] | 626 | } |
Chris Lattner | 4440826 | 2002-05-09 03:50:42 +0000 | [diff] [blame] | 627 | |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 628 | Out << " return"; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 629 | if (I.getNumOperands()) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 630 | Out << " "; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 631 | writeOperand(I.getOperand(0)); |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 632 | } |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 633 | Out << ";\n"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 636 | static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) { |
| 637 | // If PHI nodes need copies, we need the copy code... |
| 638 | if (isa<PHINode>(To->front()) || |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 639 | From->getNext() != To) // Not directly successor, need goto |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 640 | return true; |
| 641 | |
| 642 | // Otherwise we don't need the code. |
| 643 | return false; |
| 644 | } |
| 645 | |
| 646 | void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ, |
| 647 | unsigned Indent) { |
| 648 | for (BasicBlock::iterator I = Succ->begin(); |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 649 | PHINode *PN = dyn_cast<PHINode>(&*I); ++I) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 650 | // now we have to do the printing |
| 651 | Out << string(Indent, ' '); |
| 652 | outputLValue(PN); |
| 653 | writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB))); |
| 654 | Out << "; /* for PHI node */\n"; |
| 655 | } |
| 656 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 657 | if (CurBB->getNext() != Succ) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 658 | Out << string(Indent, ' ') << " goto "; |
| 659 | writeOperand(Succ); |
| 660 | Out << ";\n"; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | // Brach instruction printing - Avoid printing out a brach to a basic block that |
| 665 | // immediately succeeds the current one. |
| 666 | // |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 667 | void CWriter::visitBranchInst(BranchInst &I) { |
| 668 | if (I.isConditional()) { |
| 669 | if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(0))) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 670 | Out << " if ("; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 671 | writeOperand(I.getCondition()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 672 | Out << ") {\n"; |
| 673 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 674 | printBranchToBlock(I.getParent(), I.getSuccessor(0), 2); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 675 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 676 | if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(1))) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 677 | Out << " } else {\n"; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 678 | printBranchToBlock(I.getParent(), I.getSuccessor(1), 2); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 679 | } |
| 680 | } else { |
| 681 | // First goto not neccesary, assume second one is... |
| 682 | Out << " if (!"; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 683 | writeOperand(I.getCondition()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 684 | Out << ") {\n"; |
| 685 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 686 | printBranchToBlock(I.getParent(), I.getSuccessor(1), 2); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | Out << " }\n"; |
| 690 | } else { |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 691 | printBranchToBlock(I.getParent(), I.getSuccessor(0), 0); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 692 | } |
| 693 | Out << "\n"; |
| 694 | } |
| 695 | |
| 696 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 697 | void CWriter::visitBinaryOperator(Instruction &I) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 698 | // binary instructions, shift instructions, setCond instructions. |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 699 | if (isa<PointerType>(I.getType())) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 700 | Out << "("; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 701 | printType(I.getType()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 702 | Out << ")"; |
| 703 | } |
| 704 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 705 | if (isa<PointerType>(I.getType())) Out << "(long long)"; |
| 706 | writeOperand(I.getOperand(0)); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 707 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 708 | switch (I.getOpcode()) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 709 | case Instruction::Add: Out << " + "; break; |
| 710 | case Instruction::Sub: Out << " - "; break; |
| 711 | case Instruction::Mul: Out << "*"; break; |
| 712 | case Instruction::Div: Out << "/"; break; |
| 713 | case Instruction::Rem: Out << "%"; break; |
| 714 | case Instruction::And: Out << " & "; break; |
| 715 | case Instruction::Or: Out << " | "; break; |
| 716 | case Instruction::Xor: Out << " ^ "; break; |
| 717 | case Instruction::SetEQ: Out << " == "; break; |
| 718 | case Instruction::SetNE: Out << " != "; break; |
| 719 | case Instruction::SetLE: Out << " <= "; break; |
| 720 | case Instruction::SetGE: Out << " >= "; break; |
| 721 | case Instruction::SetLT: Out << " < "; break; |
| 722 | case Instruction::SetGT: Out << " > "; break; |
| 723 | case Instruction::Shl : Out << " << "; break; |
| 724 | case Instruction::Shr : Out << " >> "; break; |
Chris Lattner | e7f65d3b | 2002-06-30 16:07:20 +0000 | [diff] [blame] | 725 | default: std::cerr << "Invalid operator type!" << I; abort(); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 728 | if (isa<PointerType>(I.getType())) Out << "(long long)"; |
| 729 | writeOperand(I.getOperand(1)); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 732 | void CWriter::visitCastInst(CastInst &I) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 733 | Out << "("; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 734 | printType(I.getType()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 735 | Out << ")"; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 736 | writeOperand(I.getOperand(0)); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 739 | void CWriter::visitCallInst(CallInst &I) { |
| 740 | const PointerType *PTy = cast<PointerType>(I.getCalledValue()->getType()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 741 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 742 | const Type *RetTy = FTy->getReturnType(); |
| 743 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 744 | Out << getValueName(I.getOperand(0)) << "("; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 745 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 746 | if (I.getNumOperands() > 1) { |
| 747 | writeOperand(I.getOperand(1)); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 748 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 749 | for (unsigned op = 2, Eop = I.getNumOperands(); op != Eop; ++op) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 750 | Out << ", "; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 751 | writeOperand(I.getOperand(op)); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 752 | } |
| 753 | } |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 754 | Out << ")"; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 757 | void CWriter::visitMallocInst(MallocInst &I) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 758 | Out << "("; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 759 | printType(I.getType()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 760 | Out << ")malloc(sizeof("; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 761 | printType(I.getType()->getElementType()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 762 | Out << ")"; |
| 763 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 764 | if (I.isArrayAllocation()) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 765 | Out << " * " ; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 766 | writeOperand(I.getOperand(0)); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 767 | } |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 768 | Out << ")"; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 771 | void CWriter::visitAllocaInst(AllocaInst &I) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 772 | Out << "("; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 773 | printType(I.getType()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 774 | Out << ") alloca(sizeof("; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 775 | printType(I.getType()->getElementType()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 776 | Out << ")"; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 777 | if (I.isArrayAllocation()) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 778 | Out << " * " ; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 779 | writeOperand(I.getOperand(0)); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 780 | } |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 781 | Out << ")"; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 784 | void CWriter::visitFreeInst(FreeInst &I) { |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 785 | Out << "free("; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 786 | writeOperand(I.getOperand(0)); |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 787 | Out << ")"; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 790 | void CWriter::printIndexingExpr(MemAccessInst &MAI) { |
| 791 | MemAccessInst::op_iterator I = MAI.idx_begin(), E = MAI.idx_end(); |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 792 | if (I == E) { |
| 793 | // If accessing a global value with no indexing, avoid *(&GV) syndrome |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 794 | if (GlobalValue *V = dyn_cast<GlobalValue>(MAI.getPointerOperand())) { |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 795 | writeOperandInternal(V); |
| 796 | return; |
| 797 | } |
| 798 | |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 799 | Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]' |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 800 | } |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 801 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 802 | writeOperand(MAI.getPointerOperand()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 803 | |
| 804 | if (I == E) return; |
| 805 | |
| 806 | // Print out the -> operator if possible... |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 807 | const Constant *CI = dyn_cast<Constant>(I->get()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 808 | if (CI && CI->isNullValue() && I+1 != E && |
| 809 | (*(I+1))->getType() == Type::UByteTy) { |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 810 | Out << "->field" << cast<ConstantUInt>(*(I+1))->getValue(); |
| 811 | I += 2; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | for (; I != E; ++I) |
| 815 | if ((*I)->getType() == Type::UIntTy) { |
| 816 | Out << "["; |
| 817 | writeOperand(*I); |
| 818 | Out << "]"; |
| 819 | } else { |
| 820 | Out << ".field" << cast<ConstantUInt>(*I)->getValue(); |
| 821 | } |
| 822 | } |
| 823 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 824 | void CWriter::visitLoadInst(LoadInst &I) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 825 | printIndexingExpr(I); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 828 | void CWriter::visitStoreInst(StoreInst &I) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 829 | printIndexingExpr(I); |
| 830 | Out << " = "; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 831 | writeOperand(I.getOperand(0)); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 834 | void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 835 | Out << "&"; |
| 836 | printIndexingExpr(I); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 837 | } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 838 | |
| 839 | //===----------------------------------------------------------------------===// |
| 840 | // External Interface declaration |
| 841 | //===----------------------------------------------------------------------===// |
| 842 | |
Chris Lattner | f34ee81 | 2002-05-09 03:12:34 +0000 | [diff] [blame] | 843 | void WriteToC(const Module *M, ostream &Out) { |
| 844 | assert(M && "You can't write a null module!!"); |
| 845 | SlotCalculator SlotTable(M, false); |
| 846 | CWriter W(Out, SlotTable, M); |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 847 | W.write((Module*)M); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 848 | Out.flush(); |
| 849 | } |