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