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" |
| 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; |
| 34 | using std::vector; |
| 35 | using std::ostream; |
| 36 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 37 | //===-----------------------------------------------------------------------==// |
| 38 | // |
| 39 | // Implementation of the CLocalVars methods |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 40 | |
| 41 | // Appends a variable to the LocalVars map if it does not already exist |
| 42 | // Also check that the type exists on the map. |
| 43 | void CLocalVars::addLocalVar(const Type *t, const string & var) { |
| 44 | if (!LocalVars.count(t) || |
| 45 | find(LocalVars[t].begin(), LocalVars[t].end(), var) |
| 46 | == LocalVars[t].end()) { |
| 47 | LocalVars[t].push_back(var); |
| 48 | } |
| 49 | } |
| 50 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 51 | static string calcTypeNameVar(const Type *Ty, |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 52 | map<const Type *, string> &TypeNames, |
| 53 | string VariableName, string NameSoFar); |
| 54 | |
| 55 | static std::string getConstStrValue(const Constant* CPV); |
| 56 | |
| 57 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 58 | // We dont want identifier names with ., space, - in them. |
| 59 | // So we replace them with _ |
| 60 | static string makeNameProper(string x) { |
| 61 | string tmp; |
| 62 | for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++) { |
| 63 | if (*sI == '.') |
| 64 | tmp += '_'; |
| 65 | else if (*sI == ' ') |
| 66 | tmp += '_'; |
| 67 | else if (*sI == '-') |
| 68 | tmp += "__"; |
| 69 | else |
| 70 | tmp += *sI; |
| 71 | } |
| 72 | return tmp; |
| 73 | } |
| 74 | |
| 75 | static string getConstantName(const Constant *CPV) { |
| 76 | return CPV->getName(); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | static std::string getConstArrayStrValue(const Constant* CPV) { |
| 81 | std::string Result; |
| 82 | |
| 83 | // As a special case, print the array as a string if it is an array of |
| 84 | // ubytes or an array of sbytes with positive values. |
| 85 | // |
| 86 | const Type *ETy = cast<ArrayType>(CPV->getType())->getElementType(); |
| 87 | bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy); |
| 88 | |
| 89 | if (ETy == Type::SByteTy) { |
| 90 | for (unsigned i = 0; i < CPV->getNumOperands(); ++i) |
| 91 | if (ETy == Type::SByteTy && |
| 92 | cast<ConstantSInt>(CPV->getOperand(i))->getValue() < 0) { |
| 93 | isString = false; |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | if (isString) { |
| 99 | Result = "\""; |
| 100 | for (unsigned i = 0; i < CPV->getNumOperands(); ++i) { |
| 101 | unsigned char C = (ETy == Type::SByteTy) ? |
| 102 | (unsigned char)cast<ConstantSInt>(CPV->getOperand(i))->getValue() : |
| 103 | (unsigned char)cast<ConstantUInt>(CPV->getOperand(i))->getValue(); |
| 104 | |
| 105 | if (isprint(C)) { |
| 106 | Result += C; |
| 107 | } else { |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 108 | Result += "\\x"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 109 | Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'); |
| 110 | Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'); |
| 111 | } |
| 112 | } |
| 113 | Result += "\""; |
| 114 | |
| 115 | } else { |
| 116 | Result = "{"; |
| 117 | if (CPV->getNumOperands()) { |
| 118 | Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0))); |
| 119 | for (unsigned i = 1; i < CPV->getNumOperands(); i++) |
| 120 | Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i))); |
| 121 | } |
| 122 | Result += " }"; |
| 123 | } |
| 124 | |
| 125 | return Result; |
| 126 | } |
| 127 | |
| 128 | static std::string getConstStructStrValue(const Constant* CPV) { |
| 129 | std::string Result = "{"; |
| 130 | if (CPV->getNumOperands()) { |
| 131 | Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0))); |
| 132 | for (unsigned i = 1; i < CPV->getNumOperands(); i++) |
| 133 | Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i))); |
| 134 | } |
| 135 | |
| 136 | return Result + " }"; |
| 137 | } |
| 138 | |
| 139 | // our own getStrValue function for constant initializers |
| 140 | static std::string getConstStrValue(const Constant* CPV) { |
| 141 | // Does not handle null pointers, that needs to be checked explicitly |
| 142 | string tempstr; |
| 143 | if (CPV == ConstantBool::False) |
| 144 | return "0"; |
| 145 | else if (CPV == ConstantBool::True) |
| 146 | return "1"; |
| 147 | |
| 148 | else if (isa<ConstantArray>(CPV)) { |
| 149 | tempstr = getConstArrayStrValue(CPV); |
| 150 | } |
| 151 | else if (isa<ConstantStruct>(CPV)) { |
| 152 | tempstr = getConstStructStrValue(CPV); |
| 153 | } |
| 154 | else if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(CPV)) { |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 155 | tempstr = utostr(CUI->getValue()); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 156 | } |
| 157 | else if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(CPV)) { |
| 158 | tempstr = itostr(CSI->getValue()); |
| 159 | } |
| 160 | else if (ConstantFP *CFP = dyn_cast<ConstantFP>(CPV)) { |
| 161 | tempstr = ftostr(CFP->getValue()); |
| 162 | } |
| 163 | |
| 164 | if (CPV->getType() == Type::ULongTy) |
| 165 | tempstr += "ull"; |
| 166 | else if (CPV->getType() == Type::LongTy) |
| 167 | tempstr += "ll"; |
| 168 | else if (CPV->getType() == Type::UIntTy || |
| 169 | CPV->getType() == Type::UShortTy) |
| 170 | tempstr += "u"; |
| 171 | |
| 172 | return tempstr; |
| 173 | |
| 174 | } |
| 175 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 176 | // Internal function |
| 177 | // Essentially pass the Type* variable, an empty typestack and this prints |
| 178 | // out the C type |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 179 | static string calcTypeName(const Type *Ty, map<const Type *, string> &TypeNames, |
| 180 | string &FunctionInfo) { |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 181 | |
| 182 | // Takin' care of the fact that boolean would be int in C |
| 183 | // and that ushort would be unsigned short etc. |
| 184 | |
| 185 | // Base Case |
| 186 | if (Ty->isPrimitiveType()) |
| 187 | switch (Ty->getPrimitiveID()) { |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 188 | case Type::VoidTyID: return "void"; |
| 189 | case Type::BoolTyID: return "bool"; |
| 190 | case Type::UByteTyID: return "unsigned char"; |
| 191 | case Type::SByteTyID: return "signed char"; |
| 192 | case Type::UShortTyID: return "unsigned short"; |
| 193 | case Type::ShortTyID: return "short"; |
| 194 | case Type::UIntTyID: return "unsigned"; |
| 195 | case Type::IntTyID: return "int"; |
| 196 | case Type::ULongTyID: return "unsigned long long"; |
| 197 | case Type::LongTyID: return "signed long long"; |
| 198 | case Type::FloatTyID: return "float"; |
| 199 | case Type::DoubleTyID: return "double"; |
| 200 | default : assert(0 && "Unknown primitive type!"); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | // Check to see if the type is named. |
| 204 | map<const Type *, string>::iterator I = TypeNames.find(Ty); |
| 205 | if (I != TypeNames.end()) |
| 206 | return I->second; |
| 207 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 208 | string Result; |
| 209 | string MInfo = ""; |
| 210 | switch (Ty->getPrimitiveID()) { |
| 211 | case Type::FunctionTyID: { |
| 212 | const FunctionType *MTy = cast<const FunctionType>(Ty); |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 213 | Result = calcTypeName(MTy->getReturnType(), TypeNames, MInfo); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 214 | if (MInfo != "") |
| 215 | Result += ") " + MInfo; |
| 216 | Result += "("; |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 217 | FunctionInfo += " ("; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 218 | for (FunctionType::ParamTypes::const_iterator |
| 219 | I = MTy->getParamTypes().begin(), |
| 220 | E = MTy->getParamTypes().end(); I != E; ++I) { |
| 221 | if (I != MTy->getParamTypes().begin()) |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 222 | FunctionInfo += ", "; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 223 | MInfo = ""; |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 224 | FunctionInfo += calcTypeName(*I, TypeNames, MInfo); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 225 | if (MInfo != "") |
| 226 | Result += ") " + MInfo; |
| 227 | } |
| 228 | if (MTy->isVarArg()) { |
| 229 | if (!MTy->getParamTypes().empty()) |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 230 | FunctionInfo += ", "; |
| 231 | FunctionInfo += "..."; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 232 | } |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 233 | FunctionInfo += ")"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 234 | break; |
| 235 | } |
| 236 | case Type::StructTyID: { |
| 237 | string tempstr = ""; |
| 238 | const StructType *STy = cast<const StructType>(Ty); |
| 239 | Result = " struct {\n "; |
| 240 | int indx = 0; |
| 241 | for (StructType::ElementTypes::const_iterator |
| 242 | I = STy->getElementTypes().begin(), |
| 243 | E = STy->getElementTypes().end(); I != E; ++I) { |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 244 | Result += calcTypeNameVar(*I, TypeNames, |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 245 | "field" + itostr(indx++), tempstr); |
| 246 | Result += ";\n "; |
| 247 | } |
| 248 | Result += " } "; |
| 249 | break; |
| 250 | } |
| 251 | case Type::PointerTyID: |
| 252 | Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(), |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 253 | TypeNames, MInfo); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 254 | Result += "*"; |
| 255 | break; |
| 256 | case Type::ArrayTyID: { |
| 257 | const ArrayType *ATy = cast<const ArrayType>(Ty); |
| 258 | int NumElements = ATy->getNumElements(); |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 259 | Result = calcTypeName(ATy->getElementType(), TypeNames, MInfo); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 260 | Result += "*"; |
| 261 | break; |
| 262 | } |
| 263 | default: |
| 264 | assert(0 && "Unhandled case in getTypeProps!"); |
| 265 | Result = "<error>"; |
| 266 | } |
| 267 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 268 | return Result; |
| 269 | } |
| 270 | |
| 271 | // Internal function |
| 272 | // Pass the Type* variable and and the variable name and this prints out the |
| 273 | // variable declaration. |
| 274 | // This is different from calcTypeName because if you need to declare an array |
| 275 | // the size of the array would appear after the variable name itself |
| 276 | // For eg. int a[10]; |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 277 | static string calcTypeNameVar(const Type *Ty, |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 278 | map<const Type *, string> &TypeNames, |
| 279 | string VariableName, string NameSoFar) { |
| 280 | if (Ty->isPrimitiveType()) |
| 281 | switch (Ty->getPrimitiveID()) { |
| 282 | case Type::BoolTyID: |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 283 | return "bool " + NameSoFar + VariableName; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 284 | case Type::UByteTyID: |
| 285 | return "unsigned char " + NameSoFar + VariableName; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 286 | case Type::SByteTyID: |
| 287 | return "signed char " + NameSoFar + VariableName; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 288 | case Type::UShortTyID: |
| 289 | return "unsigned long long " + NameSoFar + VariableName; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 290 | case Type::ULongTyID: |
| 291 | return "unsigned long long " + NameSoFar + VariableName; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 292 | case Type::LongTyID: |
| 293 | return "signed long long " + NameSoFar + VariableName; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 294 | case Type::UIntTyID: |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 295 | return "unsigned " + NameSoFar + VariableName; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 296 | default : |
| 297 | return Ty->getDescription() + " " + NameSoFar + VariableName; |
| 298 | } |
| 299 | |
| 300 | // Check to see if the type is named. |
| 301 | map<const Type *, string>::iterator I = TypeNames.find(Ty); |
| 302 | if (I != TypeNames.end()) |
| 303 | return I->second + " " + NameSoFar + VariableName; |
| 304 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 305 | string Result; |
| 306 | string tempstr = ""; |
| 307 | |
| 308 | switch (Ty->getPrimitiveID()) { |
| 309 | case Type::FunctionTyID: { |
| 310 | string MInfo = ""; |
| 311 | const FunctionType *MTy = cast<const FunctionType>(Ty); |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 312 | Result += calcTypeName(MTy->getReturnType(), TypeNames, MInfo); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 313 | if (MInfo != "") |
| 314 | Result += ") " + MInfo; |
| 315 | Result += " " + NameSoFar + VariableName; |
| 316 | Result += " ("; |
| 317 | for (FunctionType::ParamTypes::const_iterator |
| 318 | I = MTy->getParamTypes().begin(), |
| 319 | E = MTy->getParamTypes().end(); I != E; ++I) { |
| 320 | if (I != MTy->getParamTypes().begin()) |
| 321 | Result += ", "; |
| 322 | MInfo = ""; |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 323 | Result += calcTypeName(*I, TypeNames, MInfo); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 324 | if (MInfo != "") |
| 325 | Result += ") " + MInfo; |
| 326 | } |
| 327 | if (MTy->isVarArg()) { |
| 328 | if (!MTy->getParamTypes().empty()) |
| 329 | Result += ", "; |
| 330 | Result += "..."; |
| 331 | } |
| 332 | Result += ")"; |
| 333 | break; |
| 334 | } |
| 335 | case Type::StructTyID: { |
| 336 | const StructType *STy = cast<const StructType>(Ty); |
| 337 | Result = " struct {\n "; |
| 338 | int indx = 0; |
| 339 | for (StructType::ElementTypes::const_iterator |
| 340 | I = STy->getElementTypes().begin(), |
| 341 | E = STy->getElementTypes().end(); I != E; ++I) { |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 342 | Result += calcTypeNameVar(*I, TypeNames, |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 343 | "field" + itostr(indx++), ""); |
| 344 | Result += ";\n "; |
| 345 | } |
| 346 | Result += " }"; |
| 347 | Result += " " + NameSoFar + VariableName; |
| 348 | break; |
| 349 | } |
| 350 | |
| 351 | case Type::PointerTyID: { |
| 352 | Result = calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(), |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 353 | TypeNames, tempstr, |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 354 | "(*" + NameSoFar + VariableName + ")"); |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | case Type::ArrayTyID: { |
| 359 | const ArrayType *ATy = cast<const ArrayType>(Ty); |
| 360 | int NumElements = ATy->getNumElements(); |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 361 | Result = calcTypeNameVar(ATy->getElementType(), TypeNames, |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 362 | tempstr, NameSoFar + VariableName + "[" + |
| 363 | itostr(NumElements) + "]"); |
| 364 | break; |
| 365 | } |
| 366 | default: |
| 367 | assert(0 && "Unhandled case in getTypeProps!"); |
| 368 | Result = "<error>"; |
| 369 | } |
| 370 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 371 | return Result; |
| 372 | } |
| 373 | |
| 374 | // printTypeVarInt - The internal guts of printing out a type that has a |
| 375 | // potentially named portion and the variable associated with the type. |
| 376 | static ostream &printTypeVarInt(ostream &Out, const Type *Ty, |
| 377 | map<const Type *, string> &TypeNames, |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 378 | const string &VariableName) { |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 379 | // Primitive types always print out their description, regardless of whether |
| 380 | // they have been named or not. |
| 381 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 382 | if (Ty->isPrimitiveType()) |
| 383 | switch (Ty->getPrimitiveID()) { |
| 384 | case Type::BoolTyID: |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 385 | return Out << "bool " << VariableName; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 386 | case Type::UByteTyID: |
| 387 | return Out << "unsigned char " << VariableName; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 388 | case Type::SByteTyID: |
| 389 | return Out << "signed char " << VariableName; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 390 | case Type::UShortTyID: |
| 391 | return Out << "unsigned long long " << VariableName; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 392 | case Type::ULongTyID: |
| 393 | return Out << "unsigned long long " << VariableName; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 394 | case Type::LongTyID: |
| 395 | return Out << "signed long long " << VariableName; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 396 | case Type::UIntTyID: |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 397 | return Out << "unsigned " << VariableName; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 398 | default : |
| 399 | return Out << Ty->getDescription() << " " << VariableName; |
| 400 | } |
| 401 | |
| 402 | // Check to see if the type is named. |
| 403 | map<const Type *, string>::iterator I = TypeNames.find(Ty); |
| 404 | if (I != TypeNames.end()) return Out << I->second << " " << VariableName; |
| 405 | |
| 406 | // Otherwise we have a type that has not been named but is a derived type. |
| 407 | // Carefully recurse the type hierarchy to print out any contained symbolic |
| 408 | // names. |
| 409 | // |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 410 | string TypeNameVar, tempstr = ""; |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 411 | TypeNameVar = calcTypeNameVar(Ty, TypeNames, VariableName, tempstr); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 412 | return Out << TypeNameVar; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | // Internal guts of printing a type name |
| 416 | static ostream &printTypeInt(ostream &Out, const Type *Ty, |
| 417 | map<const Type *, string> &TypeNames) { |
| 418 | // Primitive types always print out their description, regardless of whether |
| 419 | // they have been named or not. |
| 420 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 421 | if (Ty->isPrimitiveType()) |
| 422 | switch (Ty->getPrimitiveID()) { |
| 423 | case Type::BoolTyID: |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 424 | return Out << "bool"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 425 | case Type::UByteTyID: |
| 426 | return Out << "unsigned char"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 427 | case Type::SByteTyID: |
| 428 | return Out << "signed char"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 429 | case Type::UShortTyID: |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 430 | return Out << "unsigned short"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 431 | case Type::ULongTyID: |
| 432 | return Out << "unsigned long long"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 433 | case Type::LongTyID: |
| 434 | return Out << "signed long long"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 435 | case Type::UIntTyID: |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 436 | return Out << "unsigned"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 437 | default : |
| 438 | return Out << Ty->getDescription(); |
| 439 | } |
| 440 | |
| 441 | // Check to see if the type is named. |
| 442 | map<const Type *, string>::iterator I = TypeNames.find(Ty); |
| 443 | if (I != TypeNames.end()) return Out << I->second; |
| 444 | |
| 445 | // Otherwise we have a type that has not been named but is a derived type. |
| 446 | // Carefully recurse the type hierarchy to print out any contained symbolic |
| 447 | // names. |
| 448 | // |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 449 | string MInfo; |
| 450 | string TypeName = calcTypeName(Ty, TypeNames, MInfo); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 451 | // TypeNames.insert(std::make_pair(Ty, TypeName)); |
| 452 | //Cache type name for later use |
| 453 | if (MInfo != "") |
| 454 | return Out << TypeName << ")" << MInfo; |
| 455 | else |
| 456 | return Out << TypeName; |
| 457 | } |
| 458 | |
| 459 | namespace { |
| 460 | |
| 461 | //Internal CWriter class mimics AssemblyWriter. |
| 462 | class CWriter { |
| 463 | ostream& Out; |
| 464 | SlotCalculator &Table; |
| 465 | const Module *TheModule; |
| 466 | map<const Type *, string> TypeNames; |
| 467 | public: |
| 468 | inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M) |
| 469 | : Out(o), Table(Tab), TheModule(M) { |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 472 | inline void write(const Module *M) { printModule(M); } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 473 | |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 474 | ostream& printTypeVar(const Type *Ty, const string &VariableName) { |
| 475 | return printTypeVarInt(Out, Ty, TypeNames, VariableName); |
| 476 | } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 477 | |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 478 | |
| 479 | |
| 480 | ostream& printType(const Type *Ty, ostream &Out); |
| 481 | void writeOperand(const Value *Operand, ostream &Out,bool PrintName = true); |
| 482 | |
| 483 | string getValueName(const Value *V); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 484 | private : |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 485 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 486 | void printModule(const Module *M); |
| 487 | void printSymbolTable(const SymbolTable &ST); |
| 488 | void printConstant(const Constant *CPV); |
| 489 | void printGlobal(const GlobalVariable *GV); |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 490 | void printFunctionSignature(const Function *F); |
| 491 | void printFunctionDecl(const Function *F); // Print just the forward decl |
| 492 | void printFunctionArgument(const Argument *FA); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 493 | |
| 494 | void printFunction(const Function *); |
| 495 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 496 | void outputBasicBlock(const BasicBlock *); |
| 497 | }; |
| 498 | /* END class CWriter */ |
| 499 | |
| 500 | |
| 501 | /* CLASS InstLocalVarsVisitor */ |
| 502 | class InstLocalVarsVisitor : public InstVisitor<InstLocalVarsVisitor> { |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 503 | CWriter& CW; |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 504 | void handleTerminator(TerminatorInst *tI, int indx); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 505 | public: |
| 506 | CLocalVars CLV; |
| 507 | |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 508 | InstLocalVarsVisitor(CWriter &cw) : CW(cw) {} |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 509 | |
| 510 | void visitInstruction(Instruction *I) { |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 511 | if (I->getType() != Type::VoidTy) { |
| 512 | string tempostr = CW.getValueName(I); |
| 513 | CLV.addLocalVar(I->getType(), tempostr); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 514 | } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | void visitBranchInst(BranchInst *I) { |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 518 | handleTerminator(I, 0); |
| 519 | if (I->isConditional()) |
| 520 | handleTerminator(I, 1); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 521 | } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 522 | }; |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 523 | } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 524 | |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 525 | void InstLocalVarsVisitor::handleTerminator(TerminatorInst *tI,int indx) { |
| 526 | BasicBlock *bb = tI->getSuccessor(indx); |
| 527 | |
| 528 | BasicBlock::const_iterator insIt = bb->begin(); |
| 529 | while (insIt != bb->end()) { |
| 530 | if (const PHINode *pI = dyn_cast<PHINode>(*insIt)) { |
| 531 | // Its a phinode! |
| 532 | // Calculate the incoming index for this |
| 533 | assert(pI->getBasicBlockIndex(tI->getParent()) != -1); |
| 534 | |
| 535 | CLV.addLocalVar(pI->getType(), CW.getValueName(pI)); |
| 536 | } else |
| 537 | break; |
| 538 | insIt++; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | namespace { |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 543 | /* CLASS CInstPrintVisitor */ |
| 544 | |
| 545 | class CInstPrintVisitor: public InstVisitor<CInstPrintVisitor> { |
| 546 | CWriter& CW; |
| 547 | SlotCalculator& Table; |
| 548 | ostream &Out; |
| 549 | const Value *Operand; |
| 550 | |
| 551 | void outputLValue(Instruction *); |
| 552 | void printPhiFromNextBlock(TerminatorInst *tI, int indx); |
Chris Lattner | 4440826 | 2002-05-09 03:50:42 +0000 | [diff] [blame^] | 553 | void printIndexingExpr(MemAccessInst *MAI); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 554 | |
| 555 | public: |
| 556 | CInstPrintVisitor (CWriter &cw, SlotCalculator& table, ostream& o) |
Chris Lattner | 4440826 | 2002-05-09 03:50:42 +0000 | [diff] [blame^] | 557 | : CW(cw), Table(table), Out(o) {} |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 558 | |
| 559 | void visitCastInst(CastInst *I); |
| 560 | void visitCallInst(CallInst *I); |
Chris Lattner | 2f5f51a | 2002-05-09 03:28:37 +0000 | [diff] [blame] | 561 | void visitShiftInst(ShiftInst *I) { visitBinaryOperator(I); } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 562 | void visitReturnInst(ReturnInst *I); |
| 563 | void visitBranchInst(BranchInst *I); |
| 564 | void visitSwitchInst(SwitchInst *I); |
| 565 | void visitInvokeInst(InvokeInst *I) ; |
| 566 | void visitMallocInst(MallocInst *I); |
| 567 | void visitAllocaInst(AllocaInst *I); |
| 568 | void visitFreeInst(FreeInst *I); |
| 569 | void visitLoadInst(LoadInst *I); |
| 570 | void visitStoreInst(StoreInst *I); |
| 571 | void visitGetElementPtrInst(GetElementPtrInst *I); |
Chris Lattner | 2f5f51a | 2002-05-09 03:28:37 +0000 | [diff] [blame] | 572 | void visitPHINode(PHINode *I) {} |
| 573 | |
| 574 | void visitNot(GenericUnaryInst *I); |
| 575 | void visitBinaryOperator(Instruction *I); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 576 | }; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 579 | void CInstPrintVisitor::outputLValue(Instruction *I) { |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 580 | Out << " " << CW.getValueName(I) << " = "; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | void CInstPrintVisitor::printPhiFromNextBlock(TerminatorInst *tI, int indx) { |
| 584 | BasicBlock *bb = tI->getSuccessor(indx); |
| 585 | BasicBlock::const_iterator insIt = bb->begin(); |
| 586 | while (insIt != bb->end()) { |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 587 | if (PHINode *pI = dyn_cast<PHINode>(*insIt)) { |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 588 | //Its a phinode! |
| 589 | //Calculate the incoming index for this |
| 590 | int incindex = pI->getBasicBlockIndex(tI->getParent()); |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 591 | if (incindex != -1) { |
| 592 | //now we have to do the printing |
| 593 | outputLValue(pI); |
| 594 | CW.writeOperand(pI->getIncomingValue(incindex), Out); |
| 595 | Out << ";\n"; |
| 596 | } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 597 | } |
| 598 | else break; |
| 599 | insIt++; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | // Implement all "other" instructions, except for PHINode |
| 604 | void CInstPrintVisitor::visitCastInst(CastInst *I) { |
| 605 | outputLValue(I); |
| 606 | Operand = I->getNumOperands() ? I->getOperand(0) : 0; |
| 607 | Out << "("; |
| 608 | CW.printType(I->getType(), Out); |
| 609 | Out << ")"; |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 610 | CW.writeOperand(Operand, Out); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 611 | Out << ";\n"; |
| 612 | } |
| 613 | |
| 614 | void CInstPrintVisitor::visitCallInst(CallInst *I) { |
Chris Lattner | f34ee81 | 2002-05-09 03:12:34 +0000 | [diff] [blame] | 615 | if (I->getType() != Type::VoidTy) |
| 616 | outputLValue(I); |
Chris Lattner | 2f5f51a | 2002-05-09 03:28:37 +0000 | [diff] [blame] | 617 | else |
| 618 | Out << " "; |
Chris Lattner | f34ee81 | 2002-05-09 03:12:34 +0000 | [diff] [blame] | 619 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 620 | Operand = I->getNumOperands() ? I->getOperand(0) : 0; |
| 621 | const PointerType *PTy = dyn_cast<PointerType>(Operand->getType()); |
| 622 | const FunctionType *MTy = PTy |
| 623 | ? dyn_cast<FunctionType>(PTy->getElementType()):0; |
| 624 | const Type *RetTy = MTy ? MTy->getReturnType() : 0; |
| 625 | |
| 626 | // If possible, print out the short form of the call instruction, but we can |
| 627 | // only do this if the first argument is a pointer to a nonvararg method, |
| 628 | // and if the value returned is not a pointer to a method. |
| 629 | // |
| 630 | if (RetTy && !MTy->isVarArg() && |
| 631 | (!isa<PointerType>(RetTy)|| |
| 632 | !isa<FunctionType>(cast<PointerType>(RetTy)))){ |
| 633 | Out << " "; |
| 634 | Out << makeNameProper(Operand->getName()); |
| 635 | } else { |
| 636 | Out << makeNameProper(Operand->getName()); |
| 637 | } |
| 638 | Out << "("; |
| 639 | if (I->getNumOperands() > 1) |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 640 | CW.writeOperand(I->getOperand(1), Out); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 641 | for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) { |
| 642 | Out << ","; |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 643 | CW.writeOperand(I->getOperand(op), Out); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | Out << " );\n"; |
| 647 | } |
| 648 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 649 | // Specific Instruction type classes... note that all of the casts are |
| 650 | // neccesary because we use the instruction classes as opaque types... |
| 651 | // |
| 652 | void CInstPrintVisitor::visitReturnInst(ReturnInst *I) { |
Chris Lattner | 2f5f51a | 2002-05-09 03:28:37 +0000 | [diff] [blame] | 653 | Out << " return "; |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 654 | if (I->getNumOperands()) |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 655 | CW.writeOperand(I->getOperand(0), Out); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 656 | Out << ";\n"; |
| 657 | } |
| 658 | |
| 659 | void CInstPrintVisitor::visitBranchInst(BranchInst *I) { |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 660 | TerminatorInst *tI = cast<TerminatorInst>(I); |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 661 | if (I->isConditional()) { |
| 662 | Out << " if ("; |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 663 | CW.writeOperand(I->getCondition(), Out); |
Chris Lattner | 2f5f51a | 2002-05-09 03:28:37 +0000 | [diff] [blame] | 664 | Out << ") {\n"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 665 | printPhiFromNextBlock(tI,0); |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 666 | Out << " goto "; |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 667 | CW.writeOperand(I->getOperand(0), Out); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 668 | Out << ";\n"; |
Chris Lattner | 2f5f51a | 2002-05-09 03:28:37 +0000 | [diff] [blame] | 669 | Out << " } else {\n"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 670 | printPhiFromNextBlock(tI,1); |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 671 | Out << " goto "; |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 672 | CW.writeOperand(I->getOperand(1), Out); |
Chris Lattner | 2f5f51a | 2002-05-09 03:28:37 +0000 | [diff] [blame] | 673 | Out << ";\n }\n"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 674 | } else { |
| 675 | printPhiFromNextBlock(tI,0); |
| 676 | Out << " goto "; |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 677 | CW.writeOperand(I->getOperand(0), Out); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 678 | Out << ";\n"; |
| 679 | } |
| 680 | Out << "\n"; |
| 681 | } |
| 682 | |
| 683 | void CInstPrintVisitor::visitSwitchInst(SwitchInst *I) { |
Chris Lattner | f34ee81 | 2002-05-09 03:12:34 +0000 | [diff] [blame] | 684 | assert(0 && "Switch not implemented!"); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | void CInstPrintVisitor::visitInvokeInst(InvokeInst *I) { |
Chris Lattner | f34ee81 | 2002-05-09 03:12:34 +0000 | [diff] [blame] | 688 | assert(0 && "Invoke not implemented!"); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | void CInstPrintVisitor::visitMallocInst(MallocInst *I) { |
| 692 | outputLValue(I); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 693 | Out << "("; |
Chris Lattner | 2f5f51a | 2002-05-09 03:28:37 +0000 | [diff] [blame] | 694 | CW.printType(I->getType()->getElementType(), Out); |
| 695 | Out << "*)malloc(sizeof("; |
| 696 | CW.printTypeVar(I->getType()->getElementType(), ""); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 697 | Out << ")"; |
Chris Lattner | 2f5f51a | 2002-05-09 03:28:37 +0000 | [diff] [blame] | 698 | |
| 699 | if (I->isArrayAllocation()) { |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 700 | Out << " * " ; |
Chris Lattner | 2f5f51a | 2002-05-09 03:28:37 +0000 | [diff] [blame] | 701 | CW.writeOperand(I->getOperand(0), Out); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 702 | } |
| 703 | Out << ");"; |
| 704 | } |
| 705 | |
| 706 | void CInstPrintVisitor::visitAllocaInst(AllocaInst *I) { |
| 707 | outputLValue(I); |
| 708 | Operand = I->getNumOperands() ? I->getOperand(0) : 0; |
| 709 | string tempstr = ""; |
| 710 | Out << "("; |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 711 | CW.printTypeVar(I->getType(), tempstr); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 712 | Out << ") alloca(sizeof("; |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 713 | CW.printTypeVar(cast<PointerType>(I->getType())->getElementType(), |
| 714 | tempstr); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 715 | Out << ")"; |
| 716 | if (I->getNumOperands()) { |
| 717 | Out << " * " ; |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 718 | CW.writeOperand(Operand, Out); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 719 | } |
| 720 | Out << ");\n"; |
| 721 | } |
| 722 | |
| 723 | void CInstPrintVisitor::visitFreeInst(FreeInst *I) { |
| 724 | Operand = I->getNumOperands() ? I->getOperand(0) : 0; |
| 725 | Out << "free("; |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 726 | CW.writeOperand(Operand, Out); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 727 | Out << ");\n"; |
| 728 | } |
| 729 | |
Chris Lattner | 4440826 | 2002-05-09 03:50:42 +0000 | [diff] [blame^] | 730 | void CInstPrintVisitor::printIndexingExpr(MemAccessInst *MAI) { |
| 731 | CW.writeOperand(MAI->getPointerOperand(), Out); |
| 732 | |
| 733 | for (MemAccessInst::op_iterator I = MAI->idx_begin(), E = MAI->idx_end(); |
| 734 | I != E; ++I) |
| 735 | if ((*I)->getType() == Type::UIntTy) { |
| 736 | Out << "["; |
| 737 | CW.writeOperand(*I, Out); |
| 738 | Out << "]"; |
| 739 | } else { |
| 740 | Out << ".field" << cast<ConstantUInt>(*I)->getValue(); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 741 | } |
Chris Lattner | 4440826 | 2002-05-09 03:50:42 +0000 | [diff] [blame^] | 742 | } |
| 743 | |
| 744 | void CInstPrintVisitor::visitLoadInst(LoadInst *I) { |
| 745 | outputLValue(I); |
| 746 | printIndexingExpr(I); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 747 | Out << ";\n"; |
| 748 | } |
| 749 | |
Chris Lattner | 4440826 | 2002-05-09 03:50:42 +0000 | [diff] [blame^] | 750 | void CInstPrintVisitor::visitStoreInst(StoreInst *I) { |
| 751 | Out << " "; |
| 752 | printIndexingExpr(I); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 753 | Out << " = "; |
Chris Lattner | 4440826 | 2002-05-09 03:50:42 +0000 | [diff] [blame^] | 754 | CW.writeOperand(I->getOperand(0), Out); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 755 | Out << ";\n"; |
| 756 | } |
| 757 | |
| 758 | void CInstPrintVisitor::visitGetElementPtrInst(GetElementPtrInst *I) { |
| 759 | outputLValue(I); |
Chris Lattner | 4440826 | 2002-05-09 03:50:42 +0000 | [diff] [blame^] | 760 | Out << "&"; |
| 761 | printIndexingExpr(I); |
| 762 | Out << ";\n"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Chris Lattner | 2f5f51a | 2002-05-09 03:28:37 +0000 | [diff] [blame] | 765 | void CInstPrintVisitor::visitNot(GenericUnaryInst *I) { |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 766 | outputLValue(I); |
Chris Lattner | 2f5f51a | 2002-05-09 03:28:37 +0000 | [diff] [blame] | 767 | Out << "~"; |
| 768 | CW.writeOperand(I->getOperand(0), Out); |
| 769 | Out << ";\n"; |
| 770 | } |
| 771 | |
| 772 | void CInstPrintVisitor::visitBinaryOperator(Instruction *I) { |
| 773 | // binary instructions, shift instructions, setCond instructions. |
| 774 | outputLValue(I); |
| 775 | if (isa<PointerType>(I->getType())) { |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 776 | Out << "("; |
| 777 | CW.printType(I->getType(), Out); |
| 778 | Out << ")"; |
| 779 | } |
Chris Lattner | 2f5f51a | 2002-05-09 03:28:37 +0000 | [diff] [blame] | 780 | |
| 781 | if (isa<PointerType>(I->getType())) Out << "(long long)"; |
| 782 | CW.writeOperand(I->getOperand(0), Out); |
| 783 | |
| 784 | switch (I->getOpcode()) { |
| 785 | case Instruction::Add: Out << "+"; break; |
| 786 | case Instruction::Sub: Out << "-"; break; |
| 787 | case Instruction::Mul: Out << "*"; break; |
| 788 | case Instruction::Div: Out << "/"; break; |
| 789 | case Instruction::Rem: Out << "%"; break; |
| 790 | case Instruction::And: Out << "&"; break; |
| 791 | case Instruction::Or: Out << "|"; break; |
| 792 | case Instruction::Xor: Out << "^"; break; |
| 793 | case Instruction::SetEQ: Out << "=="; break; |
| 794 | case Instruction::SetNE: Out << "!="; break; |
| 795 | case Instruction::SetLE: Out << "<="; break; |
| 796 | case Instruction::SetGE: Out << ">="; break; |
| 797 | case Instruction::SetLT: Out << "<"; break; |
| 798 | case Instruction::SetGT: Out << ">"; break; |
| 799 | case Instruction::Shl : Out << "<<"; break; |
| 800 | case Instruction::Shr : Out << ">>"; break; |
| 801 | default: cerr << "Invalid operator type!" << I; abort(); |
| 802 | } |
| 803 | |
| 804 | if (isa<PointerType>(I->getType())) Out << "(long long)"; |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 805 | CW.writeOperand(I->getOperand(1), Out); |
Chris Lattner | 2f5f51a | 2002-05-09 03:28:37 +0000 | [diff] [blame] | 806 | Out << ";\n"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | /* END : CInstPrintVisitor implementation */ |
| 810 | |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 811 | string CWriter::getValueName(const Value *V) { |
| 812 | if (V->hasName()) // Print out the label if it exists... |
Chris Lattner | 4440826 | 2002-05-09 03:50:42 +0000 | [diff] [blame^] | 813 | return "l_" + makeNameProper(V->getName()) + "_" + |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 814 | utostr(V->getType()->getUniqueID()); |
| 815 | |
| 816 | int Slot = Table.getValSlot(V); |
| 817 | assert(Slot >= 0 && "Invalid value!"); |
Chris Lattner | 4440826 | 2002-05-09 03:50:42 +0000 | [diff] [blame^] | 818 | return "ltmp_" + itostr(Slot) + "_" + |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 819 | utostr(V->getType()->getUniqueID()); |
| 820 | } |
| 821 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 822 | void CWriter::printModule(const Module *M) { |
| 823 | // printing stdlib inclusion |
| 824 | // Out << "#include <stdlib.h>\n"; |
| 825 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 826 | // get declaration for alloca |
| 827 | Out << "/* Provide Declarations */\n" |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 828 | << "#include <alloca.h>\n\n" |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 829 | |
| 830 | // Provide a definition for null if one does not already exist. |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 831 | << "#ifndef NULL\n#define NULL 0\n#endif\n\n" |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 832 | << "typedef unsigned char bool;\n" |
| 833 | |
| 834 | << "\n\n/* Global Symbols */\n"; |
| 835 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 836 | // Loop over the symbol table, emitting all named constants... |
| 837 | if (M->hasSymbolTable()) |
| 838 | printSymbolTable(*M->getSymbolTable()); |
| 839 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 840 | Out << "\n\n/* Global Data */\n"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 841 | for_each(M->gbegin(), M->gend(), |
| 842 | bind_obj(this, &CWriter::printGlobal)); |
| 843 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 844 | // First output all the declarations of the functions as C requires Functions |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 845 | // be declared before they are used. |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 846 | // |
| 847 | Out << "\n\n/* Function Declarations */\n"; |
| 848 | for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunctionDecl)); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 849 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 850 | // Output all of the functions... |
| 851 | Out << "\n\n/* Function Bodies */\n"; |
| 852 | for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunction)); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | // prints the global constants |
| 856 | void CWriter::printGlobal(const GlobalVariable *GV) { |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 857 | string tempostr = getValueName(GV); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 858 | if (GV->hasInternalLinkage()) Out << "static "; |
| 859 | |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 860 | printTypeVar(GV->getType()->getElementType(), tempostr); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 861 | |
| 862 | if (GV->hasInitializer()) { |
| 863 | Out << " = " ; |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 864 | writeOperand(GV->getInitializer(), Out, false); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | Out << ";\n"; |
| 868 | } |
| 869 | |
| 870 | // printSymbolTable - Run through symbol table looking for named constants |
| 871 | // if a named constant is found, emit it's declaration... |
| 872 | // Assuming that symbol table has only types and constants. |
| 873 | void CWriter::printSymbolTable(const SymbolTable &ST) { |
| 874 | // GraphT G; |
| 875 | for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) { |
| 876 | SymbolTable::type_const_iterator I = ST.type_begin(TI->first); |
| 877 | SymbolTable::type_const_iterator End = ST.type_end(TI->first); |
| 878 | |
| 879 | // TODO |
| 880 | // Need to run through all the used types in the program |
| 881 | // FindUsedTypes &FUT = new FindUsedTypes(); |
| 882 | // const std::set<const Type *> &UsedTypes = FUT.getTypes(); |
| 883 | // Filter out the structures printing forward definitions for each of them |
| 884 | // and creating the dependency graph. |
| 885 | // Print forward definitions to all of them |
| 886 | // print the typedefs topologically sorted |
| 887 | |
| 888 | // But for now we have |
| 889 | for (; I != End; ++I) { |
| 890 | const Value *V = I->second; |
| 891 | if (const Constant *CPV = dyn_cast<const Constant>(V)) { |
| 892 | printConstant(CPV); |
| 893 | } else if (const Type *Ty = dyn_cast<const Type>(V)) { |
| 894 | string tempostr; |
| 895 | string tempstr = ""; |
| 896 | Out << "typedef "; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 897 | tempostr = "llvm__" + I->first; |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 898 | string TypeNameVar = calcTypeNameVar(Ty, TypeNames, |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 899 | tempostr, tempstr); |
| 900 | Out << TypeNameVar << ";\n"; |
| 901 | if (!isa<PointerType>(Ty) || |
| 902 | !cast<PointerType>(Ty)->getElementType()->isPrimitiveType()) |
| 903 | TypeNames.insert(std::make_pair(Ty, "llvm__"+I->first)); |
| 904 | } |
| 905 | } |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | |
| 910 | // printConstant - Print out a constant pool entry... |
| 911 | // |
| 912 | void CWriter::printConstant(const Constant *CPV) { |
| 913 | // TODO |
| 914 | // Dinakar : Don't know what to do with unnamed constants |
| 915 | // should do something about it later. |
| 916 | |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 917 | string tempostr = getValueName(CPV); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 918 | |
| 919 | // Print out the constant type... |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 920 | printTypeVar(CPV->getType(), tempostr); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 921 | |
| 922 | Out << " = "; |
| 923 | // Write the value out now... |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 924 | writeOperand(CPV, Out, false); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 925 | |
| 926 | Out << "\n"; |
| 927 | } |
| 928 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 929 | // printFunctionDecl - Print function declaration |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 930 | // |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 931 | void CWriter::printFunctionDecl(const Function *F) { |
| 932 | printFunctionSignature(F); |
| 933 | Out << ";\n"; |
| 934 | } |
| 935 | |
| 936 | void CWriter::printFunctionSignature(const Function *F) { |
| 937 | if (F->hasInternalLinkage()) Out << "static "; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 938 | |
| 939 | // Loop over the arguments, printing them... |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 940 | const FunctionType *FT = cast<FunctionType>(F->getFunctionType()); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 941 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 942 | // Print out the return type and name... |
| 943 | printType(F->getReturnType(), Out); |
| 944 | Out << " " << makeNameProper(F->getName()) << "("; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 945 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 946 | if (!F->isExternal()) { |
| 947 | for_each(F->getArgumentList().begin(), F->getArgumentList().end(), |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 948 | bind_obj(this, &CWriter::printFunctionArgument)); |
| 949 | } else { |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 950 | // Loop over the arguments, printing them... |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 951 | for (FunctionType::ParamTypes::const_iterator I = |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 952 | FT->getParamTypes().begin(), |
| 953 | E = FT->getParamTypes().end(); I != E; ++I) { |
| 954 | if (I != FT->getParamTypes().begin()) Out << ", "; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 955 | printType(*I, Out); |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | // Finish printing arguments... |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 960 | if (FT->isVarArg()) { |
| 961 | if (FT->getParamTypes().size()) Out << ", "; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 962 | Out << "..."; // Output varargs portion of signature! |
| 963 | } |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 964 | Out << ")"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 967 | |
| 968 | // printFunctionArgument - This member is called for every argument that |
| 969 | // is passed into the method. Simply print it out |
| 970 | // |
| 971 | void CWriter::printFunctionArgument(const Argument *Arg) { |
| 972 | // Insert commas as we go... the first arg doesn't get a comma |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 973 | if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", "; |
| 974 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 975 | // Output type... |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 976 | printTypeVar(Arg->getType(), getValueName(Arg)); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 979 | void CWriter::printFunction(const Function *F) { |
| 980 | if (F->isExternal()) return; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 981 | |
Chris Lattner | f34ee81 | 2002-05-09 03:12:34 +0000 | [diff] [blame] | 982 | Table.incorporateFunction(F); |
| 983 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 984 | // Process each of the basic blocks, gather information and call the |
| 985 | // output methods on the CLocalVars and Function* objects. |
| 986 | |
| 987 | // gather local variable information for each basic block |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 988 | InstLocalVarsVisitor ILV(*this); |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 989 | ILV.visit((Function *)F); |
| 990 | |
| 991 | printFunctionSignature(F); |
| 992 | Out << " {\n"; |
| 993 | |
| 994 | // Loop over the symbol table, emitting all named constants... |
| 995 | if (F->hasSymbolTable()) |
| 996 | printSymbolTable(*F->getSymbolTable()); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 997 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 998 | // print the local variables |
| 999 | // we assume that every local variable is alloca'ed in the C code. |
| 1000 | std::map<const Type*, VarListType> &locals = ILV.CLV.LocalVars; |
| 1001 | |
| 1002 | map<const Type*, VarListType>::iterator iter; |
| 1003 | for (iter = locals.begin(); iter != locals.end(); ++iter) { |
| 1004 | VarListType::iterator listiter; |
| 1005 | for (listiter = iter->second.begin(); listiter != iter->second.end(); |
| 1006 | ++listiter) { |
| 1007 | Out << " "; |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 1008 | printTypeVar(iter->first, *listiter); |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 1009 | Out << ";\n"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1010 | } |
| 1011 | } |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 1012 | |
| 1013 | // print the basic blocks |
| 1014 | for_each(F->begin(), F->end(), bind_obj(this, &CWriter::outputBasicBlock)); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1015 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 1016 | Out << "}\n"; |
Chris Lattner | f34ee81 | 2002-05-09 03:12:34 +0000 | [diff] [blame] | 1017 | Table.purgeFunction(); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | void CWriter::outputBasicBlock(const BasicBlock* BB) { |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 1021 | Out << getValueName(BB) << ":\n"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1022 | |
| 1023 | // Output all of the instructions in the basic block... |
| 1024 | // print the basic blocks |
| 1025 | CInstPrintVisitor CIPV(*this, Table, Out); |
| 1026 | CIPV.visit((BasicBlock *) BB); |
| 1027 | } |
| 1028 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1029 | // printType - Go to extreme measures to attempt to print out a short, symbolic |
| 1030 | // version of a type name. |
| 1031 | ostream& CWriter::printType(const Type *Ty, ostream &Out) { |
| 1032 | return printTypeInt(Out, Ty, TypeNames); |
| 1033 | } |
| 1034 | |
| 1035 | |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 1036 | void CWriter::writeOperand(const Value *Operand, |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 1037 | ostream &Out, bool PrintName = true) { |
Chris Lattner | 4440826 | 2002-05-09 03:50:42 +0000 | [diff] [blame^] | 1038 | if (isa<GlobalValue>(Operand)) |
| 1039 | Out << "(&"; // Global values are references as their addresses by llvm |
| 1040 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 1041 | if (PrintName && Operand->hasName()) { |
| 1042 | // If Operand has a name. |
Chris Lattner | 4440826 | 2002-05-09 03:50:42 +0000 | [diff] [blame^] | 1043 | Out << "l_" << makeNameProper(Operand->getName()) << "_" << |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 1044 | Operand->getType()->getUniqueID(); |
Chris Lattner | 4440826 | 2002-05-09 03:50:42 +0000 | [diff] [blame^] | 1045 | } else if (const Constant *CPV = dyn_cast<const Constant>(Operand)) { |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 1046 | if (isa<ConstantPointerNull>(CPV)) |
| 1047 | Out << "NULL"; |
| 1048 | else |
| 1049 | Out << getConstStrValue(CPV); |
Chris Lattner | 4440826 | 2002-05-09 03:50:42 +0000 | [diff] [blame^] | 1050 | } else { |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 1051 | int Slot = Table.getValSlot(Operand); |
| 1052 | if (Slot >= 0) |
Chris Lattner | 4440826 | 2002-05-09 03:50:42 +0000 | [diff] [blame^] | 1053 | Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID(); |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 1054 | else if (PrintName) |
| 1055 | Out << "<badref>"; |
| 1056 | } |
Chris Lattner | 4440826 | 2002-05-09 03:50:42 +0000 | [diff] [blame^] | 1057 | |
| 1058 | if (isa<GlobalValue>(Operand)) |
| 1059 | Out << ")"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | |
| 1063 | //===----------------------------------------------------------------------===// |
| 1064 | // External Interface declaration |
| 1065 | //===----------------------------------------------------------------------===// |
| 1066 | |
Chris Lattner | f34ee81 | 2002-05-09 03:12:34 +0000 | [diff] [blame] | 1067 | void WriteToC(const Module *M, ostream &Out) { |
| 1068 | assert(M && "You can't write a null module!!"); |
| 1069 | SlotCalculator SlotTable(M, false); |
| 1070 | CWriter W(Out, SlotTable, M); |
| 1071 | W.write(M); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1072 | Out.flush(); |
| 1073 | } |