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