Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1 | //===-- Writer.cpp - Library for writing C files -----------------*- C++ -*--=// |
| 2 | // |
| 3 | // This library implements the functionality defined in llvm/Assembly/CWriter.h |
| 4 | // and CLocalVars.h |
| 5 | // |
| 6 | // TODO : Recursive types. |
| 7 | //===-----------------------------------------------------------------------==// |
| 8 | #include "llvm/Assembly/CWriter.h" |
| 9 | #include "CLocalVars.h" |
| 10 | #include "llvm/SlotCalculator.h" |
| 11 | #include "llvm/Module.h" |
| 12 | #include "llvm/Argument.h" |
| 13 | #include "llvm/Function.h" |
| 14 | #include "llvm/DerivedTypes.h" |
| 15 | #include "llvm/Constants.h" |
| 16 | #include "llvm/GlobalVariable.h" |
| 17 | #include "llvm/BasicBlock.h" |
| 18 | #include "llvm/iMemory.h" |
| 19 | #include "llvm/iTerminators.h" |
| 20 | #include "llvm/iPHINode.h" |
| 21 | #include "llvm/iOther.h" |
| 22 | #include "llvm/SymbolTable.h" |
| 23 | #include "llvm/Support/InstVisitor.h" |
| 24 | #include "Support/StringExtras.h" |
| 25 | #include "Support/STLExtras.h" |
| 26 | |
| 27 | #include <algorithm> |
| 28 | #include <strstream> |
| 29 | using std::string; |
| 30 | using std::map; |
| 31 | using std::vector; |
| 32 | using std::ostream; |
| 33 | |
| 34 | /* Implementation of the CLocalVars methods */ |
| 35 | |
| 36 | // Appends a variable to the LocalVars map if it does not already exist |
| 37 | // Also check that the type exists on the map. |
| 38 | void CLocalVars::addLocalVar(const Type *t, const string & var) { |
| 39 | if (!LocalVars.count(t) || |
| 40 | find(LocalVars[t].begin(), LocalVars[t].end(), var) |
| 41 | == LocalVars[t].end()) { |
| 42 | LocalVars[t].push_back(var); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /* Writer.cpp */ |
| 47 | static string calcTypeNameVar(const Type *Ty, vector<const Type *> &TypeStack, |
| 48 | map<const Type *, string> &TypeNames, |
| 49 | string VariableName, string NameSoFar); |
| 50 | |
| 51 | static std::string getConstStrValue(const Constant* CPV); |
| 52 | |
| 53 | |
| 54 | // |
| 55 | //Getting opcodes in terms of the operator |
| 56 | // |
| 57 | static const char *getOpcodeOperName(const Instruction *I) { |
| 58 | switch (I->getOpcode()) { |
| 59 | // Standard binary operators... |
| 60 | case Instruction::Add: return "+"; |
| 61 | case Instruction::Sub: return "-"; |
| 62 | case Instruction::Mul: return "*"; |
| 63 | case Instruction::Div: return "/"; |
| 64 | case Instruction::Rem: return "%"; |
| 65 | |
| 66 | // Logical operators... |
| 67 | case Instruction::And: return "&"; |
| 68 | case Instruction::Or: return "|"; |
| 69 | case Instruction::Xor: return "^"; |
| 70 | |
| 71 | // SetCond operators... |
| 72 | case Instruction::SetEQ: return "=="; |
| 73 | case Instruction::SetNE: return "!="; |
| 74 | case Instruction::SetLE: return "<="; |
| 75 | case Instruction::SetGE: return ">="; |
| 76 | case Instruction::SetLT: return "<"; |
| 77 | case Instruction::SetGT: return ">"; |
| 78 | |
| 79 | //ShiftInstruction... |
| 80 | |
| 81 | case Instruction::Shl : return "<<"; |
| 82 | case Instruction::Shr : return ">>"; |
| 83 | |
| 84 | default: |
| 85 | cerr << "Invalid operator type!" << I->getOpcode() << "\n"; |
| 86 | abort(); |
| 87 | } |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | // We dont want identifier names with ., space, - in them. |
| 93 | // So we replace them with _ |
| 94 | static string makeNameProper(string x) { |
| 95 | string tmp; |
| 96 | for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++) { |
| 97 | if (*sI == '.') |
| 98 | tmp += '_'; |
| 99 | else if (*sI == ' ') |
| 100 | tmp += '_'; |
| 101 | else if (*sI == '-') |
| 102 | tmp += "__"; |
| 103 | else |
| 104 | tmp += *sI; |
| 105 | } |
| 106 | return tmp; |
| 107 | } |
| 108 | |
| 109 | static string getConstantName(const Constant *CPV) { |
| 110 | return CPV->getName(); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | static std::string getConstArrayStrValue(const Constant* CPV) { |
| 115 | std::string Result; |
| 116 | |
| 117 | // As a special case, print the array as a string if it is an array of |
| 118 | // ubytes or an array of sbytes with positive values. |
| 119 | // |
| 120 | const Type *ETy = cast<ArrayType>(CPV->getType())->getElementType(); |
| 121 | bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy); |
| 122 | |
| 123 | if (ETy == Type::SByteTy) { |
| 124 | for (unsigned i = 0; i < CPV->getNumOperands(); ++i) |
| 125 | if (ETy == Type::SByteTy && |
| 126 | cast<ConstantSInt>(CPV->getOperand(i))->getValue() < 0) { |
| 127 | isString = false; |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if (isString) { |
| 133 | Result = "\""; |
| 134 | for (unsigned i = 0; i < CPV->getNumOperands(); ++i) { |
| 135 | unsigned char C = (ETy == Type::SByteTy) ? |
| 136 | (unsigned char)cast<ConstantSInt>(CPV->getOperand(i))->getValue() : |
| 137 | (unsigned char)cast<ConstantUInt>(CPV->getOperand(i))->getValue(); |
| 138 | |
| 139 | if (isprint(C)) { |
| 140 | Result += C; |
| 141 | } else { |
| 142 | Result += '\\'; |
| 143 | Result += 'x'; |
| 144 | Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'); |
| 145 | Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'); |
| 146 | } |
| 147 | } |
| 148 | Result += "\""; |
| 149 | |
| 150 | } else { |
| 151 | Result = "{"; |
| 152 | if (CPV->getNumOperands()) { |
| 153 | Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0))); |
| 154 | for (unsigned i = 1; i < CPV->getNumOperands(); i++) |
| 155 | Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i))); |
| 156 | } |
| 157 | Result += " }"; |
| 158 | } |
| 159 | |
| 160 | return Result; |
| 161 | } |
| 162 | |
| 163 | static std::string getConstStructStrValue(const Constant* CPV) { |
| 164 | std::string Result = "{"; |
| 165 | if (CPV->getNumOperands()) { |
| 166 | Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0))); |
| 167 | for (unsigned i = 1; i < CPV->getNumOperands(); i++) |
| 168 | Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i))); |
| 169 | } |
| 170 | |
| 171 | return Result + " }"; |
| 172 | } |
| 173 | |
| 174 | // our own getStrValue function for constant initializers |
| 175 | static std::string getConstStrValue(const Constant* CPV) { |
| 176 | // Does not handle null pointers, that needs to be checked explicitly |
| 177 | string tempstr; |
| 178 | if (CPV == ConstantBool::False) |
| 179 | return "0"; |
| 180 | else if (CPV == ConstantBool::True) |
| 181 | return "1"; |
| 182 | |
| 183 | else if (isa<ConstantArray>(CPV)) { |
| 184 | tempstr = getConstArrayStrValue(CPV); |
| 185 | } |
| 186 | else if (isa<ConstantStruct>(CPV)) { |
| 187 | tempstr = getConstStructStrValue(CPV); |
| 188 | } |
| 189 | else if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(CPV)) { |
| 190 | tempstr = utostr((long long unsigned int) CUI->getValue()); |
| 191 | } |
| 192 | else if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(CPV)) { |
| 193 | tempstr = itostr(CSI->getValue()); |
| 194 | } |
| 195 | else if (ConstantFP *CFP = dyn_cast<ConstantFP>(CPV)) { |
| 196 | tempstr = ftostr(CFP->getValue()); |
| 197 | } |
| 198 | |
| 199 | if (CPV->getType() == Type::ULongTy) |
| 200 | tempstr += "ull"; |
| 201 | else if (CPV->getType() == Type::LongTy) |
| 202 | tempstr += "ll"; |
| 203 | else if (CPV->getType() == Type::UIntTy || |
| 204 | CPV->getType() == Type::UShortTy) |
| 205 | tempstr += "u"; |
| 206 | |
| 207 | return tempstr; |
| 208 | |
| 209 | } |
| 210 | |
| 211 | // WriteCOperand - Write the name of the specified value out to the specified |
| 212 | // ostream. This can be useful when you just want to print int %0 not the |
| 213 | // whole instruction that generated it. |
| 214 | // |
| 215 | static void WriteCOperandInternal(ostream &Out, const Value *V, |
| 216 | bool PrintName, SlotCalculator *Table, |
| 217 | string &OperandType) { |
| 218 | int Slot; |
| 219 | if (PrintName && V->hasName()) { |
| 220 | // If V has a name. |
| 221 | Out << "llvm__" << makeNameProper(V->getName()) << "_" << |
| 222 | (V->getType())->getUniqueID(); |
| 223 | return; |
| 224 | } |
| 225 | else if (const Constant *CPV = dyn_cast<const Constant>(V)) { |
| 226 | if (isa<ConstantPointerNull>(CPV)) { |
| 227 | Out << "(" << OperandType << ")0"; |
| 228 | } |
| 229 | else |
| 230 | Out << getConstStrValue(CPV); |
| 231 | } |
| 232 | else { |
| 233 | Slot = Table->getValSlot(V); |
| 234 | if (Slot >= 0) |
| 235 | Out << "llvm__tmp_" << Slot << "_" << V->getType()->getUniqueID(); |
| 236 | else if (PrintName) |
| 237 | Out << "<badref>"; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // Internal function |
| 242 | // Essentially pass the Type* variable, an empty typestack and this prints |
| 243 | // out the C type |
| 244 | static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack, |
| 245 | map<const Type *, string> &TypeNames, |
| 246 | string *FunctionInfo) { |
| 247 | |
| 248 | // Takin' care of the fact that boolean would be int in C |
| 249 | // and that ushort would be unsigned short etc. |
| 250 | |
| 251 | // Base Case |
| 252 | if (Ty->isPrimitiveType()) |
| 253 | switch (Ty->getPrimitiveID()) { |
| 254 | case Type::BoolTyID: |
| 255 | return "int"; |
| 256 | break; |
| 257 | case Type::UByteTyID: |
| 258 | return "unsigned char"; |
| 259 | break; |
| 260 | case Type::SByteTyID: |
| 261 | return "signed char"; |
| 262 | break; |
| 263 | case Type::UShortTyID: |
| 264 | return "unsigned long long"; |
| 265 | break; |
| 266 | case Type::ULongTyID: |
| 267 | return "unsigned long long"; |
| 268 | break; |
| 269 | case Type::LongTyID: |
| 270 | return "signed long long"; |
| 271 | break; |
| 272 | case Type::UIntTyID: |
| 273 | return "unsigned int"; |
| 274 | break; |
| 275 | default : |
| 276 | return Ty->getDescription(); |
| 277 | } |
| 278 | |
| 279 | // Check to see if the type is named. |
| 280 | map<const Type *, string>::iterator I = TypeNames.find(Ty); |
| 281 | if (I != TypeNames.end()) |
| 282 | return I->second; |
| 283 | |
| 284 | // Check to see if the Type is already on the stack... |
| 285 | unsigned Slot = 0, CurSize = TypeStack.size(); |
| 286 | while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type |
| 287 | |
| 288 | // This is another base case for the recursion. In this case, we know |
| 289 | // that we have looped back to a type that we have previously visited. |
| 290 | // Generate the appropriate upreference to handle this. |
| 291 | // |
| 292 | if (Slot < CurSize) |
| 293 | return "\\" + utostr(CurSize-Slot); // Here's the upreference |
| 294 | |
| 295 | TypeStack.push_back(Ty); // Recursive case: Add us to the stack.. |
| 296 | |
| 297 | string Result; |
| 298 | string MInfo = ""; |
| 299 | switch (Ty->getPrimitiveID()) { |
| 300 | case Type::FunctionTyID: { |
| 301 | const FunctionType *MTy = cast<const FunctionType>(Ty); |
| 302 | Result = calcTypeName(MTy->getReturnType(), TypeStack, TypeNames, &MInfo); |
| 303 | if (MInfo != "") |
| 304 | Result += ") " + MInfo; |
| 305 | Result += "("; |
| 306 | *FunctionInfo += " ("; |
| 307 | for (FunctionType::ParamTypes::const_iterator |
| 308 | I = MTy->getParamTypes().begin(), |
| 309 | E = MTy->getParamTypes().end(); I != E; ++I) { |
| 310 | if (I != MTy->getParamTypes().begin()) |
| 311 | *FunctionInfo += ", "; |
| 312 | MInfo = ""; |
| 313 | *FunctionInfo += calcTypeName(*I, TypeStack, TypeNames, &MInfo); |
| 314 | if (MInfo != "") |
| 315 | Result += ") " + MInfo; |
| 316 | } |
| 317 | if (MTy->isVarArg()) { |
| 318 | if (!MTy->getParamTypes().empty()) |
| 319 | *FunctionInfo += ", "; |
| 320 | *FunctionInfo += "..."; |
| 321 | } |
| 322 | *FunctionInfo += ")"; |
| 323 | break; |
| 324 | } |
| 325 | case Type::StructTyID: { |
| 326 | string tempstr = ""; |
| 327 | const StructType *STy = cast<const StructType>(Ty); |
| 328 | Result = " struct {\n "; |
| 329 | int indx = 0; |
| 330 | for (StructType::ElementTypes::const_iterator |
| 331 | I = STy->getElementTypes().begin(), |
| 332 | E = STy->getElementTypes().end(); I != E; ++I) { |
| 333 | Result += calcTypeNameVar(*I, TypeStack, TypeNames, |
| 334 | "field" + itostr(indx++), tempstr); |
| 335 | Result += ";\n "; |
| 336 | } |
| 337 | Result += " } "; |
| 338 | break; |
| 339 | } |
| 340 | case Type::PointerTyID: |
| 341 | Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(), |
| 342 | TypeStack, TypeNames, &MInfo); |
| 343 | Result += "*"; |
| 344 | break; |
| 345 | case Type::ArrayTyID: { |
| 346 | const ArrayType *ATy = cast<const ArrayType>(Ty); |
| 347 | int NumElements = ATy->getNumElements(); |
| 348 | Result = calcTypeName(ATy->getElementType(), TypeStack, TypeNames, &MInfo); |
| 349 | Result += "*"; |
| 350 | break; |
| 351 | } |
| 352 | default: |
| 353 | assert(0 && "Unhandled case in getTypeProps!"); |
| 354 | Result = "<error>"; |
| 355 | } |
| 356 | |
| 357 | TypeStack.pop_back(); // Remove self from stack... |
| 358 | return Result; |
| 359 | } |
| 360 | |
| 361 | // Internal function |
| 362 | // Pass the Type* variable and and the variable name and this prints out the |
| 363 | // variable declaration. |
| 364 | // This is different from calcTypeName because if you need to declare an array |
| 365 | // the size of the array would appear after the variable name itself |
| 366 | // For eg. int a[10]; |
| 367 | static string calcTypeNameVar(const Type *Ty, vector<const Type *> &TypeStack, |
| 368 | map<const Type *, string> &TypeNames, |
| 369 | string VariableName, string NameSoFar) { |
| 370 | if (Ty->isPrimitiveType()) |
| 371 | switch (Ty->getPrimitiveID()) { |
| 372 | case Type::BoolTyID: |
| 373 | return "int " + NameSoFar + VariableName; |
| 374 | break; |
| 375 | case Type::UByteTyID: |
| 376 | return "unsigned char " + NameSoFar + VariableName; |
| 377 | break; |
| 378 | case Type::SByteTyID: |
| 379 | return "signed char " + NameSoFar + VariableName; |
| 380 | break; |
| 381 | case Type::UShortTyID: |
| 382 | return "unsigned long long " + NameSoFar + VariableName; |
| 383 | break; |
| 384 | case Type::ULongTyID: |
| 385 | return "unsigned long long " + NameSoFar + VariableName; |
| 386 | break; |
| 387 | case Type::LongTyID: |
| 388 | return "signed long long " + NameSoFar + VariableName; |
| 389 | break; |
| 390 | case Type::UIntTyID: |
| 391 | return "unsigned int " + NameSoFar + VariableName; |
| 392 | break; |
| 393 | default : |
| 394 | return Ty->getDescription() + " " + NameSoFar + VariableName; |
| 395 | } |
| 396 | |
| 397 | // Check to see if the type is named. |
| 398 | map<const Type *, string>::iterator I = TypeNames.find(Ty); |
| 399 | if (I != TypeNames.end()) |
| 400 | return I->second + " " + NameSoFar + VariableName; |
| 401 | |
| 402 | // Check to see if the Type is already on the stack... |
| 403 | unsigned Slot = 0, CurSize = TypeStack.size(); |
| 404 | while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type |
| 405 | |
| 406 | if (Slot < CurSize) |
| 407 | return "\\" + utostr(CurSize-Slot) + " " + NameSoFar + VariableName; |
| 408 | // Here's the upreference |
| 409 | |
| 410 | TypeStack.push_back(Ty); // Recursive case: Add us to the stack.. |
| 411 | |
| 412 | string Result; |
| 413 | string tempstr = ""; |
| 414 | |
| 415 | switch (Ty->getPrimitiveID()) { |
| 416 | case Type::FunctionTyID: { |
| 417 | string MInfo = ""; |
| 418 | const FunctionType *MTy = cast<const FunctionType>(Ty); |
| 419 | Result += calcTypeName(MTy->getReturnType(), TypeStack, TypeNames, &MInfo); |
| 420 | if (MInfo != "") |
| 421 | Result += ") " + MInfo; |
| 422 | Result += " " + NameSoFar + VariableName; |
| 423 | Result += " ("; |
| 424 | for (FunctionType::ParamTypes::const_iterator |
| 425 | I = MTy->getParamTypes().begin(), |
| 426 | E = MTy->getParamTypes().end(); I != E; ++I) { |
| 427 | if (I != MTy->getParamTypes().begin()) |
| 428 | Result += ", "; |
| 429 | MInfo = ""; |
| 430 | Result += calcTypeName(*I, TypeStack, TypeNames, &MInfo); |
| 431 | if (MInfo != "") |
| 432 | Result += ") " + MInfo; |
| 433 | } |
| 434 | if (MTy->isVarArg()) { |
| 435 | if (!MTy->getParamTypes().empty()) |
| 436 | Result += ", "; |
| 437 | Result += "..."; |
| 438 | } |
| 439 | Result += ")"; |
| 440 | break; |
| 441 | } |
| 442 | case Type::StructTyID: { |
| 443 | const StructType *STy = cast<const StructType>(Ty); |
| 444 | Result = " struct {\n "; |
| 445 | int indx = 0; |
| 446 | for (StructType::ElementTypes::const_iterator |
| 447 | I = STy->getElementTypes().begin(), |
| 448 | E = STy->getElementTypes().end(); I != E; ++I) { |
| 449 | Result += calcTypeNameVar(*I, TypeStack, TypeNames, |
| 450 | "field" + itostr(indx++), ""); |
| 451 | Result += ";\n "; |
| 452 | } |
| 453 | Result += " }"; |
| 454 | Result += " " + NameSoFar + VariableName; |
| 455 | break; |
| 456 | } |
| 457 | |
| 458 | case Type::PointerTyID: { |
| 459 | Result = calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(), |
| 460 | TypeStack, TypeNames, tempstr, |
| 461 | "(*" + NameSoFar + VariableName + ")"); |
| 462 | break; |
| 463 | } |
| 464 | |
| 465 | case Type::ArrayTyID: { |
| 466 | const ArrayType *ATy = cast<const ArrayType>(Ty); |
| 467 | int NumElements = ATy->getNumElements(); |
| 468 | Result = calcTypeNameVar(ATy->getElementType(), TypeStack, TypeNames, |
| 469 | tempstr, NameSoFar + VariableName + "[" + |
| 470 | itostr(NumElements) + "]"); |
| 471 | break; |
| 472 | } |
| 473 | default: |
| 474 | assert(0 && "Unhandled case in getTypeProps!"); |
| 475 | Result = "<error>"; |
| 476 | } |
| 477 | |
| 478 | TypeStack.pop_back(); // Remove self from stack... |
| 479 | return Result; |
| 480 | } |
| 481 | |
| 482 | // printTypeVarInt - The internal guts of printing out a type that has a |
| 483 | // potentially named portion and the variable associated with the type. |
| 484 | static ostream &printTypeVarInt(ostream &Out, const Type *Ty, |
| 485 | map<const Type *, string> &TypeNames, |
| 486 | string VariableName) { |
| 487 | // Primitive types always print out their description, regardless of whether |
| 488 | // they have been named or not. |
| 489 | |
| 490 | // Booleans have to be specially handled to be printed as ints with values |
| 491 | // 0 or 1; |
| 492 | if (Ty->isPrimitiveType()) |
| 493 | switch (Ty->getPrimitiveID()) { |
| 494 | case Type::BoolTyID: |
| 495 | return Out << "int " << VariableName; |
| 496 | break; |
| 497 | case Type::UByteTyID: |
| 498 | return Out << "unsigned char " << VariableName; |
| 499 | break; |
| 500 | case Type::SByteTyID: |
| 501 | return Out << "signed char " << VariableName; |
| 502 | break; |
| 503 | case Type::UShortTyID: |
| 504 | return Out << "unsigned long long " << VariableName; |
| 505 | break; |
| 506 | case Type::ULongTyID: |
| 507 | return Out << "unsigned long long " << VariableName; |
| 508 | break; |
| 509 | case Type::LongTyID: |
| 510 | return Out << "signed long long " << VariableName; |
| 511 | break; |
| 512 | case Type::UIntTyID: |
| 513 | return Out << "unsigned int " << VariableName; |
| 514 | break; |
| 515 | default : |
| 516 | return Out << Ty->getDescription() << " " << VariableName; |
| 517 | } |
| 518 | |
| 519 | // Check to see if the type is named. |
| 520 | map<const Type *, string>::iterator I = TypeNames.find(Ty); |
| 521 | if (I != TypeNames.end()) return Out << I->second << " " << VariableName; |
| 522 | |
| 523 | // Otherwise we have a type that has not been named but is a derived type. |
| 524 | // Carefully recurse the type hierarchy to print out any contained symbolic |
| 525 | // names. |
| 526 | // |
| 527 | vector<const Type *> TypeStack; |
| 528 | string TypeNameVar, tempstr = ""; |
| 529 | TypeNameVar = calcTypeNameVar(Ty, TypeStack, TypeNames, VariableName, |
| 530 | tempstr); |
| 531 | return Out << TypeNameVar; |
| 532 | // TODO: Check what happens to caching |
| 533 | // TypeNames.insert(std::make_pair(Ty, TypeName)); |
| 534 | //Cache type name for later use |
| 535 | } |
| 536 | |
| 537 | // Internal guts of printing a type name |
| 538 | static ostream &printTypeInt(ostream &Out, const Type *Ty, |
| 539 | map<const Type *, string> &TypeNames) { |
| 540 | // Primitive types always print out their description, regardless of whether |
| 541 | // they have been named or not. |
| 542 | |
| 543 | // Booleans have to be specially handled to be printed as ints with values |
| 544 | // 0 or 1; |
| 545 | |
| 546 | if (Ty->isPrimitiveType()) |
| 547 | switch (Ty->getPrimitiveID()) { |
| 548 | case Type::BoolTyID: |
| 549 | return Out << "int"; |
| 550 | break; |
| 551 | case Type::UByteTyID: |
| 552 | return Out << "unsigned char"; |
| 553 | break; |
| 554 | case Type::SByteTyID: |
| 555 | return Out << "signed char"; |
| 556 | break; |
| 557 | case Type::UShortTyID: |
| 558 | return Out << "unsigned long long"; |
| 559 | break; |
| 560 | case Type::ULongTyID: |
| 561 | return Out << "unsigned long long"; |
| 562 | break; |
| 563 | case Type::LongTyID: |
| 564 | return Out << "signed long long"; |
| 565 | break; |
| 566 | case Type::UIntTyID: |
| 567 | return Out << "unsigned int"; |
| 568 | break; |
| 569 | default : |
| 570 | return Out << Ty->getDescription(); |
| 571 | } |
| 572 | |
| 573 | // Check to see if the type is named. |
| 574 | map<const Type *, string>::iterator I = TypeNames.find(Ty); |
| 575 | if (I != TypeNames.end()) return Out << I->second; |
| 576 | |
| 577 | // Otherwise we have a type that has not been named but is a derived type. |
| 578 | // Carefully recurse the type hierarchy to print out any contained symbolic |
| 579 | // names. |
| 580 | // |
| 581 | vector<const Type *> TypeStack; |
| 582 | string MInfo = ""; |
| 583 | string TypeName = calcTypeName(Ty, TypeStack, TypeNames, &MInfo); |
| 584 | // TypeNames.insert(std::make_pair(Ty, TypeName)); |
| 585 | //Cache type name for later use |
| 586 | if (MInfo != "") |
| 587 | return Out << TypeName << ")" << MInfo; |
| 588 | else |
| 589 | return Out << TypeName; |
| 590 | } |
| 591 | |
| 592 | namespace { |
| 593 | |
| 594 | //Internal CWriter class mimics AssemblyWriter. |
| 595 | class CWriter { |
| 596 | ostream& Out; |
| 597 | SlotCalculator &Table; |
| 598 | const Module *TheModule; |
| 599 | map<const Type *, string> TypeNames; |
| 600 | public: |
| 601 | inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M) |
| 602 | : Out(o), Table(Tab), TheModule(M) { |
| 603 | |
| 604 | } |
| 605 | |
| 606 | inline void write(const Module *M) { printModule(M); } |
| 607 | |
| 608 | ostream& printTypeVar(const Type *Ty, string VariableName, ostream &Out); |
| 609 | ostream& printType(const Type *Ty, ostream &Out); |
| 610 | void writeOperand(const Value *Operand, bool PrintType,ostream &Out, |
| 611 | bool PrintName = true); |
| 612 | |
| 613 | private : |
| 614 | void printModule(const Module *M); |
| 615 | void printSymbolTable(const SymbolTable &ST); |
| 616 | void printConstant(const Constant *CPV); |
| 617 | void printGlobal(const GlobalVariable *GV); |
| 618 | void printFunctionDecl(const Function *M); //for printing just the method |
| 619 | // declaration |
| 620 | void printFunctionArgument(const Argument *MA); |
| 621 | |
| 622 | void printFunction(const Function *); |
| 623 | |
| 624 | void outputFunction(const Function *, CLocalVars &); |
| 625 | void outputBasicBlock(const BasicBlock *); |
| 626 | }; |
| 627 | /* END class CWriter */ |
| 628 | |
| 629 | |
| 630 | /* CLASS InstLocalVarsVisitor */ |
| 631 | class InstLocalVarsVisitor : public InstVisitor<InstLocalVarsVisitor> { |
| 632 | SlotCalculator& Table; |
| 633 | |
| 634 | void handleTerminator(TerminatorInst *tI,int indx); |
| 635 | |
| 636 | public: |
| 637 | CLocalVars CLV; |
| 638 | |
| 639 | InstLocalVarsVisitor(SlotCalculator& table) : Table(table) { |
| 640 | |
| 641 | } |
| 642 | |
| 643 | void visitInstruction(Instruction *I) { |
| 644 | string tempostr; |
| 645 | if (I && I->hasName() && !isa<PHINode>(I)) { |
| 646 | tempostr = "llvm__" + makeNameProper(I->getName()) + "_" + |
| 647 | itostr((int)I->getType()->getUniqueID()); |
| 648 | CLV.addLocalVar(I->getType(), tempostr); |
| 649 | } else if (I) { |
| 650 | int Slot = Table.getValSlot(I); |
| 651 | //if (Slot < 0) then it is a instruction with no |
| 652 | // value (like return void ) |
| 653 | if ((Slot >= 0) && !isa<PHINode>(I)) { |
| 654 | tempostr = "llvm__tmp_"; |
| 655 | tempostr += itostr(Slot) + "_" + |
| 656 | itostr((int)I->getType()->getUniqueID()); |
| 657 | CLV.addLocalVar(I->getType(), tempostr); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | } |
| 662 | |
| 663 | void visitBranchInst(BranchInst *I) { |
| 664 | TerminatorInst *tI = cast<TerminatorInst>(I); |
| 665 | if (I->getNumOperands() > 1) { |
| 666 | handleTerminator(tI, 0); |
| 667 | handleTerminator(tI, 1); |
| 668 | } |
| 669 | else { |
| 670 | handleTerminator(tI, 0); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | }; |
| 675 | |
| 676 | |
| 677 | /* CLASS CInstPrintVisitor */ |
| 678 | |
| 679 | class CInstPrintVisitor: public InstVisitor<CInstPrintVisitor> { |
| 680 | CWriter& CW; |
| 681 | SlotCalculator& Table; |
| 682 | ostream &Out; |
| 683 | const Value *Operand; |
| 684 | |
| 685 | void outputLValue(Instruction *); |
| 686 | void printPhiFromNextBlock(TerminatorInst *tI, int indx); |
| 687 | |
| 688 | public: |
| 689 | CInstPrintVisitor (CWriter &cw, SlotCalculator& table, ostream& o) |
| 690 | : CW(cw), Table(table), Out(o) { |
| 691 | |
| 692 | } |
| 693 | |
| 694 | void visitCastInst(CastInst *I); |
| 695 | void visitCallInst(CallInst *I); |
| 696 | void visitShr(ShiftInst *I); |
| 697 | void visitShl(ShiftInst *I); |
| 698 | void visitReturnInst(ReturnInst *I); |
| 699 | void visitBranchInst(BranchInst *I); |
| 700 | void visitSwitchInst(SwitchInst *I); |
| 701 | void visitInvokeInst(InvokeInst *I) ; |
| 702 | void visitMallocInst(MallocInst *I); |
| 703 | void visitAllocaInst(AllocaInst *I); |
| 704 | void visitFreeInst(FreeInst *I); |
| 705 | void visitLoadInst(LoadInst *I); |
| 706 | void visitStoreInst(StoreInst *I); |
| 707 | void visitGetElementPtrInst(GetElementPtrInst *I); |
| 708 | void visitPHINode(PHINode *I); |
| 709 | void visitUnaryOperator (UnaryOperator *I); |
| 710 | void visitBinaryOperator(BinaryOperator *I); |
| 711 | |
| 712 | }; |
| 713 | |
| 714 | } |
| 715 | |
| 716 | void InstLocalVarsVisitor::handleTerminator(TerminatorInst *tI,int indx) { |
| 717 | BasicBlock *bb = tI->getSuccessor(indx); |
| 718 | BasicBlock::const_iterator insIt = bb->begin(); |
| 719 | while (insIt != bb->end()) { |
| 720 | if (const PHINode *pI = dyn_cast<const PHINode>(*insIt)) { |
| 721 | //Its a phinode! |
| 722 | //Calculate the incoming index for this |
| 723 | int incindex = pI->getBasicBlockIndex(tI->getParent()); |
| 724 | if (incindex != -1) |
| 725 | if (pI && pI->hasName()) { |
| 726 | string tempostr; |
| 727 | tempostr = "llvm__" + makeNameProper(pI->getName()) + "_" + |
| 728 | itostr((int)pI->getType()->getUniqueID()); |
| 729 | CLV.addLocalVar(pI->getType(), tempostr) ; |
| 730 | } else { |
| 731 | string tempostr; |
| 732 | int Slot = Table.getValSlot(pI); |
| 733 | if (Slot >= 0) { |
| 734 | tempostr = "llvm__tmp_" + itostr(Slot) + "_" |
| 735 | + itostr((int)pI->getType()->getUniqueID()); |
| 736 | CLV.addLocalVar(pI->getType(), tempostr); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | } |
| 741 | else break; |
| 742 | insIt++; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | /* Implementation of CInstPrintVisitor */ |
| 747 | |
| 748 | void CInstPrintVisitor::outputLValue(Instruction *I) { |
| 749 | if (I && I->hasName() && !isa<PHINode>(I)) { |
| 750 | Out << "llvm__" << makeNameProper(I->getName()) << "_" |
| 751 | << I->getType()->getUniqueID() << " = "; |
| 752 | } else { |
| 753 | int Slot = Table.getValSlot(I); |
| 754 | //if (Slot < 0) then it is a instruction with no value (like return void ) |
| 755 | if ((Slot >= 0) && !isa<PHINode>(I)) |
| 756 | Out << "llvm__tmp_" << Slot << "_" << I->getType()->getUniqueID() |
| 757 | << " = "; |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | void CInstPrintVisitor::printPhiFromNextBlock(TerminatorInst *tI, int indx) { |
| 762 | BasicBlock *bb = tI->getSuccessor(indx); |
| 763 | BasicBlock::const_iterator insIt = bb->begin(); |
| 764 | while (insIt != bb->end()) { |
| 765 | if (const PHINode *pI = dyn_cast<const PHINode>(*insIt)) { |
| 766 | //Its a phinode! |
| 767 | //Calculate the incoming index for this |
| 768 | int incindex = pI->getBasicBlockIndex(tI->getParent()); |
| 769 | if (incindex != -1) |
| 770 | { |
| 771 | //now we have to do the printing |
| 772 | if (pI && pI->hasName()) { |
| 773 | Out << "llvm__" << makeNameProper(pI->getName()) << "_" |
| 774 | << pI->getType()->getUniqueID() << " = "; |
| 775 | } else { |
| 776 | int Slot = Table.getValSlot(pI); |
| 777 | if (Slot >= 0) |
| 778 | Out << "llvm__tmp_" << Slot << "_" |
| 779 | << pI->getType()->getUniqueID() << " = "; |
| 780 | } |
| 781 | CW.writeOperand(pI->getIncomingValue(incindex),false, Out); |
| 782 | Out << ";\n"; |
| 783 | } |
| 784 | } |
| 785 | else break; |
| 786 | insIt++; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | // Implement all "other" instructions, except for PHINode |
| 791 | void CInstPrintVisitor::visitCastInst(CastInst *I) { |
| 792 | outputLValue(I); |
| 793 | Operand = I->getNumOperands() ? I->getOperand(0) : 0; |
| 794 | Out << "("; |
| 795 | CW.printType(I->getType(), Out); |
| 796 | Out << ")"; |
| 797 | CW.writeOperand(Operand, false, Out); |
| 798 | Out << ";\n"; |
| 799 | } |
| 800 | |
| 801 | void CInstPrintVisitor::visitCallInst(CallInst *I) { |
| 802 | outputLValue(I); |
| 803 | Operand = I->getNumOperands() ? I->getOperand(0) : 0; |
| 804 | const PointerType *PTy = dyn_cast<PointerType>(Operand->getType()); |
| 805 | const FunctionType *MTy = PTy |
| 806 | ? dyn_cast<FunctionType>(PTy->getElementType()):0; |
| 807 | const Type *RetTy = MTy ? MTy->getReturnType() : 0; |
| 808 | |
| 809 | // If possible, print out the short form of the call instruction, but we can |
| 810 | // only do this if the first argument is a pointer to a nonvararg method, |
| 811 | // and if the value returned is not a pointer to a method. |
| 812 | // |
| 813 | if (RetTy && !MTy->isVarArg() && |
| 814 | (!isa<PointerType>(RetTy)|| |
| 815 | !isa<FunctionType>(cast<PointerType>(RetTy)))){ |
| 816 | Out << " "; |
| 817 | Out << makeNameProper(Operand->getName()); |
| 818 | } else { |
| 819 | Out << makeNameProper(Operand->getName()); |
| 820 | } |
| 821 | Out << "("; |
| 822 | if (I->getNumOperands() > 1) |
| 823 | CW.writeOperand(I->getOperand(1), false, Out); |
| 824 | for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) { |
| 825 | Out << ","; |
| 826 | CW.writeOperand(I->getOperand(op), false, Out); |
| 827 | } |
| 828 | |
| 829 | Out << " );\n"; |
| 830 | } |
| 831 | |
| 832 | void CInstPrintVisitor::visitShr(ShiftInst *I) { |
| 833 | outputLValue(I); |
| 834 | Operand = I->getNumOperands() ? I->getOperand(0) : 0; |
| 835 | Out << "("; |
| 836 | CW.writeOperand(Operand, false, Out); |
| 837 | Out << " >> "; |
| 838 | Out << "("; |
| 839 | CW.writeOperand(I->getOperand(1), false, Out); |
| 840 | Out << "));\n"; |
| 841 | } |
| 842 | |
| 843 | void CInstPrintVisitor::visitShl(ShiftInst *I) { |
| 844 | outputLValue(I); |
| 845 | Operand = I->getNumOperands() ? I->getOperand(0) : 0; |
| 846 | Out << "("; |
| 847 | CW.writeOperand(Operand, false, Out); |
| 848 | Out << " << "; |
| 849 | Out << "("; |
| 850 | CW.writeOperand(I->getOperand(1), false, Out); |
| 851 | Out << "));\n"; |
| 852 | } |
| 853 | |
| 854 | // Specific Instruction type classes... note that all of the casts are |
| 855 | // neccesary because we use the instruction classes as opaque types... |
| 856 | // |
| 857 | void CInstPrintVisitor::visitReturnInst(ReturnInst *I) { |
| 858 | Operand = I->getNumOperands() ? I->getOperand(0) : 0; |
| 859 | Out << "return "; |
| 860 | if (Operand) |
| 861 | CW.writeOperand(Operand,false, Out); |
| 862 | Out << ";\n"; |
| 863 | } |
| 864 | |
| 865 | void CInstPrintVisitor::visitBranchInst(BranchInst *I) { |
| 866 | Operand = I->getNumOperands() ? I->getOperand(0) : 0; |
| 867 | TerminatorInst *tI = cast<TerminatorInst>(I); |
| 868 | if (I->getNumOperands() > 1) { |
| 869 | Out << "if ("; |
| 870 | CW.writeOperand(I->getOperand(2),false, Out); |
| 871 | Out << ") {\n"; |
| 872 | printPhiFromNextBlock(tI,0); |
| 873 | Out << " goto "; |
| 874 | CW.writeOperand(Operand,false, Out); |
| 875 | Out << ";\n"; |
| 876 | Out << "}" << "else {\n"; |
| 877 | printPhiFromNextBlock(tI,1); |
| 878 | Out << " goto "; |
| 879 | CW.writeOperand(I->getOperand(1),false, Out); |
| 880 | Out << ";\n"; |
| 881 | Out << "}\n"; |
| 882 | } else { |
| 883 | printPhiFromNextBlock(tI,0); |
| 884 | Out << " goto "; |
| 885 | CW.writeOperand(Operand, false, Out); |
| 886 | Out << ";\n"; |
| 887 | } |
| 888 | Out << "\n"; |
| 889 | } |
| 890 | |
| 891 | void CInstPrintVisitor::visitSwitchInst(SwitchInst *I) { |
| 892 | Out << "\n"; |
| 893 | } |
| 894 | |
| 895 | void CInstPrintVisitor::visitInvokeInst(InvokeInst *I) { |
| 896 | Out << "\n"; |
| 897 | } |
| 898 | |
| 899 | void CInstPrintVisitor::visitMallocInst(MallocInst *I) { |
| 900 | outputLValue(I); |
| 901 | Operand = I->getNumOperands() ? I->getOperand(0) : 0; |
| 902 | string tempstr = ""; |
| 903 | Out << "("; |
| 904 | CW.printType(cast<const PointerType>(I->getType())->getElementType(), Out); |
| 905 | Out << "*) malloc(sizeof("; |
| 906 | CW.printTypeVar(cast<const PointerType>(I->getType())->getElementType(), |
| 907 | tempstr, Out); |
| 908 | Out << ")"; |
| 909 | if (I->getNumOperands()) { |
| 910 | Out << " * " ; |
| 911 | CW.writeOperand(Operand, false, Out); |
| 912 | } |
| 913 | Out << ");"; |
| 914 | } |
| 915 | |
| 916 | void CInstPrintVisitor::visitAllocaInst(AllocaInst *I) { |
| 917 | outputLValue(I); |
| 918 | Operand = I->getNumOperands() ? I->getOperand(0) : 0; |
| 919 | string tempstr = ""; |
| 920 | Out << "("; |
| 921 | CW.printTypeVar(I->getType(), tempstr, Out); |
| 922 | Out << ") alloca(sizeof("; |
| 923 | CW.printTypeVar(cast<const PointerType>(I->getType())->getElementType(), |
| 924 | tempstr, Out); |
| 925 | Out << ")"; |
| 926 | if (I->getNumOperands()) { |
| 927 | Out << " * " ; |
| 928 | CW.writeOperand(Operand, false, Out); |
| 929 | } |
| 930 | Out << ");\n"; |
| 931 | } |
| 932 | |
| 933 | void CInstPrintVisitor::visitFreeInst(FreeInst *I) { |
| 934 | Operand = I->getNumOperands() ? I->getOperand(0) : 0; |
| 935 | Out << "free("; |
| 936 | CW.writeOperand(Operand, false, Out); |
| 937 | Out << ");\n"; |
| 938 | } |
| 939 | |
| 940 | void CInstPrintVisitor::visitLoadInst(LoadInst *I) { |
| 941 | outputLValue(I); |
| 942 | Operand = I->getNumOperands() ? I->getOperand(0) : 0; |
| 943 | if (I->getNumOperands() <= 1) { |
| 944 | Out << "*"; |
| 945 | CW.writeOperand(Operand,false, Out); |
| 946 | } |
| 947 | else { |
| 948 | //Check if it is an array type or struct type ptr! |
| 949 | int arrtype = 1; |
| 950 | const PointerType *PTy = dyn_cast<PointerType>(I->getType()); |
| 951 | if (cast<const PointerType>(Operand->getType())->getElementType()->getPrimitiveID() == Type::StructTyID) |
| 952 | arrtype = 0; |
| 953 | if (arrtype && isa<GlobalValue>(Operand)) |
| 954 | Out << "(&"; |
| 955 | CW.writeOperand(Operand,false, Out); |
| 956 | for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) { |
| 957 | if (i == 1) { |
| 958 | if (arrtype || !isa<GlobalValue>(Operand)) { |
| 959 | Out << "["; |
| 960 | CW.writeOperand(I->getOperand(i), false, Out); |
| 961 | Out << "]"; |
| 962 | } |
| 963 | if (isa<GlobalValue>(Operand) && arrtype) |
| 964 | Out << ")"; |
| 965 | } |
| 966 | else { |
| 967 | if (arrtype == 1) Out << "["; |
| 968 | else |
| 969 | Out << ".field"; |
| 970 | CW.writeOperand(I->getOperand(i), false, Out); |
| 971 | if (arrtype == 1) Out << "]"; |
| 972 | } |
| 973 | } |
| 974 | } |
| 975 | Out << ";\n"; |
| 976 | } |
| 977 | |
| 978 | void CInstPrintVisitor::visitStoreInst(StoreInst *I) { |
| 979 | Operand = I->getNumOperands() ? I->getOperand(0) : 0; |
| 980 | if (I->getNumOperands() <= 2) { |
| 981 | Out << "*"; |
| 982 | CW.writeOperand(I->getOperand(1), false, Out); |
| 983 | } |
| 984 | else { |
| 985 | //Check if it is an array type or struct type ptr! |
| 986 | int arrtype = 1; |
| 987 | if (cast<const PointerType>(I->getOperand(1)->getType())->getElementType()->getPrimitiveID() == Type::StructTyID) |
| 988 | arrtype = 0; |
| 989 | if (isa<GlobalValue>(I->getOperand(1)) && arrtype) |
| 990 | Out << "(&"; |
| 991 | CW.writeOperand(I->getOperand(1), false, Out); |
| 992 | for (unsigned i = 2, E = I->getNumOperands(); i != E; ++i) { |
| 993 | if (i == 2) { |
| 994 | if (arrtype || !isa<GlobalValue>(I->getOperand(1))) { |
| 995 | Out << "["; |
| 996 | CW.writeOperand(I->getOperand(i), false, Out); |
| 997 | Out << "]"; |
| 998 | } |
| 999 | if (isa<GlobalValue>(I->getOperand(1)) && arrtype) |
| 1000 | Out << ")"; |
| 1001 | } |
| 1002 | else { |
| 1003 | if (arrtype == 1) Out << "["; |
| 1004 | else |
| 1005 | Out << ".field"; |
| 1006 | CW.writeOperand(I->getOperand(i), false, Out); |
| 1007 | if (arrtype == 1) Out << "]"; |
| 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | Out << " = "; |
| 1012 | CW.writeOperand(Operand,false, Out); |
| 1013 | Out << ";\n"; |
| 1014 | } |
| 1015 | |
| 1016 | void CInstPrintVisitor::visitGetElementPtrInst(GetElementPtrInst *I) { |
| 1017 | outputLValue(I); |
| 1018 | Operand = I->getNumOperands() ? I->getOperand(0) : 0; |
| 1019 | Out << " &("; |
| 1020 | if (I->getNumOperands() <= 1) |
| 1021 | CW.writeOperand(Operand,false, Out); |
| 1022 | else { |
| 1023 | //Check if it is an array type or struct type ptr! |
| 1024 | int arrtype = 1; |
| 1025 | if ((cast<const PointerType>(Operand->getType()))->getElementType()->getPrimitiveID() == Type::StructTyID) |
| 1026 | arrtype = 0; |
| 1027 | if ((isa<GlobalValue>(Operand)) && arrtype) |
| 1028 | Out << "(&"; |
| 1029 | CW.writeOperand(Operand,false, Out); |
| 1030 | for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) { |
| 1031 | if (i == 1) { |
| 1032 | if (arrtype || !isa<GlobalValue>(Operand)){ |
| 1033 | Out << "["; |
| 1034 | CW.writeOperand(I->getOperand(i), false, Out); |
| 1035 | Out << "]"; |
| 1036 | } |
| 1037 | if (isa<GlobalValue>(Operand) && arrtype) |
| 1038 | Out << ")"; |
| 1039 | } |
| 1040 | else { |
| 1041 | if (arrtype == 1) Out << "["; |
| 1042 | else |
| 1043 | Out << ".field"; |
| 1044 | CW.writeOperand(I->getOperand(i), false, Out); |
| 1045 | if (arrtype == 1) Out << "]"; |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | Out << ");\n"; |
| 1050 | } |
| 1051 | |
| 1052 | void CInstPrintVisitor::visitPHINode(PHINode *I) { |
| 1053 | |
| 1054 | } |
| 1055 | |
| 1056 | void CInstPrintVisitor::visitUnaryOperator (UnaryOperator *I) { |
| 1057 | if (I->getOpcode() == Instruction::Not) { |
| 1058 | outputLValue(I); |
| 1059 | Operand = I->getNumOperands() ? I->getOperand(0) : 0; |
| 1060 | Out << "!("; |
| 1061 | CW.writeOperand(Operand,false, Out); |
| 1062 | Out << ");\n"; |
| 1063 | } |
| 1064 | else { |
| 1065 | Out << "<bad unary inst>\n"; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | void CInstPrintVisitor::visitBinaryOperator(BinaryOperator *I) { |
| 1070 | //binary instructions, shift instructions, setCond instructions. |
| 1071 | outputLValue(I); |
| 1072 | Operand = I->getNumOperands() ? I->getOperand(0) : 0; |
| 1073 | if (I->getType()->getPrimitiveID() == Type::PointerTyID) { |
| 1074 | Out << "("; |
| 1075 | CW.printType(I->getType(), Out); |
| 1076 | Out << ")"; |
| 1077 | } |
| 1078 | Out << "("; |
| 1079 | if (Operand->getType()->getPrimitiveID() == Type::PointerTyID) |
| 1080 | Out << "(long long)"; |
| 1081 | CW.writeOperand(Operand,false, Out); |
| 1082 | Out << getOpcodeOperName(I); |
| 1083 | // Need the extra parenthisis if the second operand is < 0 |
| 1084 | Out << '('; |
| 1085 | if (I->getOperand(1)->getType()->getPrimitiveID() == Type::PointerTyID) |
| 1086 | Out << "(long long)"; |
| 1087 | CW.writeOperand(I->getOperand(1),false, Out); |
| 1088 | Out << ')'; |
| 1089 | Out << ");\n"; |
| 1090 | } |
| 1091 | |
| 1092 | /* END : CInstPrintVisitor implementation */ |
| 1093 | |
| 1094 | void CWriter::printModule(const Module *M) { |
| 1095 | // printing stdlib inclusion |
| 1096 | // Out << "#include <stdlib.h>\n"; |
| 1097 | |
| 1098 | // Loop over the symbol table, emitting all named constants... |
| 1099 | if (M->hasSymbolTable()) |
| 1100 | printSymbolTable(*M->getSymbolTable()); |
| 1101 | |
| 1102 | for_each(M->gbegin(), M->gend(), |
| 1103 | bind_obj(this, &CWriter::printGlobal)); |
| 1104 | |
| 1105 | // First output all the declarations of the methods as C requires Functions |
| 1106 | // be declared before they are used. |
| 1107 | for_each(M->begin(), M->end(), bind_obj(this,&CWriter::printFunctionDecl)); |
| 1108 | |
| 1109 | // declaration of alloca |
| 1110 | Out << "void *alloca(unsigned long size);\n"; |
| 1111 | |
| 1112 | // Output all of the methods... |
| 1113 | for_each(M->begin(), M->end(), bind_obj(this,&CWriter::printFunction)); |
| 1114 | } |
| 1115 | |
| 1116 | // prints the global constants |
| 1117 | void CWriter::printGlobal(const GlobalVariable *GV) { |
| 1118 | string tempostr; |
| 1119 | if (GV->hasName()) |
| 1120 | tempostr = "llvm__" + makeNameProper(GV->getName()) + "_" + |
| 1121 | itostr((int)GV->getType()->getUniqueID()); |
| 1122 | if (GV->hasInternalLinkage()) Out << "static "; |
| 1123 | |
| 1124 | printTypeVar(GV->getType()->getElementType(), tempostr, Out); |
| 1125 | |
| 1126 | if (GV->hasInitializer()) { |
| 1127 | Out << " = " ; |
| 1128 | writeOperand(GV->getInitializer(), false, Out, false); |
| 1129 | } |
| 1130 | |
| 1131 | Out << ";\n"; |
| 1132 | } |
| 1133 | |
| 1134 | // printSymbolTable - Run through symbol table looking for named constants |
| 1135 | // if a named constant is found, emit it's declaration... |
| 1136 | // Assuming that symbol table has only types and constants. |
| 1137 | void CWriter::printSymbolTable(const SymbolTable &ST) { |
| 1138 | // GraphT G; |
| 1139 | for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) { |
| 1140 | SymbolTable::type_const_iterator I = ST.type_begin(TI->first); |
| 1141 | SymbolTable::type_const_iterator End = ST.type_end(TI->first); |
| 1142 | |
| 1143 | // TODO |
| 1144 | // Need to run through all the used types in the program |
| 1145 | // FindUsedTypes &FUT = new FindUsedTypes(); |
| 1146 | // const std::set<const Type *> &UsedTypes = FUT.getTypes(); |
| 1147 | // Filter out the structures printing forward definitions for each of them |
| 1148 | // and creating the dependency graph. |
| 1149 | // Print forward definitions to all of them |
| 1150 | // print the typedefs topologically sorted |
| 1151 | |
| 1152 | // But for now we have |
| 1153 | for (; I != End; ++I) { |
| 1154 | const Value *V = I->second; |
| 1155 | if (const Constant *CPV = dyn_cast<const Constant>(V)) { |
| 1156 | printConstant(CPV); |
| 1157 | } else if (const Type *Ty = dyn_cast<const Type>(V)) { |
| 1158 | string tempostr; |
| 1159 | string tempstr = ""; |
| 1160 | Out << "typedef "; |
| 1161 | vector<const Type *> TypeStack; |
| 1162 | tempostr = "llvm__" + I->first; |
| 1163 | string TypeNameVar = calcTypeNameVar(Ty, TypeStack, TypeNames, |
| 1164 | tempostr, tempstr); |
| 1165 | Out << TypeNameVar << ";\n"; |
| 1166 | if (!isa<PointerType>(Ty) || |
| 1167 | !cast<PointerType>(Ty)->getElementType()->isPrimitiveType()) |
| 1168 | TypeNames.insert(std::make_pair(Ty, "llvm__"+I->first)); |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | |
| 1175 | // printConstant - Print out a constant pool entry... |
| 1176 | // |
| 1177 | void CWriter::printConstant(const Constant *CPV) { |
| 1178 | // TODO |
| 1179 | // Dinakar : Don't know what to do with unnamed constants |
| 1180 | // should do something about it later. |
| 1181 | |
| 1182 | string tempostr; |
| 1183 | if (CPV->hasName()) { |
| 1184 | // Print out name... |
| 1185 | tempostr = "llvm__" + makeNameProper(CPV->getName()) + "_" + |
| 1186 | itostr((int)CPV->getType()->getUniqueID()); |
| 1187 | } else { |
| 1188 | int Slot = Table.getValSlot(CPV); // slot number |
| 1189 | if (Slot >= 0) |
| 1190 | tempostr = "llvm__tmp_" + itostr(Slot) + "_" + |
| 1191 | itostr((int)CPV->getType()->getUniqueID()); |
| 1192 | else |
| 1193 | tempostr = "<badref>"; |
| 1194 | } |
| 1195 | |
| 1196 | // Print out the constant type... |
| 1197 | printTypeVar(CPV->getType(), tempostr, Out); |
| 1198 | |
| 1199 | Out << " = "; |
| 1200 | // Write the value out now... |
| 1201 | writeOperand(CPV, false, Out, false); |
| 1202 | |
| 1203 | Out << "\n"; |
| 1204 | } |
| 1205 | |
| 1206 | |
| 1207 | |
| 1208 | // printFunctionDecl - Print method declaration |
| 1209 | // |
| 1210 | void CWriter::printFunctionDecl(const Function *M) { |
| 1211 | |
| 1212 | if (M->hasInternalLinkage()) Out <<"static "; |
| 1213 | |
| 1214 | // Loop over the arguments, printing them... |
| 1215 | const FunctionType *MT = cast<const FunctionType>(M->getFunctionType()); |
| 1216 | |
| 1217 | if (!M->isExternal()) { |
| 1218 | // Print out the return type and name... |
| 1219 | printType(M->getReturnType(), Out); |
| 1220 | Out << " " << makeNameProper(M->getName()) << "("; |
| 1221 | |
| 1222 | for_each(M->getArgumentList().begin(), M->getArgumentList().end(), |
| 1223 | bind_obj(this, &CWriter::printFunctionArgument)); |
| 1224 | } else { |
| 1225 | // Print out the return type and name... |
| 1226 | printType(M->getReturnType(), Out) ; |
| 1227 | Out << " " << makeNameProper(M->getName()) << "("; |
| 1228 | |
| 1229 | // Loop over the arguments, printing them... |
| 1230 | const FunctionType *MT = cast<const FunctionType>(M->getFunctionType()); |
| 1231 | for (FunctionType::ParamTypes::const_iterator I = |
| 1232 | MT->getParamTypes().begin(), |
| 1233 | E = MT->getParamTypes().end(); I != E; ++I) { |
| 1234 | if (I != MT->getParamTypes().begin()) Out << ", "; |
| 1235 | printType(*I, Out); |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | // Finish printing arguments... |
| 1240 | if (MT->isVarArg()) { |
| 1241 | if (MT->getParamTypes().size()) Out << ", "; |
| 1242 | Out << "..."; // Output varargs portion of signature! |
| 1243 | } |
| 1244 | Out << ");\n"; |
| 1245 | } |
| 1246 | |
| 1247 | void CWriter::printFunction(const Function *M) { |
| 1248 | if (!M->isExternal()) { |
| 1249 | // Process each of the basic blocks, gather information and call the |
| 1250 | // output methods on the CLocalVars and Function* objects. |
| 1251 | |
| 1252 | // gather local variable information for each basic block |
| 1253 | InstLocalVarsVisitor ILV(Table); |
| 1254 | ILV.visit((Function *)M); |
| 1255 | |
| 1256 | // Spout out code. |
| 1257 | outputFunction(M, ILV.CLV); |
| 1258 | |
| 1259 | } |
| 1260 | } |
| 1261 | |
| 1262 | // printFunctionArgument - This member is called for every argument that |
| 1263 | // is passed into the method. Simply print it out |
| 1264 | // |
| 1265 | void CWriter::printFunctionArgument(const Argument *Arg) { |
| 1266 | // Insert commas as we go... the first arg doesn't get a comma |
| 1267 | string tempostr; |
| 1268 | if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", "; |
| 1269 | |
| 1270 | // Output name, if available... |
| 1271 | if (Arg->hasName()) { |
| 1272 | tempostr = "llvm__" + makeNameProper(Arg->getName()) + "_" + |
| 1273 | itostr((int)Arg->getType()->getUniqueID()); |
| 1274 | } else if (Table.getValSlot(Arg) < 0) { |
| 1275 | tempostr = "<badref>"; |
| 1276 | } |
| 1277 | else { |
| 1278 | tempostr = "llvm__tmp_" + itostr(Table.getValSlot(Arg)) + "_" + |
| 1279 | itostr((int)Arg->getType()->getUniqueID()); |
| 1280 | } |
| 1281 | // Output type... |
| 1282 | // printType(Arg->getType(), Out); |
| 1283 | // Out << " " << tempostr; |
| 1284 | printTypeVar (Arg->getType(), tempostr, Out); |
| 1285 | } |
| 1286 | |
| 1287 | void CWriter::outputFunction(const Function *M, CLocalVars& CLV) { |
| 1288 | // Currently we have a no-loop-structure implementation |
| 1289 | // Seems like its not really necessary. |
| 1290 | |
| 1291 | // Print out the return type and name... |
| 1292 | printType(M->getReturnType(), Out) ; |
| 1293 | Out << " " << makeNameProper(M->getName()) << "("; |
| 1294 | // Loop over the arguments, printing them... |
| 1295 | const FunctionType *MT = cast<const FunctionType>(M->getFunctionType()); |
| 1296 | |
| 1297 | if (!M->isExternal()) { |
| 1298 | for_each(M->getArgumentList().begin(), M->getArgumentList().end(), |
| 1299 | bind_obj(this, &CWriter::printFunctionArgument)); |
| 1300 | } else { |
| 1301 | // Loop over the arguments, printing them... |
| 1302 | const FunctionType *MT = cast<const FunctionType>(M->getFunctionType()); |
| 1303 | for (FunctionType::ParamTypes::const_iterator I = |
| 1304 | MT->getParamTypes().begin(), |
| 1305 | E = MT->getParamTypes().end(); I != E; ++I) { |
| 1306 | if (I != MT->getParamTypes().begin()) Out << ", "; |
| 1307 | printType(*I, Out); |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | // Finish printing arguments... |
| 1312 | if (MT->isVarArg()) { |
| 1313 | if (MT->getParamTypes().size()) Out << ", "; |
| 1314 | Out << "..."; // Output varargs portion of signature! |
| 1315 | } |
| 1316 | Out << ")\n"; |
| 1317 | |
| 1318 | if (!M->isExternal()) { |
| 1319 | Out << "{\n"; |
| 1320 | // Loop over the symbol table, emitting all named constants... |
| 1321 | if (M->hasSymbolTable()) |
| 1322 | printSymbolTable(*M->getSymbolTable()); |
| 1323 | |
| 1324 | // print the local variables |
| 1325 | // we assume that every local variable is alloca'ed in the C code. |
| 1326 | std::map<const Type*, VarListType> locals; |
| 1327 | locals = CLV.LocalVars; |
| 1328 | |
| 1329 | map<const Type*, VarListType>::iterator iter; |
| 1330 | for (iter = locals.begin(); iter != locals.end(); iter++) { |
| 1331 | VarListType::iterator listiter; |
| 1332 | for (listiter = iter->second.begin(); listiter != iter->second.end(); |
| 1333 | listiter++) { |
| 1334 | // printType(iter->first, Out); |
| 1335 | // Out << " " << *listiter << ";\n"; |
| 1336 | printTypeVar(iter->first, *listiter, Out); |
| 1337 | Out << ";\n"; |
| 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | // print the basic blocks |
| 1342 | Function::const_iterator iterBB; |
| 1343 | for (iterBB = M->begin(); iterBB != M->end(); ++iterBB) |
| 1344 | outputBasicBlock(*iterBB); |
| 1345 | |
| 1346 | Out << "}\n"; |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | void CWriter::outputBasicBlock(const BasicBlock* BB) { |
| 1351 | |
| 1352 | if (BB->hasName()) { // Print out the label if it exists... |
| 1353 | Out << "llvm__" << makeNameProper(BB->getName()) << "_" |
| 1354 | << BB->getType()->getUniqueID() << ":\n"; |
| 1355 | } else { |
| 1356 | int Slot = Table.getValSlot(BB); |
| 1357 | Out << "llvm__tmp_"; |
| 1358 | if (Slot >= 0) |
| 1359 | Out << Slot << "_" << BB->getType()->getUniqueID() << ":\n"; |
| 1360 | // Extra newline seperates out label's |
| 1361 | else |
| 1362 | Out << "<badref>\n"; |
| 1363 | } |
| 1364 | |
| 1365 | // Output all of the instructions in the basic block... |
| 1366 | // print the basic blocks |
| 1367 | CInstPrintVisitor CIPV(*this, Table, Out); |
| 1368 | CIPV.visit((BasicBlock *) BB); |
| 1369 | } |
| 1370 | |
| 1371 | // printTypeVar - Go to extreme measures to attempt to print out a short, |
| 1372 | // symbolic version of a type name. |
| 1373 | // |
| 1374 | ostream& CWriter::printTypeVar(const Type *Ty, string VariableName, |
| 1375 | ostream &Out) { |
| 1376 | return printTypeVarInt(Out, Ty, TypeNames, VariableName); |
| 1377 | } |
| 1378 | |
| 1379 | // printType - Go to extreme measures to attempt to print out a short, symbolic |
| 1380 | // version of a type name. |
| 1381 | ostream& CWriter::printType(const Type *Ty, ostream &Out) { |
| 1382 | return printTypeInt(Out, Ty, TypeNames); |
| 1383 | } |
| 1384 | |
| 1385 | |
| 1386 | void CWriter::writeOperand(const Value *Operand, bool PrintType, |
| 1387 | ostream &Out, bool PrintName = true) { |
| 1388 | if (PrintType){ |
| 1389 | string tempstr = ""; |
| 1390 | Out << " "; |
| 1391 | printType(Operand->getType(), Out); |
| 1392 | } |
| 1393 | vector<const Type *> TypeStack; |
| 1394 | string MInfo = ""; |
| 1395 | string OperandType = calcTypeName(Operand->getType(), TypeStack, TypeNames, |
| 1396 | &MInfo); |
| 1397 | if (MInfo != "") |
| 1398 | OperandType += ")" + MInfo; |
| 1399 | WriteCOperandInternal(Out, Operand, PrintName, &Table, OperandType); |
| 1400 | } |
| 1401 | |
| 1402 | |
| 1403 | //===----------------------------------------------------------------------===// |
| 1404 | // External Interface declaration |
| 1405 | //===----------------------------------------------------------------------===// |
| 1406 | |
| 1407 | |
| 1408 | void WriteToC(const Module *C, ostream &Out) { |
| 1409 | assert(C && "You can't write a null module!!"); |
| 1410 | SlotCalculator SlotTable(C, true); |
| 1411 | CWriter W(Out, SlotTable, C); |
| 1412 | W.write(C); |
| 1413 | Out.flush(); |
| 1414 | } |
| 1415 | |