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