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