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