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