blob: 3dfd8f9cd00b6956cc69a02865f99395dc053e9b [file] [log] [blame]
Chris Lattner3af3ba82002-05-09 21:31:18 +00001//===-- Writer.cpp - Library for converting LLVM code to C ----------------===//
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00002//
Chris Lattnerd1cf1b42002-09-20 23:26:33 +00003// This library converts LLVM code to C code, compilable by GCC.
Chris Lattner16c7bb22002-05-09 02:28:59 +00004//
Chris Lattneredd8ce12003-05-03 03:14:35 +00005//===----------------------------------------------------------------------===//
Chris Lattner84e66652003-05-03 07:11:00 +00006
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00007#include "llvm/Assembly/CWriter.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00008#include "llvm/Constants.h"
Chris Lattner2f5f51a2002-05-09 03:28:37 +00009#include "llvm/DerivedTypes.h"
10#include "llvm/Module.h"
Chris Lattnera9f5e052003-04-22 20:19:52 +000011#include "llvm/Instructions.h"
Chris Lattner0e9f93e2002-08-31 00:29:16 +000012#include "llvm/Pass.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000013#include "llvm/SymbolTable.h"
Chris Lattner18ac3c82003-05-08 18:41:45 +000014#include "llvm/Intrinsics.h"
Chris Lattner3af3ba82002-05-09 21:31:18 +000015#include "llvm/SlotCalculator.h"
Chris Lattner0e9f93e2002-08-31 00:29:16 +000016#include "llvm/Analysis/FindUsedTypes.h"
Chris Lattnerd1cf1b42002-09-20 23:26:33 +000017#include "llvm/Analysis/ConstantsScanner.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000018#include "llvm/Support/InstVisitor.h"
Chris Lattner7683a122002-05-09 20:33:35 +000019#include "llvm/Support/InstIterator.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000020#include "Support/StringExtras.h"
21#include "Support/STLExtras.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000022#include <algorithm>
Chris Lattner78771c82002-05-17 04:55:35 +000023#include <set>
Nick Hildenbrandt98502372002-11-18 20:55:50 +000024#include <sstream>
Nick Hildenbrandt3cecdc52002-10-23 18:59:40 +000025
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000026namespace {
Chris Lattner0e9f93e2002-08-31 00:29:16 +000027 class CWriter : public Pass, public InstVisitor<CWriter> {
Chris Lattneredd8ce12003-05-03 03:14:35 +000028 std::ostream &Out;
Chris Lattner0e9f93e2002-08-31 00:29:16 +000029 SlotCalculator *Table;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000030 const Module *TheModule;
Chris Lattneredd8ce12003-05-03 03:14:35 +000031 std::map<const Type *, std::string> TypeNames;
Chris Lattner78771c82002-05-17 04:55:35 +000032 std::set<const Value*> MangledGlobals;
Nick Hildenbrandt3cecdc52002-10-23 18:59:40 +000033 bool needsMalloc;
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +000034
Chris Lattneredd8ce12003-05-03 03:14:35 +000035 std::map<const ConstantFP *, unsigned> FPConstantMap;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000036 public:
Chris Lattneredd8ce12003-05-03 03:14:35 +000037 CWriter(std::ostream &o) : Out(o) {}
Chris Lattner0e9f93e2002-08-31 00:29:16 +000038
39 void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.setPreservesAll();
41 AU.addRequired<FindUsedTypes>();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000042 }
Chris Lattner0e9f93e2002-08-31 00:29:16 +000043
44 virtual bool run(Module &M) {
45 // Initialize
46 Table = new SlotCalculator(&M, false);
47 TheModule = &M;
48
49 // Ensure that all structure types have names...
50 bool Changed = nameAllUsedStructureTypes(M);
51
52 // Run...
53 printModule(&M);
54
55 // Free memory...
56 delete Table;
57 TypeNames.clear();
58 MangledGlobals.clear();
59 return false;
60 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000061
Chris Lattneredd8ce12003-05-03 03:14:35 +000062 std::ostream &printType(std::ostream &Out, const Type *Ty,
63 const std::string &VariableName = "",
64 bool IgnoreName = false, bool namedContext = true);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000065
Chris Lattnerbb03efd2002-06-25 15:57:03 +000066 void writeOperand(Value *Operand);
67 void writeOperandInternal(Value *Operand);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000068
Chris Lattneredd8ce12003-05-03 03:14:35 +000069 std::string getValueName(const Value *V);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000070
Chris Lattner4fbf26d2002-05-09 20:53:56 +000071 private :
Chris Lattner0e9f93e2002-08-31 00:29:16 +000072 bool nameAllUsedStructureTypes(Module &M);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +000073 void printModule(Module *M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000074 void printSymbolTable(const SymbolTable &ST);
Chris Lattner2c601a72002-09-20 15:05:40 +000075 void printContainedStructs(const Type *Ty, std::set<const StructType *> &);
Chris Lattner4cda8352002-08-20 16:55:48 +000076 void printFunctionSignature(const Function *F, bool Prototype);
77
Chris Lattner8c3c4bf2002-05-09 15:49:41 +000078 void printFunction(Function *);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000079
Chris Lattner6d492922002-08-19 21:32:41 +000080 void printConstant(Constant *CPV);
81 void printConstantArray(ConstantArray *CPA);
82
Chris Lattnerd0c668c2002-05-09 21:18:38 +000083 // isInlinableInst - Attempt to inline instructions into their uses to build
84 // trees as much as possible. To do this, we have to consistently decide
85 // what is acceptable to inline, so that variable declarations don't get
86 // printed and an extra copy of the expr is not emitted.
87 //
Chris Lattnerbb03efd2002-06-25 15:57:03 +000088 static bool isInlinableInst(const Instruction &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +000089 // Must be an expression, must be used exactly once. If it is dead, we
90 // emit it inline where it would go.
Chris Lattnerbb03efd2002-06-25 15:57:03 +000091 if (I.getType() == Type::VoidTy || I.use_size() != 1 ||
Nick Hildenbrandt2cf2cbc2002-11-06 20:07:54 +000092 isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I) ||
93 isa<LoadInst>(I)) // Don't inline a load across a store!
Chris Lattnerd0c668c2002-05-09 21:18:38 +000094 return false;
95
96 // Only inline instruction it it's use is in the same BB as the inst.
Chris Lattnerbb03efd2002-06-25 15:57:03 +000097 return I.getParent() == cast<Instruction>(I.use_back())->getParent();
Chris Lattnerd0c668c2002-05-09 21:18:38 +000098 }
99
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000100 // Instruction visitation functions
101 friend class InstVisitor<CWriter>;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000102
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000103 void visitReturnInst(ReturnInst &I);
104 void visitBranchInst(BranchInst &I);
Chris Lattnera9f5e052003-04-22 20:19:52 +0000105 void visitSwitchInst(SwitchInst &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000106
Chris Lattner84e66652003-05-03 07:11:00 +0000107 void visitPHINode(PHINode &I);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000108 void visitBinaryOperator(Instruction &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000109
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000110 void visitCastInst (CastInst &I);
111 void visitCallInst (CallInst &I);
112 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000113
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000114 void visitMallocInst(MallocInst &I);
115 void visitAllocaInst(AllocaInst &I);
116 void visitFreeInst (FreeInst &I);
117 void visitLoadInst (LoadInst &I);
118 void visitStoreInst (StoreInst &I);
119 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner18ac3c82003-05-08 18:41:45 +0000120 void visitVarArgInst(VarArgInst &I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000121
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000122 void visitInstruction(Instruction &I) {
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000123 std::cerr << "C Writer does not know about " << I;
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000124 abort();
125 }
126
127 void outputLValue(Instruction *I) {
128 Out << " " << getValueName(I) << " = ";
129 }
130 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
131 unsigned Indent);
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000132 void printIndexingExpression(Value *Ptr, User::op_iterator I,
133 User::op_iterator E);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000134 };
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000135}
136
Chris Lattner2f499022002-05-09 04:21:21 +0000137// We dont want identifier names with ., space, - in them.
138// So we replace them with _
Chris Lattneredd8ce12003-05-03 03:14:35 +0000139static std::string makeNameProper(std::string x) {
140 std::string tmp;
141 for (std::string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
Chris Lattner2f499022002-05-09 04:21:21 +0000142 switch (*sI) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000143 case '.': tmp += "d_"; break;
144 case ' ': tmp += "s_"; break;
145 case '-': tmp += "D_"; break;
Chris Lattner2f499022002-05-09 04:21:21 +0000146 default: tmp += *sI;
147 }
148
149 return tmp;
150}
151
Chris Lattneredd8ce12003-05-03 03:14:35 +0000152std::string CWriter::getValueName(const Value *V) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000153 if (V->hasName()) { // Print out the label if it exists...
154 if (isa<GlobalValue>(V) && // Do not mangle globals...
Chris Lattneredd8ce12003-05-03 03:14:35 +0000155 (cast<GlobalValue>(V)->hasExternalLinkage() &&// Unless it's internal or
156 !MangledGlobals.count(V))) // Unless the name would collide if we don't
Chris Lattner2f499022002-05-09 04:21:21 +0000157 return makeNameProper(V->getName());
158
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000159 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
160 makeNameProper(V->getName());
Chris Lattner2f499022002-05-09 04:21:21 +0000161 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000162
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000163 int Slot = Table->getValSlot(V);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000164 assert(Slot >= 0 && "Invalid value!");
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000165 return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000166}
167
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000168// A pointer type should not use parens around *'s alone, e.g., (**)
Chris Lattneredd8ce12003-05-03 03:14:35 +0000169inline bool ptrTypeNameNeedsParens(const std::string &NameSoFar) {
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000170 return (NameSoFar.find_last_not_of('*') != std::string::npos);
171}
172
Chris Lattner83c57752002-08-19 22:17:53 +0000173// Pass the Type* and the variable name and this prints out the variable
174// declaration.
175//
Chris Lattneredd8ce12003-05-03 03:14:35 +0000176std::ostream &CWriter::printType(std::ostream &Out, const Type *Ty,
177 const std::string &NameSoFar,
178 bool IgnoreName, bool namedContext) {
Chris Lattner83c57752002-08-19 22:17:53 +0000179 if (Ty->isPrimitiveType())
180 switch (Ty->getPrimitiveID()) {
181 case Type::VoidTyID: return Out << "void " << NameSoFar;
182 case Type::BoolTyID: return Out << "bool " << NameSoFar;
183 case Type::UByteTyID: return Out << "unsigned char " << NameSoFar;
184 case Type::SByteTyID: return Out << "signed char " << NameSoFar;
185 case Type::UShortTyID: return Out << "unsigned short " << NameSoFar;
186 case Type::ShortTyID: return Out << "short " << NameSoFar;
187 case Type::UIntTyID: return Out << "unsigned " << NameSoFar;
188 case Type::IntTyID: return Out << "int " << NameSoFar;
189 case Type::ULongTyID: return Out << "unsigned long long " << NameSoFar;
190 case Type::LongTyID: return Out << "signed long long " << NameSoFar;
191 case Type::FloatTyID: return Out << "float " << NameSoFar;
192 case Type::DoubleTyID: return Out << "double " << NameSoFar;
193 default :
194 std::cerr << "Unknown primitive type: " << Ty << "\n";
195 abort();
196 }
197
198 // Check to see if the type is named.
Chris Lattner04b72c82002-10-16 00:08:22 +0000199 if (!IgnoreName || isa<OpaqueType>(Ty)) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000200 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner83c57752002-08-19 22:17:53 +0000201 if (I != TypeNames.end()) {
202 return Out << I->second << " " << NameSoFar;
203 }
Chris Lattner04b72c82002-10-16 00:08:22 +0000204 }
Chris Lattner83c57752002-08-19 22:17:53 +0000205
Chris Lattner83c57752002-08-19 22:17:53 +0000206 switch (Ty->getPrimitiveID()) {
207 case Type::FunctionTyID: {
208 const FunctionType *MTy = cast<FunctionType>(Ty);
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000209 std::stringstream FunctionInards;
210 FunctionInards << " (" << NameSoFar << ") (";
Chris Lattner83c57752002-08-19 22:17:53 +0000211 for (FunctionType::ParamTypes::const_iterator
212 I = MTy->getParamTypes().begin(),
213 E = MTy->getParamTypes().end(); I != E; ++I) {
214 if (I != MTy->getParamTypes().begin())
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000215 FunctionInards << ", ";
216 printType(FunctionInards, *I, "");
Chris Lattner83c57752002-08-19 22:17:53 +0000217 }
218 if (MTy->isVarArg()) {
219 if (!MTy->getParamTypes().empty())
Chris Lattnerddfc03c2003-05-13 20:15:37 +0000220 FunctionInards << ", ...";
221 } else if (MTy->getParamTypes().empty()) {
222 FunctionInards << "void";
Chris Lattner83c57752002-08-19 22:17:53 +0000223 }
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000224 FunctionInards << ")";
Chris Lattneredd8ce12003-05-03 03:14:35 +0000225 std::string tstr = FunctionInards.str();
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000226 printType(Out, MTy->getReturnType(), tstr);
227 return Out;
Chris Lattner83c57752002-08-19 22:17:53 +0000228 }
229 case Type::StructTyID: {
230 const StructType *STy = cast<StructType>(Ty);
231 Out << NameSoFar + " {\n";
232 unsigned Idx = 0;
233 for (StructType::ElementTypes::const_iterator
234 I = STy->getElementTypes().begin(),
235 E = STy->getElementTypes().end(); I != E; ++I) {
236 Out << " ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000237 printType(Out, *I, "field" + utostr(Idx++));
Chris Lattner83c57752002-08-19 22:17:53 +0000238 Out << ";\n";
239 }
240 return Out << "}";
241 }
242
243 case Type::PointerTyID: {
244 const PointerType *PTy = cast<PointerType>(Ty);
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000245 std::string ptrName = "*" + NameSoFar;
246
247 // Do not need parens around "* NameSoFar" if NameSoFar consists only
248 // of zero or more '*' chars *and* this is not an unnamed pointer type
249 // such as the result type in a cast statement. Otherwise, enclose in ( ).
Nick Hildenbrandtc14ded42002-09-23 21:02:50 +0000250 if (ptrTypeNameNeedsParens(NameSoFar) || !namedContext ||
251 PTy->getElementType()->getPrimitiveID() == Type::ArrayTyID)
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000252 ptrName = "(" + ptrName + ")"; //
253
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000254 return printType(Out, PTy->getElementType(), ptrName);
255 }Out <<"--";
Chris Lattner83c57752002-08-19 22:17:53 +0000256
257 case Type::ArrayTyID: {
258 const ArrayType *ATy = cast<ArrayType>(Ty);
259 unsigned NumElements = ATy->getNumElements();
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000260 return printType(Out, ATy->getElementType(),
Chris Lattner83c57752002-08-19 22:17:53 +0000261 NameSoFar + "[" + utostr(NumElements) + "]");
262 }
Chris Lattner04b72c82002-10-16 00:08:22 +0000263
264 case Type::OpaqueTyID: {
265 static int Count = 0;
Chris Lattneredd8ce12003-05-03 03:14:35 +0000266 std::string TyName = "struct opaque_" + itostr(Count++);
Chris Lattner04b72c82002-10-16 00:08:22 +0000267 assert(TypeNames.find(Ty) == TypeNames.end());
268 TypeNames[Ty] = TyName;
269 return Out << TyName << " " << NameSoFar;
270 }
Chris Lattner83c57752002-08-19 22:17:53 +0000271 default:
272 assert(0 && "Unhandled case in getTypeProps!");
273 abort();
274 }
275
276 return Out;
277}
278
Chris Lattner6d492922002-08-19 21:32:41 +0000279void CWriter::printConstantArray(ConstantArray *CPA) {
280
281 // As a special case, print the array as a string if it is an array of
282 // ubytes or an array of sbytes with positive values.
283 //
284 const Type *ETy = CPA->getType()->getElementType();
285 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
286
287 // Make sure the last character is a null char, as automatically added by C
Chris Lattner02da6c02003-06-16 12:09:09 +0000288 if (isString && (CPA->getNumOperands() == 0 ||
289 !cast<Constant>(*(CPA->op_end()-1))->isNullValue()))
Chris Lattner6d492922002-08-19 21:32:41 +0000290 isString = false;
291
292 if (isString) {
293 Out << "\"";
Chris Lattner02da6c02003-06-16 12:09:09 +0000294 // Keep track of whether the last number was a hexadecimal escape
295 bool LastWasHex = false;
296
Chris Lattner6d492922002-08-19 21:32:41 +0000297 // Do not include the last character, which we know is null
298 for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) {
299 unsigned char C = (ETy == Type::SByteTy) ?
300 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
301 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
302
Chris Lattner02da6c02003-06-16 12:09:09 +0000303 // Print it out literally if it is a printable character. The only thing
304 // to be careful about is when the last letter output was a hex escape
305 // code, in which case we have to be careful not to print out hex digits
Chris Lattnerda920902003-06-16 12:21:19 +0000306 // explicitly (the C compiler thinks it is a continuation of the previous
307 // character, sheesh...)
Chris Lattner02da6c02003-06-16 12:09:09 +0000308 //
309 if (isprint(C) && (!LastWasHex || !isxdigit(C))) {
310 LastWasHex = false;
Nick Hildenbrandt088b4722002-11-06 21:40:23 +0000311 if (C == '"' || C == '\\')
312 Out << "\\" << C;
Nick Hildenbrandtc3dd2af2002-09-30 21:11:55 +0000313 else
314 Out << C;
Chris Lattner6d492922002-08-19 21:32:41 +0000315 } else {
Chris Lattner02da6c02003-06-16 12:09:09 +0000316 LastWasHex = false;
Chris Lattner6d492922002-08-19 21:32:41 +0000317 switch (C) {
318 case '\n': Out << "\\n"; break;
319 case '\t': Out << "\\t"; break;
320 case '\r': Out << "\\r"; break;
321 case '\v': Out << "\\v"; break;
322 case '\a': Out << "\\a"; break;
Nick Hildenbrandtc3dd2af2002-09-30 21:11:55 +0000323 case '\"': Out << "\\\""; break;
324 case '\'': Out << "\\\'"; break;
Chris Lattner6d492922002-08-19 21:32:41 +0000325 default:
326 Out << "\\x";
Chris Lattner02da6c02003-06-16 12:09:09 +0000327 Out << (char)(( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'));
328 Out << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
329 LastWasHex = true;
Chris Lattner6d492922002-08-19 21:32:41 +0000330 break;
331 }
332 }
333 }
334 Out << "\"";
335 } else {
336 Out << "{";
337 if (CPA->getNumOperands()) {
338 Out << " ";
339 printConstant(cast<Constant>(CPA->getOperand(0)));
340 for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
341 Out << ", ";
342 printConstant(cast<Constant>(CPA->getOperand(i)));
343 }
344 }
345 Out << " }";
346 }
347}
348
349
350// printConstant - The LLVM Constant to C Constant converter.
351void CWriter::printConstant(Constant *CPV) {
352 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
353 switch (CE->getOpcode()) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000354 case Instruction::Cast:
355 Out << "((";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000356 printType(Out, CPV->getType());
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000357 Out << ")";
Chris Lattner84c0d5e2003-05-14 17:50:19 +0000358 printConstant(CE->getOperand(0));
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000359 Out << ")";
360 return;
361
362 case Instruction::GetElementPtr:
Nick Hildenbrandtd2eb3862002-10-03 20:47:24 +0000363 Out << "(&(";
Chris Lattner84c0d5e2003-05-14 17:50:19 +0000364 printIndexingExpression(CE->getOperand(0),
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000365 CPV->op_begin()+1, CPV->op_end());
Nick Hildenbrandtd2eb3862002-10-03 20:47:24 +0000366 Out << "))";
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000367 return;
368 case Instruction::Add:
369 Out << "(";
Chris Lattner84c0d5e2003-05-14 17:50:19 +0000370 printConstant(CE->getOperand(0));
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000371 Out << " + ";
Chris Lattner84c0d5e2003-05-14 17:50:19 +0000372 printConstant(CE->getOperand(1));
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000373 Out << ")";
374 return;
375 case Instruction::Sub:
376 Out << "(";
Chris Lattner84c0d5e2003-05-14 17:50:19 +0000377 printConstant(CE->getOperand(0));
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000378 Out << " - ";
Chris Lattner84c0d5e2003-05-14 17:50:19 +0000379 printConstant(CE->getOperand(1));
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000380 Out << ")";
381 return;
382
Chris Lattner6d492922002-08-19 21:32:41 +0000383 default:
384 std::cerr << "CWriter Error: Unhandled constant expression: "
385 << CE << "\n";
386 abort();
387 }
388 }
389
390 switch (CPV->getType()->getPrimitiveID()) {
391 case Type::BoolTyID:
392 Out << (CPV == ConstantBool::False ? "0" : "1"); break;
393 case Type::SByteTyID:
394 case Type::ShortTyID:
Chris Lattner6d492922002-08-19 21:32:41 +0000395 Out << cast<ConstantSInt>(CPV)->getValue(); break;
Chris Lattner45343ea2003-05-12 15:39:31 +0000396 case Type::IntTyID:
397 if ((int)cast<ConstantSInt>(CPV)->getValue() == (int)0x80000000)
398 Out << "((int)0x80000000)"; // Handle MININT specially to avoid warning
399 else
400 Out << cast<ConstantSInt>(CPV)->getValue();
401 break;
402
Chris Lattner6d492922002-08-19 21:32:41 +0000403 case Type::LongTyID:
404 Out << cast<ConstantSInt>(CPV)->getValue() << "ll"; break;
405
406 case Type::UByteTyID:
407 case Type::UShortTyID:
408 Out << cast<ConstantUInt>(CPV)->getValue(); break;
409 case Type::UIntTyID:
410 Out << cast<ConstantUInt>(CPV)->getValue() << "u"; break;
411 case Type::ULongTyID:
412 Out << cast<ConstantUInt>(CPV)->getValue() << "ull"; break;
413
414 case Type::FloatTyID:
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000415 case Type::DoubleTyID: {
416 ConstantFP *FPC = cast<ConstantFP>(CPV);
Chris Lattneredd8ce12003-05-03 03:14:35 +0000417 std::map<const ConstantFP*, unsigned>::iterator I = FPConstantMap.find(FPC);
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000418 if (I != FPConstantMap.end()) {
419 // Because of FP precision problems we must load from a stack allocated
420 // value that holds the value in hex.
421 Out << "(*(" << (FPC->getType() == Type::FloatTy ? "float" : "double")
422 << "*)&FloatConstant" << I->second << ")";
423 } else {
424 Out << FPC->getValue();
425 }
426 break;
427 }
Chris Lattner6d492922002-08-19 21:32:41 +0000428
429 case Type::ArrayTyID:
430 printConstantArray(cast<ConstantArray>(CPV));
431 break;
432
433 case Type::StructTyID: {
434 Out << "{";
435 if (CPV->getNumOperands()) {
436 Out << " ";
437 printConstant(cast<Constant>(CPV->getOperand(0)));
438 for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) {
439 Out << ", ";
440 printConstant(cast<Constant>(CPV->getOperand(i)));
441 }
442 }
443 Out << " }";
444 break;
445 }
446
447 case Type::PointerTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000448 if (isa<ConstantPointerNull>(CPV)) {
Chris Lattnercf135cb2003-06-02 03:10:53 +0000449 Out << "((";
450 printType(Out, CPV->getType());
451 Out << ")/*NULL*/0)";
Chris Lattner6d492922002-08-19 21:32:41 +0000452 break;
453 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
454 writeOperand(CPR->getValue());
455 break;
456 }
457 // FALL THROUGH
458 default:
459 std::cerr << "Unknown constant type: " << CPV << "\n";
460 abort();
461 }
462}
463
464void CWriter::writeOperandInternal(Value *Operand) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000465 if (Instruction *I = dyn_cast<Instruction>(Operand))
466 if (isInlinableInst(*I)) {
467 // Should we inline this instruction to build a tree?
468 Out << "(";
469 visit(*I);
470 Out << ")";
471 return;
472 }
473
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +0000474 if (Operand->hasName()) {
Chris Lattner6d492922002-08-19 21:32:41 +0000475 Out << getValueName(Operand);
476 } else if (Constant *CPV = dyn_cast<Constant>(Operand)) {
477 printConstant(CPV);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000478 } else {
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000479 int Slot = Table->getValSlot(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000480 assert(Slot >= 0 && "Malformed LLVM!");
481 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
482 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000483}
484
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000485void CWriter::writeOperand(Value *Operand) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000486 if (isa<GlobalVariable>(Operand))
487 Out << "(&"; // Global variables are references as their addresses by llvm
488
489 writeOperandInternal(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000490
491 if (isa<GlobalVariable>(Operand))
492 Out << ")";
493}
494
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000495// nameAllUsedStructureTypes - If there are structure types in the module that
496// are used but do not have names assigned to them in the symbol table yet then
497// we assign them names now.
498//
499bool CWriter::nameAllUsedStructureTypes(Module &M) {
500 // Get a set of types that are used by the program...
501 std::set<const Type *> UT = getAnalysis<FindUsedTypes>().getTypes();
502
503 // Loop over the module symbol table, removing types from UT that are already
504 // named.
505 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000506 SymbolTable &MST = M.getSymbolTable();
507 if (MST.find(Type::TypeTy) != MST.end())
508 for (SymbolTable::type_iterator I = MST.type_begin(Type::TypeTy),
509 E = MST.type_end(Type::TypeTy); I != E; ++I)
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000510 UT.erase(cast<Type>(I->second));
511
512 // UT now contains types that are not named. Loop over it, naming structure
513 // types.
514 //
515 bool Changed = false;
516 for (std::set<const Type *>::const_iterator I = UT.begin(), E = UT.end();
517 I != E; ++I)
518 if (const StructType *ST = dyn_cast<StructType>(*I)) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000519 ((Value*)ST)->setName("unnamed", &MST);
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000520 Changed = true;
521 }
522 return Changed;
523}
524
Chris Lattneredd8ce12003-05-03 03:14:35 +0000525static void generateAllocaDecl(std::ostream& Out) {
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000526 // On SunOS, we need to insert the alloca macro & proto for the builtin.
527 Out << "#ifdef sun\n"
528 << "extern void *__builtin_alloca(unsigned long);\n"
529 << "#define alloca(x) __builtin_alloca(x)\n"
530 << "#else\n"
Brian Gaekea0145cc2003-06-16 23:57:13 +0000531 << "#ifndef __FreeBSD__\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000532 << "#include <alloca.h>\n"
Brian Gaekea0145cc2003-06-16 23:57:13 +0000533 << "#endif\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000534 << "#endif\n\n";
535}
536
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000537void CWriter::printModule(Module *M) {
Chris Lattner78771c82002-05-17 04:55:35 +0000538 // Calculate which global values have names that will collide when we throw
539 // away type information.
Chris Lattner594b9fa2002-05-21 21:10:04 +0000540 { // Scope to delete the FoundNames set when we are done with it...
Chris Lattneredd8ce12003-05-03 03:14:35 +0000541 std::set<std::string> FoundNames;
Chris Lattner78771c82002-05-17 04:55:35 +0000542 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000543 if (I->hasName()) // If the global has a name...
544 if (FoundNames.count(I->getName())) // And the name is already used
545 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000546 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000547 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000548
549 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000550 if (I->hasName()) // If the global has a name...
551 if (FoundNames.count(I->getName())) // And the name is already used
552 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000553 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000554 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000555 }
556
Chris Lattner16c7bb22002-05-09 02:28:59 +0000557 // get declaration for alloca
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000558 Out << "/* Provide Declarations */\n";
559 generateAllocaDecl(Out);
Chris Lattner18ac3c82003-05-08 18:41:45 +0000560 Out << "#include <stdarg.h>\n";
Chris Lattnerc436b372003-05-17 22:26:33 +0000561 Out << "#include <setjmp.h>\n";
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000562
Chris Lattnercf135cb2003-06-02 03:10:53 +0000563 // Provide a definition for `bool' if not compiling with a C++ compiler.
564 Out << "\n"
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000565 << "#ifndef __cplusplus\ntypedef unsigned char bool;\n#endif\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000566
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000567 << "\n\n/* Support for floating point constants */\n"
568 << "typedef unsigned long long ConstantDoubleTy;\n"
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000569 << "typedef unsigned int ConstantFloatTy;\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000570
Chris Lattnera4d4a852002-08-19 21:48:40 +0000571 << "\n\n/* Global Declarations */\n";
572
573 // First output all the declarations for the program, because C requires
574 // Functions & globals to be declared before they are used.
575 //
Chris Lattner16c7bb22002-05-09 02:28:59 +0000576
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000577 // Loop over the symbol table, emitting all named constants...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000578 printSymbolTable(M->getSymbolTable());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000579
Chris Lattnera4d4a852002-08-19 21:48:40 +0000580 // Global variable declarations...
581 if (!M->gempty()) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000582 Out << "\n/* External Global Variable Declarations */\n";
Chris Lattnera4d4a852002-08-19 21:48:40 +0000583 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000584 if (I->hasExternalLinkage()) {
585 Out << "extern ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000586 printType(Out, I->getType()->getElementType(), getValueName(I));
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000587 Out << ";\n";
588 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000589 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000590 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000591
Chris Lattnera4d4a852002-08-19 21:48:40 +0000592 // Function declarations
593 if (!M->empty()) {
594 Out << "\n/* Function Declarations */\n";
Nick Hildenbrandt3cecdc52002-10-23 18:59:40 +0000595 needsMalloc = true;
Chris Lattner4cda8352002-08-20 16:55:48 +0000596 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
Nick Hildenbrandta1a64f82002-11-18 22:21:52 +0000597 // If the function is external and the name collides don't print it.
Chris Lattner18ac3c82003-05-08 18:41:45 +0000598 // Sometimes the bytecode likes to have multiple "declarations" for
Chris Lattneredd8ce12003-05-03 03:14:35 +0000599 // external functions
Chris Lattner18ac3c82003-05-08 18:41:45 +0000600 if ((I->hasInternalLinkage() || !MangledGlobals.count(I)) &&
601 !I->getIntrinsicID()) {
Nick Hildenbrandta1a64f82002-11-18 22:21:52 +0000602 printFunctionSignature(I, true);
603 Out << ";\n";
604 }
Chris Lattner4cda8352002-08-20 16:55:48 +0000605 }
Chris Lattnera4d4a852002-08-19 21:48:40 +0000606 }
607
Nick Hildenbrandt3cecdc52002-10-23 18:59:40 +0000608 // Print Malloc prototype if needed
609 if (needsMalloc){
610 Out << "\n/* Malloc to make sun happy */\n";
611 Out << "extern void * malloc(size_t);\n\n";
612 }
613
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +0000614 // Output the global variable declerations
615 if (!M->gempty()) {
616 Out << "\n\n/* Global Variable Declerations */\n";
617 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
618 if (!I->isExternal()) {
619 Out << "extern ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000620 printType(Out, I->getType()->getElementType(), getValueName(I));
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +0000621
622 Out << ";\n";
623 }
624 }
625
626
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000627 // Output the global variable definitions and contents...
Chris Lattnera4d4a852002-08-19 21:48:40 +0000628 if (!M->gempty()) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000629 Out << "\n\n/* Global Variable Definitions and Initialization */\n";
Chris Lattnere8e035b2002-10-16 20:08:47 +0000630 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
631 if (!I->isExternal()) {
632 if (I->hasInternalLinkage())
633 Out << "static ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000634 printType(Out, I->getType()->getElementType(), getValueName(I));
Chris Lattner940b08d2003-06-06 07:10:24 +0000635 if (!I->getInitializer()->isNullValue()) {
636 Out << " = " ;
637 writeOperand(I->getInitializer());
638 }
Chris Lattnere8e035b2002-10-16 20:08:47 +0000639 Out << ";\n";
Chris Lattnera4d4a852002-08-19 21:48:40 +0000640 }
Chris Lattnera4d4a852002-08-19 21:48:40 +0000641 }
642
Chris Lattner16c7bb22002-05-09 02:28:59 +0000643 // Output all of the functions...
Chris Lattnera4d4a852002-08-19 21:48:40 +0000644 if (!M->empty()) {
645 Out << "\n\n/* Function Bodies */\n";
646 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
647 printFunction(I);
648 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000649}
650
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000651
Chris Lattner2db41cd2002-09-20 15:12:13 +0000652/// printSymbolTable - Run through symbol table looking for type names. If a
653/// type name is found, emit it's declaration...
Chris Lattnera4c047e2002-09-20 14:56:54 +0000654///
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000655void CWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattner2db41cd2002-09-20 15:12:13 +0000656 // If there are no type names, exit early.
657 if (ST.find(Type::TypeTy) == ST.end())
658 return;
659
660 // We are only interested in the type plane of the symbol table...
661 SymbolTable::type_const_iterator I = ST.type_begin(Type::TypeTy);
662 SymbolTable::type_const_iterator End = ST.type_end(Type::TypeTy);
663
Chris Lattner58d04d42002-09-20 15:18:30 +0000664 // Print out forward declarations for structure types before anything else!
665 Out << "/* Structure forward decls */\n";
666 for (; I != End; ++I)
667 if (const Type *STy = dyn_cast<StructType>(I->second)) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000668 std::string Name = "struct l_" + makeNameProper(I->first);
Chris Lattner58d04d42002-09-20 15:18:30 +0000669 Out << Name << ";\n";
670 TypeNames.insert(std::make_pair(STy, Name));
671 }
672
673 Out << "\n";
674
675 // Now we can print out typedefs...
676 Out << "/* Typedefs */\n";
677 for (I = ST.type_begin(Type::TypeTy); I != End; ++I) {
678 const Type *Ty = cast<Type>(I->second);
Chris Lattneredd8ce12003-05-03 03:14:35 +0000679 std::string Name = "l_" + makeNameProper(I->first);
Chris Lattner58d04d42002-09-20 15:18:30 +0000680 Out << "typedef ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000681 printType(Out, Ty, Name);
Chris Lattner58d04d42002-09-20 15:18:30 +0000682 Out << ";\n";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000683 }
684
685 Out << "\n";
686
Chris Lattner2c601a72002-09-20 15:05:40 +0000687 // Keep track of which structures have been printed so far...
688 std::set<const StructType *> StructPrinted;
689
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000690 // Loop over all structures then push them into the stack so they are
691 // printed in the correct order.
Chris Lattner2db41cd2002-09-20 15:12:13 +0000692 //
Chris Lattner58d04d42002-09-20 15:18:30 +0000693 Out << "/* Structure contents */\n";
Chris Lattner2db41cd2002-09-20 15:12:13 +0000694 for (I = ST.type_begin(Type::TypeTy); I != End; ++I)
695 if (const StructType *STy = dyn_cast<StructType>(I->second))
696 printContainedStructs(STy, StructPrinted);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000697}
698
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000699// Push the struct onto the stack and recursively push all structs
700// this one depends on.
Chris Lattner2c601a72002-09-20 15:05:40 +0000701void CWriter::printContainedStructs(const Type *Ty,
702 std::set<const StructType*> &StructPrinted){
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000703 if (const StructType *STy = dyn_cast<StructType>(Ty)){
704 //Check to see if we have already printed this struct
Chris Lattner2c601a72002-09-20 15:05:40 +0000705 if (StructPrinted.count(STy) == 0) {
706 // Print all contained types first...
Chris Lattnera4c047e2002-09-20 14:56:54 +0000707 for (StructType::ElementTypes::const_iterator
708 I = STy->getElementTypes().begin(),
709 E = STy->getElementTypes().end(); I != E; ++I) {
Chris Lattner2c601a72002-09-20 15:05:40 +0000710 const Type *Ty1 = I->get();
Chris Lattnera4c047e2002-09-20 14:56:54 +0000711 if (isa<StructType>(Ty1) || isa<ArrayType>(Ty1))
Chris Lattnerab2b3282003-05-29 15:12:27 +0000712 printContainedStructs(*I, StructPrinted);
Chris Lattnera4c047e2002-09-20 14:56:54 +0000713 }
714
Chris Lattner2c601a72002-09-20 15:05:40 +0000715 //Print structure type out..
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000716 StructPrinted.insert(STy);
Chris Lattneredd8ce12003-05-03 03:14:35 +0000717 std::string Name = TypeNames[STy];
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000718 printType(Out, STy, Name, true);
Chris Lattner58d04d42002-09-20 15:18:30 +0000719 Out << ";\n\n";
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000720 }
Chris Lattnera4c047e2002-09-20 14:56:54 +0000721
Chris Lattner2c601a72002-09-20 15:05:40 +0000722 // If it is an array, check contained types and continue
723 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)){
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000724 const Type *Ty1 = ATy->getElementType();
725 if (isa<StructType>(Ty1) || isa<ArrayType>(Ty1))
Chris Lattner2c601a72002-09-20 15:05:40 +0000726 printContainedStructs(Ty1, StructPrinted);
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000727 }
728}
729
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000730
Chris Lattner4cda8352002-08-20 16:55:48 +0000731void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
Nick Hildenbrandt3cecdc52002-10-23 18:59:40 +0000732 // If the program provides it's own malloc prototype we don't need
733 // to include the general one.
734 if (getValueName(F) == "malloc")
735 needsMalloc = false;
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000736 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000737 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000738 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000739
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000740 std::stringstream FunctionInards;
741
742 // Print out the name...
743 FunctionInards << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000744
Chris Lattner16c7bb22002-05-09 02:28:59 +0000745 if (!F->isExternal()) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000746 if (!F->aempty()) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000747 std::string ArgName;
Chris Lattner4cda8352002-08-20 16:55:48 +0000748 if (F->abegin()->hasName() || !Prototype)
749 ArgName = getValueName(F->abegin());
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000750 printType(FunctionInards, F->afront().getType(), ArgName);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000751 for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
752 I != E; ++I) {
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000753 FunctionInards << ", ";
Chris Lattner4cda8352002-08-20 16:55:48 +0000754 if (I->hasName() || !Prototype)
755 ArgName = getValueName(I);
756 else
757 ArgName = "";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000758 printType(FunctionInards, I->getType(), ArgName);
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000759 }
760 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000761 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000762 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000763 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000764 FT->getParamTypes().begin(),
765 E = FT->getParamTypes().end(); I != E; ++I) {
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000766 if (I != FT->getParamTypes().begin()) FunctionInards << ", ";
767 printType(FunctionInards, *I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000768 }
769 }
770
Chris Lattner1f8c4a12002-09-20 22:32:30 +0000771 // Finish printing arguments... if this is a vararg function, print the ...,
772 // unless there are no known types, in which case, we just emit ().
773 //
774 if (FT->isVarArg() && !FT->getParamTypes().empty()) {
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000775 if (FT->getParamTypes().size()) FunctionInards << ", ";
776 FunctionInards << "..."; // Output varargs portion of signature!
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000777 }
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000778 FunctionInards << ")";
779 // Print out the return type and the entire signature for that matter
780 printType(Out, F->getReturnType(), FunctionInards.str());
781
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000782}
783
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000784
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000785void CWriter::printFunction(Function *F) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000786 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000787
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000788 Table->incorporateFunction(F);
Chris Lattnerf34ee812002-05-09 03:12:34 +0000789
Chris Lattner4cda8352002-08-20 16:55:48 +0000790 printFunctionSignature(F, false);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000791 Out << " {\n";
792
Chris Lattner497e19a2002-05-09 20:39:03 +0000793 // print local variable information for the function
Chris Lattner7683a122002-05-09 20:33:35 +0000794 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000795 if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000796 Out << " ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000797 printType(Out, (*I)->getType(), getValueName(*I));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000798 Out << ";\n";
Chris Lattner84e66652003-05-03 07:11:00 +0000799
800 if (isa<PHINode>(*I)) { // Print out PHI node temporaries as well...
801 Out << " ";
802 printType(Out, (*I)->getType(), getValueName(*I)+"__PHI_TEMPORARY");
803 Out << ";\n";
804 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000805 }
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000806
807 Out << "\n";
808
809 // Scan the function for floating point constants. If any FP constant is used
810 // in the function, we want to redirect it here so that we do not depend on
811 // the precision of the printed form.
812 //
813 unsigned FPCounter = 0;
814 for (constant_iterator I = constant_begin(F), E = constant_end(F); I != E;++I)
815 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(*I))
816 if (FPConstantMap.find(FPC) == FPConstantMap.end()) {
817 double Val = FPC->getValue();
818
819 FPConstantMap[FPC] = FPCounter; // Number the FP constants
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000820
821 if (FPC->getType() == Type::DoubleTy)
822 Out << " const ConstantDoubleTy FloatConstant" << FPCounter++
823 << " = 0x" << std::hex << *(unsigned long long*)&Val << std::dec
824 << "; /* " << Val << " */\n";
Chris Lattner9c1338b2002-11-07 22:12:53 +0000825 else if (FPC->getType() == Type::FloatTy) {
826 float fVal = Val;
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000827 Out << " const ConstantFloatTy FloatConstant" << FPCounter++
Chris Lattner9c1338b2002-11-07 22:12:53 +0000828 << " = 0x" << std::hex << *(unsigned*)&fVal << std::dec
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000829 << "; /* " << Val << " */\n";
Chris Lattner9c1338b2002-11-07 22:12:53 +0000830 } else
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000831 assert(0 && "Unknown float type!");
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000832 }
833
834 Out << "\n";
Chris Lattner16c7bb22002-05-09 02:28:59 +0000835
836 // print the basic blocks
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000837 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
838 BasicBlock *Prev = BB->getPrev();
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000839
840 // Don't print the label for the basic block if there are no uses, or if the
841 // only terminator use is the precessor basic block's terminator. We have
842 // to scan the use list because PHI nodes use basic blocks too but do not
843 // require a label to be generated.
844 //
845 bool NeedsLabel = false;
846 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
847 UI != UE; ++UI)
848 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
Chris Lattnerf1acd962003-04-23 19:15:13 +0000849 if (TI != Prev->getTerminator() ||
850 isa<SwitchInst>(Prev->getTerminator())) {
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000851 NeedsLabel = true;
852 break;
853 }
854
855 if (NeedsLabel) Out << getValueName(BB) << ":\n";
856
857 // Output all of the instructions in the basic block...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000858 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II){
Chris Lattner84e66652003-05-03 07:11:00 +0000859 if (!isInlinableInst(*II)) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000860 if (II->getType() != Type::VoidTy)
861 outputLValue(II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000862 else
863 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000864 visit(*II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000865 Out << ";\n";
866 }
867 }
868
869 // Don't emit prefix or suffix for the terminator...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000870 visit(*BB->getTerminator());
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000871 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000872
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000873 Out << "}\n\n";
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000874 Table->purgeFunction();
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000875 FPConstantMap.clear();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000876}
877
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000878// Specific Instruction type classes... note that all of the casts are
879// neccesary because we use the instruction classes as opaque types...
880//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000881void CWriter::visitReturnInst(ReturnInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000882 // Don't output a void return if this is the last basic block in the function
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000883 if (I.getNumOperands() == 0 &&
884 &*--I.getParent()->getParent()->end() == I.getParent() &&
885 !I.getParent()->size() == 1) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000886 return;
Chris Lattner963b70b2002-05-21 18:05:19 +0000887 }
Chris Lattner44408262002-05-09 03:50:42 +0000888
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000889 Out << " return";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000890 if (I.getNumOperands()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000891 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000892 writeOperand(I.getOperand(0));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000893 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000894 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000895}
896
Chris Lattnera9f5e052003-04-22 20:19:52 +0000897void CWriter::visitSwitchInst(SwitchInst &SI) {
898 Out << " switch (";
899 writeOperand(SI.getOperand(0));
Chris Lattnerf1acd962003-04-23 19:15:13 +0000900 Out << ") {\n default:\n";
901 printBranchToBlock(SI.getParent(), SI.getDefaultDest(), 2);
Chris Lattnera9f5e052003-04-22 20:19:52 +0000902 Out << ";\n";
903 for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) {
904 Out << " case ";
905 writeOperand(SI.getOperand(i));
906 Out << ":\n";
907 BasicBlock *Succ = cast<BasicBlock>(SI.getOperand(i+1));
908 printBranchToBlock(SI.getParent(), Succ, 2);
909 if (Succ == SI.getParent()->getNext())
910 Out << " break;\n";
911 }
912 Out << " }\n";
913}
914
915
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000916static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
917 // If PHI nodes need copies, we need the copy code...
918 if (isa<PHINode>(To->front()) ||
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000919 From->getNext() != To) // Not directly successor, need goto
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000920 return true;
921
922 // Otherwise we don't need the code.
923 return false;
924}
925
926void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
927 unsigned Indent) {
928 for (BasicBlock::iterator I = Succ->begin();
Chris Lattner2ee82e02003-04-23 16:36:11 +0000929 PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000930 // now we have to do the printing
Chris Lattneredd8ce12003-05-03 03:14:35 +0000931 Out << std::string(Indent, ' ');
Chris Lattner84e66652003-05-03 07:11:00 +0000932 Out << " " << getValueName(I) << "__PHI_TEMPORARY = ";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000933 writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
934 Out << "; /* for PHI node */\n";
935 }
936
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000937 if (CurBB->getNext() != Succ) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000938 Out << std::string(Indent, ' ') << " goto ";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000939 writeOperand(Succ);
940 Out << ";\n";
941 }
942}
943
944// Brach instruction printing - Avoid printing out a brach to a basic block that
945// immediately succeeds the current one.
946//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000947void CWriter::visitBranchInst(BranchInst &I) {
948 if (I.isConditional()) {
949 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(0))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000950 Out << " if (";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000951 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000952 Out << ") {\n";
953
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000954 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000955
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000956 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(1))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000957 Out << " } else {\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000958 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000959 }
960 } else {
961 // First goto not neccesary, assume second one is...
962 Out << " if (!";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000963 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000964 Out << ") {\n";
965
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000966 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000967 }
968
969 Out << " }\n";
970 } else {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000971 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000972 }
973 Out << "\n";
974}
975
Chris Lattner84e66652003-05-03 07:11:00 +0000976// PHI nodes get copied into temporary values at the end of predecessor basic
977// blocks. We now need to copy these temporary values into the REAL value for
978// the PHI.
979void CWriter::visitPHINode(PHINode &I) {
980 writeOperand(&I);
981 Out << "__PHI_TEMPORARY";
982}
983
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000984
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000985void CWriter::visitBinaryOperator(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000986 // binary instructions, shift instructions, setCond instructions.
Chris Lattnerf5612b762003-04-23 19:09:22 +0000987 assert(!isa<PointerType>(I.getType()));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000988
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000989 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000990
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000991 switch (I.getOpcode()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000992 case Instruction::Add: Out << " + "; break;
993 case Instruction::Sub: Out << " - "; break;
994 case Instruction::Mul: Out << "*"; break;
995 case Instruction::Div: Out << "/"; break;
996 case Instruction::Rem: Out << "%"; break;
997 case Instruction::And: Out << " & "; break;
998 case Instruction::Or: Out << " | "; break;
999 case Instruction::Xor: Out << " ^ "; break;
1000 case Instruction::SetEQ: Out << " == "; break;
1001 case Instruction::SetNE: Out << " != "; break;
1002 case Instruction::SetLE: Out << " <= "; break;
1003 case Instruction::SetGE: Out << " >= "; break;
1004 case Instruction::SetLT: Out << " < "; break;
1005 case Instruction::SetGT: Out << " > "; break;
1006 case Instruction::Shl : Out << " << "; break;
1007 case Instruction::Shr : Out << " >> "; break;
Chris Lattnere7f65d3b2002-06-30 16:07:20 +00001008 default: std::cerr << "Invalid operator type!" << I; abort();
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001009 }
1010
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001011 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001012}
1013
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001014void CWriter::visitCastInst(CastInst &I) {
Chris Lattnerd13bd222003-06-01 03:36:51 +00001015 if (I.getType() == Type::BoolTy) {
1016 Out << "(";
1017 writeOperand(I.getOperand(0));
1018 Out << " != 0)";
1019 return;
1020 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001021 Out << "(";
Chris Lattneredd8ce12003-05-03 03:14:35 +00001022 printType(Out, I.getType(), "", /*ignoreName*/false, /*namedContext*/false);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001023 Out << ")";
Chris Lattnerf5612b762003-04-23 19:09:22 +00001024 if (isa<PointerType>(I.getType())&&I.getOperand(0)->getType()->isIntegral() ||
1025 isa<PointerType>(I.getOperand(0)->getType())&&I.getType()->isIntegral()) {
1026 // Avoid "cast to pointer from integer of different size" warnings
1027 Out << "(long)";
1028 }
Chris Lattnerd13bd222003-06-01 03:36:51 +00001029
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001030 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001031}
1032
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001033void CWriter::visitCallInst(CallInst &I) {
Chris Lattner18ac3c82003-05-08 18:41:45 +00001034 // Handle intrinsic function calls first...
1035 if (Function *F = I.getCalledFunction())
1036 if (LLVMIntrinsic::ID ID = (LLVMIntrinsic::ID)F->getIntrinsicID()) {
1037 switch (ID) {
1038 default: assert(0 && "Unknown LLVM intrinsic!");
1039 case LLVMIntrinsic::va_start:
1040 Out << "va_start((va_list)*";
1041 writeOperand(I.getOperand(1));
1042 Out << ", ";
1043 // Output the last argument to the enclosing function...
1044 writeOperand(&I.getParent()->getParent()->aback());
1045 Out << ")";
1046 return;
Chris Lattner18ac3c82003-05-08 18:41:45 +00001047 case LLVMIntrinsic::va_end:
1048 Out << "va_end((va_list)*";
1049 writeOperand(I.getOperand(1));
1050 Out << ")";
1051 return;
1052 case LLVMIntrinsic::va_copy:
1053 Out << "va_copy((va_list)*";
1054 writeOperand(I.getOperand(1));
1055 Out << ", (va_list)";
1056 writeOperand(I.getOperand(2));
1057 Out << ")";
1058 return;
Chris Lattnerc436b372003-05-17 22:26:33 +00001059
1060 case LLVMIntrinsic::setjmp:
1061 Out << "setjmp((jmp_buf)";
1062 writeOperand(I.getOperand(1));
1063 Out << ")";
1064 return;
1065 case LLVMIntrinsic::longjmp:
1066 Out << "longjmp((jmp_buf)";
1067 writeOperand(I.getOperand(1));
1068 Out << ", ";
1069 writeOperand(I.getOperand(2));
1070 Out << ")";
1071 return;
Chris Lattner18ac3c82003-05-08 18:41:45 +00001072 }
1073 }
1074
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001075 const PointerType *PTy = cast<PointerType>(I.getCalledValue()->getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001076 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1077 const Type *RetTy = FTy->getReturnType();
1078
Chris Lattnerfabc8802002-08-26 20:50:09 +00001079 writeOperand(I.getOperand(0));
1080 Out << "(";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001081
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001082 if (I.getNumOperands() > 1) {
1083 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001084
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001085 for (unsigned op = 2, Eop = I.getNumOperands(); op != Eop; ++op) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001086 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001087 writeOperand(I.getOperand(op));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001088 }
1089 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001090 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001091}
1092
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001093void CWriter::visitMallocInst(MallocInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001094 Out << "(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001095 printType(Out, I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001096 Out << ")malloc(sizeof(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001097 printType(Out, I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001098 Out << ")";
1099
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001100 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001101 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001102 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001103 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001104 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001105}
1106
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001107void CWriter::visitAllocaInst(AllocaInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001108 Out << "(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001109 printType(Out, I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001110 Out << ") alloca(sizeof(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001111 printType(Out, I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001112 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001113 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001114 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001115 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001116 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001117 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001118}
1119
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001120void CWriter::visitFreeInst(FreeInst &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001121 Out << "free(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001122 writeOperand(I.getOperand(0));
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001123 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001124}
1125
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001126void CWriter::printIndexingExpression(Value *Ptr, User::op_iterator I,
1127 User::op_iterator E) {
1128 bool HasImplicitAddress = false;
1129 // If accessing a global value with no indexing, avoid *(&GV) syndrome
1130 if (GlobalValue *V = dyn_cast<GlobalValue>(Ptr)) {
1131 HasImplicitAddress = true;
1132 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Ptr)) {
1133 HasImplicitAddress = true;
1134 Ptr = CPR->getValue(); // Get to the global...
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001135 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001136
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001137 if (I == E) {
1138 if (!HasImplicitAddress)
1139 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001140
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001141 writeOperandInternal(Ptr);
1142 return;
1143 }
1144
Chris Lattnerab2b3282003-05-29 15:12:27 +00001145 const Constant *CI = dyn_cast<Constant>(I);
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001146 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
1147 Out << "(&";
1148
1149 writeOperandInternal(Ptr);
1150
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001151 if (HasImplicitAddress && (!CI || !CI->isNullValue())) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001152 Out << ")";
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001153 HasImplicitAddress = false; // HIA is only true if we haven't addressed yet
1154 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001155
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001156 assert(!HasImplicitAddress || (CI && CI->isNullValue()) &&
1157 "Can only have implicit address with direct accessing");
1158
1159 if (HasImplicitAddress) {
1160 ++I;
1161 } else if (CI && CI->isNullValue() && I+1 != E) {
1162 // Print out the -> operator if possible...
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001163 if ((*(I+1))->getType() == Type::UByteTy) {
1164 Out << (HasImplicitAddress ? "." : "->");
1165 Out << "field" << cast<ConstantUInt>(*(I+1))->getValue();
1166 I += 2;
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001167 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001168 }
Chris Lattner5dfe7672002-08-22 22:48:55 +00001169
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001170 for (; I != E; ++I)
Chris Lattner106ff452002-09-11 01:21:29 +00001171 if ((*I)->getType() == Type::LongTy) {
Vikram S. Adve9d6f13f2002-09-15 21:51:04 +00001172 Out << "[";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001173 writeOperand(*I);
Vikram S. Adve9d6f13f2002-09-15 21:51:04 +00001174 Out << "]";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001175 } else {
Chris Lattner59afbf32002-09-12 20:34:47 +00001176 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001177 }
1178}
1179
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001180void CWriter::visitLoadInst(LoadInst &I) {
Nick Hildenbrandtca626922002-10-02 21:14:33 +00001181 Out << "*";
Chris Lattner5dfe7672002-08-22 22:48:55 +00001182 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001183}
1184
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001185void CWriter::visitStoreInst(StoreInst &I) {
Nick Hildenbrandtca626922002-10-02 21:14:33 +00001186 Out << "*";
Chris Lattner5dfe7672002-08-22 22:48:55 +00001187 writeOperand(I.getPointerOperand());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001188 Out << " = ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001189 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001190}
1191
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001192void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
Nick Hildenbrandtca626922002-10-02 21:14:33 +00001193 Out << "&";
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001194 printIndexingExpression(I.getPointerOperand(), I.idx_begin(), I.idx_end());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001195}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001196
Chris Lattner18ac3c82003-05-08 18:41:45 +00001197void CWriter::visitVarArgInst(VarArgInst &I) {
1198 Out << "va_arg((va_list)*";
1199 writeOperand(I.getOperand(0));
1200 Out << ", ";
1201 printType(Out, I.getType(), "", /*ignoreName*/false, /*namedContext*/false);
1202 Out << ")";
1203}
1204
1205
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001206//===----------------------------------------------------------------------===//
1207// External Interface declaration
1208//===----------------------------------------------------------------------===//
1209
Chris Lattner0e9f93e2002-08-31 00:29:16 +00001210Pass *createWriteToCPass(std::ostream &o) { return new CWriter(o); }