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