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