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