blob: 4c5fd07207ded2403bd95b072b254ed6e35452de [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//===----------------------------------------------------------------------===//
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00006#include "llvm/Assembly/CWriter.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00007#include "llvm/Constants.h"
Chris Lattner2f5f51a2002-05-09 03:28:37 +00008#include "llvm/DerivedTypes.h"
9#include "llvm/Module.h"
Chris Lattnera9f5e052003-04-22 20:19:52 +000010#include "llvm/Instructions.h"
Chris Lattner0e9f93e2002-08-31 00:29:16 +000011#include "llvm/Pass.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000012#include "llvm/SymbolTable.h"
Chris Lattner3af3ba82002-05-09 21:31:18 +000013#include "llvm/SlotCalculator.h"
Chris Lattner0e9f93e2002-08-31 00:29:16 +000014#include "llvm/Analysis/FindUsedTypes.h"
Chris Lattnerd1cf1b42002-09-20 23:26:33 +000015#include "llvm/Analysis/ConstantsScanner.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000016#include "llvm/Support/InstVisitor.h"
Chris Lattner7683a122002-05-09 20:33:35 +000017#include "llvm/Support/InstIterator.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000018#include "Support/StringExtras.h"
19#include "Support/STLExtras.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000020#include <algorithm>
Chris Lattner78771c82002-05-17 04:55:35 +000021#include <set>
Nick Hildenbrandt98502372002-11-18 20:55:50 +000022#include <sstream>
Nick Hildenbrandt3cecdc52002-10-23 18:59:40 +000023
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000024namespace {
Chris Lattner0e9f93e2002-08-31 00:29:16 +000025 class CWriter : public Pass, public InstVisitor<CWriter> {
Chris Lattneredd8ce12003-05-03 03:14:35 +000026 std::ostream &Out;
Chris Lattner0e9f93e2002-08-31 00:29:16 +000027 SlotCalculator *Table;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000028 const Module *TheModule;
Chris Lattneredd8ce12003-05-03 03:14:35 +000029 std::map<const Type *, std::string> TypeNames;
Chris Lattner78771c82002-05-17 04:55:35 +000030 std::set<const Value*> MangledGlobals;
Nick Hildenbrandt3cecdc52002-10-23 18:59:40 +000031 bool needsMalloc;
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +000032
Chris Lattneredd8ce12003-05-03 03:14:35 +000033 std::map<const ConstantFP *, unsigned> FPConstantMap;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000034 public:
Chris Lattneredd8ce12003-05-03 03:14:35 +000035 CWriter(std::ostream &o) : Out(o) {}
Chris Lattner0e9f93e2002-08-31 00:29:16 +000036
37 void getAnalysisUsage(AnalysisUsage &AU) const {
38 AU.setPreservesAll();
39 AU.addRequired<FindUsedTypes>();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000040 }
Chris Lattner0e9f93e2002-08-31 00:29:16 +000041
42 virtual bool run(Module &M) {
43 // Initialize
44 Table = new SlotCalculator(&M, false);
45 TheModule = &M;
46
47 // Ensure that all structure types have names...
48 bool Changed = nameAllUsedStructureTypes(M);
49
50 // Run...
51 printModule(&M);
52
53 // Free memory...
54 delete Table;
55 TypeNames.clear();
56 MangledGlobals.clear();
57 return false;
58 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000059
Chris Lattneredd8ce12003-05-03 03:14:35 +000060 std::ostream &printType(std::ostream &Out, const Type *Ty,
61 const std::string &VariableName = "",
62 bool IgnoreName = false, bool namedContext = true);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000063
Chris Lattnerbb03efd2002-06-25 15:57:03 +000064 void writeOperand(Value *Operand);
65 void writeOperandInternal(Value *Operand);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000066
Chris Lattneredd8ce12003-05-03 03:14:35 +000067 std::string getValueName(const Value *V);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000068
Chris Lattner4fbf26d2002-05-09 20:53:56 +000069 private :
Chris Lattner0e9f93e2002-08-31 00:29:16 +000070 bool nameAllUsedStructureTypes(Module &M);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +000071 void printModule(Module *M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000072 void printSymbolTable(const SymbolTable &ST);
Chris Lattner2c601a72002-09-20 15:05:40 +000073 void printContainedStructs(const Type *Ty, std::set<const StructType *> &);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000074 void printGlobal(const GlobalVariable *GV);
Chris Lattner4cda8352002-08-20 16:55:48 +000075 void printFunctionSignature(const Function *F, bool Prototype);
76
Chris Lattner8c3c4bf2002-05-09 15:49:41 +000077 void printFunction(Function *);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000078
Chris Lattner6d492922002-08-19 21:32:41 +000079 void printConstant(Constant *CPV);
80 void printConstantArray(ConstantArray *CPA);
81
Chris Lattnerd0c668c2002-05-09 21:18:38 +000082 // isInlinableInst - Attempt to inline instructions into their uses to build
83 // trees as much as possible. To do this, we have to consistently decide
84 // what is acceptable to inline, so that variable declarations don't get
85 // printed and an extra copy of the expr is not emitted.
86 //
Chris Lattnerbb03efd2002-06-25 15:57:03 +000087 static bool isInlinableInst(const Instruction &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +000088 // Must be an expression, must be used exactly once. If it is dead, we
89 // emit it inline where it would go.
Chris Lattnerbb03efd2002-06-25 15:57:03 +000090 if (I.getType() == Type::VoidTy || I.use_size() != 1 ||
Nick Hildenbrandt2cf2cbc2002-11-06 20:07:54 +000091 isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I) ||
92 isa<LoadInst>(I)) // Don't inline a load across a store!
Chris Lattnerd0c668c2002-05-09 21:18:38 +000093 return false;
94
95 // Only inline instruction it it's use is in the same BB as the inst.
Chris Lattnerbb03efd2002-06-25 15:57:03 +000096 return I.getParent() == cast<Instruction>(I.use_back())->getParent();
Chris Lattnerd0c668c2002-05-09 21:18:38 +000097 }
98
Chris Lattner4fbf26d2002-05-09 20:53:56 +000099 // Instruction visitation functions
100 friend class InstVisitor<CWriter>;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000101
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000102 void visitReturnInst(ReturnInst &I);
103 void visitBranchInst(BranchInst &I);
Chris Lattnera9f5e052003-04-22 20:19:52 +0000104 void visitSwitchInst(SwitchInst &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000105
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000106 void visitPHINode(PHINode &I) {}
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000107 void visitBinaryOperator(Instruction &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000108
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000109 void visitCastInst (CastInst &I);
110 void visitCallInst (CallInst &I);
111 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000112
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000113 void visitMallocInst(MallocInst &I);
114 void visitAllocaInst(AllocaInst &I);
115 void visitFreeInst (FreeInst &I);
116 void visitLoadInst (LoadInst &I);
117 void visitStoreInst (StoreInst &I);
118 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000119
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000120 void visitInstruction(Instruction &I) {
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000121 std::cerr << "C Writer does not know about " << I;
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000122 abort();
123 }
124
125 void outputLValue(Instruction *I) {
126 Out << " " << getValueName(I) << " = ";
127 }
128 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
129 unsigned Indent);
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000130 void printIndexingExpression(Value *Ptr, User::op_iterator I,
131 User::op_iterator E);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000132 };
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000133}
134
Chris Lattner2f499022002-05-09 04:21:21 +0000135// We dont want identifier names with ., space, - in them.
136// So we replace them with _
Chris Lattneredd8ce12003-05-03 03:14:35 +0000137static std::string makeNameProper(std::string x) {
138 std::string tmp;
139 for (std::string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
Chris Lattner2f499022002-05-09 04:21:21 +0000140 switch (*sI) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000141 case '.': tmp += "d_"; break;
142 case ' ': tmp += "s_"; break;
143 case '-': tmp += "D_"; break;
Chris Lattner2f499022002-05-09 04:21:21 +0000144 default: tmp += *sI;
145 }
146
147 return tmp;
148}
149
Chris Lattneredd8ce12003-05-03 03:14:35 +0000150std::string CWriter::getValueName(const Value *V) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000151 if (V->hasName()) { // Print out the label if it exists...
152 if (isa<GlobalValue>(V) && // Do not mangle globals...
Chris Lattneredd8ce12003-05-03 03:14:35 +0000153 (cast<GlobalValue>(V)->hasExternalLinkage() &&// Unless it's internal or
154 !MangledGlobals.count(V))) // Unless the name would collide if we don't
Chris Lattner2f499022002-05-09 04:21:21 +0000155 return makeNameProper(V->getName());
156
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000157 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
158 makeNameProper(V->getName());
Chris Lattner2f499022002-05-09 04:21:21 +0000159 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000160
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000161 int Slot = Table->getValSlot(V);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000162 assert(Slot >= 0 && "Invalid value!");
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000163 return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000164}
165
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000166// A pointer type should not use parens around *'s alone, e.g., (**)
Chris Lattneredd8ce12003-05-03 03:14:35 +0000167inline bool ptrTypeNameNeedsParens(const std::string &NameSoFar) {
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000168 return (NameSoFar.find_last_not_of('*') != std::string::npos);
169}
170
Chris Lattner83c57752002-08-19 22:17:53 +0000171// Pass the Type* and the variable name and this prints out the variable
172// declaration.
173//
Chris Lattneredd8ce12003-05-03 03:14:35 +0000174std::ostream &CWriter::printType(std::ostream &Out, const Type *Ty,
175 const std::string &NameSoFar,
176 bool IgnoreName, bool namedContext) {
Chris Lattner83c57752002-08-19 22:17:53 +0000177 if (Ty->isPrimitiveType())
178 switch (Ty->getPrimitiveID()) {
179 case Type::VoidTyID: return Out << "void " << NameSoFar;
180 case Type::BoolTyID: return Out << "bool " << NameSoFar;
181 case Type::UByteTyID: return Out << "unsigned char " << NameSoFar;
182 case Type::SByteTyID: return Out << "signed char " << NameSoFar;
183 case Type::UShortTyID: return Out << "unsigned short " << NameSoFar;
184 case Type::ShortTyID: return Out << "short " << NameSoFar;
185 case Type::UIntTyID: return Out << "unsigned " << NameSoFar;
186 case Type::IntTyID: return Out << "int " << NameSoFar;
187 case Type::ULongTyID: return Out << "unsigned long long " << NameSoFar;
188 case Type::LongTyID: return Out << "signed long long " << NameSoFar;
189 case Type::FloatTyID: return Out << "float " << NameSoFar;
190 case Type::DoubleTyID: return Out << "double " << NameSoFar;
191 default :
192 std::cerr << "Unknown primitive type: " << Ty << "\n";
193 abort();
194 }
195
196 // Check to see if the type is named.
Chris Lattner04b72c82002-10-16 00:08:22 +0000197 if (!IgnoreName || isa<OpaqueType>(Ty)) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000198 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner83c57752002-08-19 22:17:53 +0000199 if (I != TypeNames.end()) {
200 return Out << I->second << " " << NameSoFar;
201 }
Chris Lattner04b72c82002-10-16 00:08:22 +0000202 }
Chris Lattner83c57752002-08-19 22:17:53 +0000203
Chris Lattner83c57752002-08-19 22:17:53 +0000204 switch (Ty->getPrimitiveID()) {
205 case Type::FunctionTyID: {
206 const FunctionType *MTy = cast<FunctionType>(Ty);
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000207 std::stringstream FunctionInards;
208 FunctionInards << " (" << NameSoFar << ") (";
Chris Lattner83c57752002-08-19 22:17:53 +0000209 for (FunctionType::ParamTypes::const_iterator
210 I = MTy->getParamTypes().begin(),
211 E = MTy->getParamTypes().end(); I != E; ++I) {
212 if (I != MTy->getParamTypes().begin())
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000213 FunctionInards << ", ";
214 printType(FunctionInards, *I, "");
Chris Lattner83c57752002-08-19 22:17:53 +0000215 }
216 if (MTy->isVarArg()) {
217 if (!MTy->getParamTypes().empty())
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000218 FunctionInards << ", ";
219 FunctionInards << "...";
Chris Lattner83c57752002-08-19 22:17:53 +0000220 }
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000221 FunctionInards << ")";
Chris Lattneredd8ce12003-05-03 03:14:35 +0000222 std::string tstr = FunctionInards.str();
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000223 printType(Out, MTy->getReturnType(), tstr);
224 return Out;
Chris Lattner83c57752002-08-19 22:17:53 +0000225 }
226 case Type::StructTyID: {
227 const StructType *STy = cast<StructType>(Ty);
228 Out << NameSoFar + " {\n";
229 unsigned Idx = 0;
230 for (StructType::ElementTypes::const_iterator
231 I = STy->getElementTypes().begin(),
232 E = STy->getElementTypes().end(); I != E; ++I) {
233 Out << " ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000234 printType(Out, *I, "field" + utostr(Idx++));
Chris Lattner83c57752002-08-19 22:17:53 +0000235 Out << ";\n";
236 }
237 return Out << "}";
238 }
239
240 case Type::PointerTyID: {
241 const PointerType *PTy = cast<PointerType>(Ty);
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000242 std::string ptrName = "*" + NameSoFar;
243
244 // Do not need parens around "* NameSoFar" if NameSoFar consists only
245 // of zero or more '*' chars *and* this is not an unnamed pointer type
246 // such as the result type in a cast statement. Otherwise, enclose in ( ).
Nick Hildenbrandtc14ded42002-09-23 21:02:50 +0000247 if (ptrTypeNameNeedsParens(NameSoFar) || !namedContext ||
248 PTy->getElementType()->getPrimitiveID() == Type::ArrayTyID)
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000249 ptrName = "(" + ptrName + ")"; //
250
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000251 return printType(Out, PTy->getElementType(), ptrName);
252 }Out <<"--";
Chris Lattner83c57752002-08-19 22:17:53 +0000253
254 case Type::ArrayTyID: {
255 const ArrayType *ATy = cast<ArrayType>(Ty);
256 unsigned NumElements = ATy->getNumElements();
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000257 return printType(Out, ATy->getElementType(),
Chris Lattner83c57752002-08-19 22:17:53 +0000258 NameSoFar + "[" + utostr(NumElements) + "]");
259 }
Chris Lattner04b72c82002-10-16 00:08:22 +0000260
261 case Type::OpaqueTyID: {
262 static int Count = 0;
Chris Lattneredd8ce12003-05-03 03:14:35 +0000263 std::string TyName = "struct opaque_" + itostr(Count++);
Chris Lattner04b72c82002-10-16 00:08:22 +0000264 assert(TypeNames.find(Ty) == TypeNames.end());
265 TypeNames[Ty] = TyName;
266 return Out << TyName << " " << NameSoFar;
267 }
Chris Lattner83c57752002-08-19 22:17:53 +0000268 default:
269 assert(0 && "Unhandled case in getTypeProps!");
270 abort();
271 }
272
273 return Out;
274}
275
Chris Lattner6d492922002-08-19 21:32:41 +0000276void CWriter::printConstantArray(ConstantArray *CPA) {
277
278 // As a special case, print the array as a string if it is an array of
279 // ubytes or an array of sbytes with positive values.
280 //
281 const Type *ETy = CPA->getType()->getElementType();
282 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
283
284 // Make sure the last character is a null char, as automatically added by C
285 if (CPA->getNumOperands() == 0 ||
286 !cast<Constant>(*(CPA->op_end()-1))->isNullValue())
287 isString = false;
288
289 if (isString) {
290 Out << "\"";
291 // Do not include the last character, which we know is null
292 for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) {
293 unsigned char C = (ETy == Type::SByteTy) ?
294 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
295 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
296
297 if (isprint(C)) {
Nick Hildenbrandt088b4722002-11-06 21:40:23 +0000298 if (C == '"' || C == '\\')
299 Out << "\\" << C;
Nick Hildenbrandtc3dd2af2002-09-30 21:11:55 +0000300 else
301 Out << C;
Chris Lattner6d492922002-08-19 21:32:41 +0000302 } else {
303 switch (C) {
304 case '\n': Out << "\\n"; break;
305 case '\t': Out << "\\t"; break;
306 case '\r': Out << "\\r"; break;
307 case '\v': Out << "\\v"; break;
308 case '\a': Out << "\\a"; break;
Nick Hildenbrandtc3dd2af2002-09-30 21:11:55 +0000309 case '\"': Out << "\\\""; break;
310 case '\'': Out << "\\\'"; break;
Chris Lattner6d492922002-08-19 21:32:41 +0000311 default:
312 Out << "\\x";
313 Out << ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
314 Out << ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
315 break;
316 }
317 }
318 }
319 Out << "\"";
320 } else {
321 Out << "{";
322 if (CPA->getNumOperands()) {
323 Out << " ";
324 printConstant(cast<Constant>(CPA->getOperand(0)));
325 for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
326 Out << ", ";
327 printConstant(cast<Constant>(CPA->getOperand(i)));
328 }
329 }
330 Out << " }";
331 }
332}
333
334
335// printConstant - The LLVM Constant to C Constant converter.
336void CWriter::printConstant(Constant *CPV) {
337 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
338 switch (CE->getOpcode()) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000339 case Instruction::Cast:
340 Out << "((";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000341 printType(Out, CPV->getType());
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000342 Out << ")";
343 printConstant(cast<Constant>(CPV->getOperand(0)));
344 Out << ")";
345 return;
346
347 case Instruction::GetElementPtr:
Nick Hildenbrandtd2eb3862002-10-03 20:47:24 +0000348 Out << "(&(";
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000349 printIndexingExpression(CPV->getOperand(0),
350 CPV->op_begin()+1, CPV->op_end());
Nick Hildenbrandtd2eb3862002-10-03 20:47:24 +0000351 Out << "))";
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000352 return;
353 case Instruction::Add:
354 Out << "(";
355 printConstant(cast<Constant>(CPV->getOperand(0)));
356 Out << " + ";
357 printConstant(cast<Constant>(CPV->getOperand(1)));
358 Out << ")";
359 return;
360 case Instruction::Sub:
361 Out << "(";
362 printConstant(cast<Constant>(CPV->getOperand(0)));
363 Out << " - ";
364 printConstant(cast<Constant>(CPV->getOperand(1)));
365 Out << ")";
366 return;
367
Chris Lattner6d492922002-08-19 21:32:41 +0000368 default:
369 std::cerr << "CWriter Error: Unhandled constant expression: "
370 << CE << "\n";
371 abort();
372 }
373 }
374
375 switch (CPV->getType()->getPrimitiveID()) {
376 case Type::BoolTyID:
377 Out << (CPV == ConstantBool::False ? "0" : "1"); break;
378 case Type::SByteTyID:
379 case Type::ShortTyID:
380 case Type::IntTyID:
381 Out << cast<ConstantSInt>(CPV)->getValue(); break;
382 case Type::LongTyID:
383 Out << cast<ConstantSInt>(CPV)->getValue() << "ll"; break;
384
385 case Type::UByteTyID:
386 case Type::UShortTyID:
387 Out << cast<ConstantUInt>(CPV)->getValue(); break;
388 case Type::UIntTyID:
389 Out << cast<ConstantUInt>(CPV)->getValue() << "u"; break;
390 case Type::ULongTyID:
391 Out << cast<ConstantUInt>(CPV)->getValue() << "ull"; break;
392
393 case Type::FloatTyID:
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000394 case Type::DoubleTyID: {
395 ConstantFP *FPC = cast<ConstantFP>(CPV);
Chris Lattneredd8ce12003-05-03 03:14:35 +0000396 std::map<const ConstantFP*, unsigned>::iterator I = FPConstantMap.find(FPC);
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000397 if (I != FPConstantMap.end()) {
398 // Because of FP precision problems we must load from a stack allocated
399 // value that holds the value in hex.
400 Out << "(*(" << (FPC->getType() == Type::FloatTy ? "float" : "double")
401 << "*)&FloatConstant" << I->second << ")";
402 } else {
403 Out << FPC->getValue();
404 }
405 break;
406 }
Chris Lattner6d492922002-08-19 21:32:41 +0000407
408 case Type::ArrayTyID:
409 printConstantArray(cast<ConstantArray>(CPV));
410 break;
411
412 case Type::StructTyID: {
413 Out << "{";
414 if (CPV->getNumOperands()) {
415 Out << " ";
416 printConstant(cast<Constant>(CPV->getOperand(0)));
417 for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) {
418 Out << ", ";
419 printConstant(cast<Constant>(CPV->getOperand(i)));
420 }
421 }
422 Out << " }";
423 break;
424 }
425
426 case Type::PointerTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000427 if (isa<ConstantPointerNull>(CPV)) {
Nick Hildenbrandtf0fca362002-10-28 19:54:06 +0000428 Out << "(NULL)";
Chris Lattner6d492922002-08-19 21:32:41 +0000429 break;
430 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
431 writeOperand(CPR->getValue());
432 break;
433 }
434 // FALL THROUGH
435 default:
436 std::cerr << "Unknown constant type: " << CPV << "\n";
437 abort();
438 }
439}
440
441void CWriter::writeOperandInternal(Value *Operand) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000442 if (Instruction *I = dyn_cast<Instruction>(Operand))
443 if (isInlinableInst(*I)) {
444 // Should we inline this instruction to build a tree?
445 Out << "(";
446 visit(*I);
447 Out << ")";
448 return;
449 }
450
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +0000451 if (Operand->hasName()) {
Chris Lattner6d492922002-08-19 21:32:41 +0000452 Out << getValueName(Operand);
453 } else if (Constant *CPV = dyn_cast<Constant>(Operand)) {
454 printConstant(CPV);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000455 } else {
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000456 int Slot = Table->getValSlot(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000457 assert(Slot >= 0 && "Malformed LLVM!");
458 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
459 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000460}
461
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000462void CWriter::writeOperand(Value *Operand) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000463 if (isa<GlobalVariable>(Operand))
464 Out << "(&"; // Global variables are references as their addresses by llvm
465
466 writeOperandInternal(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000467
468 if (isa<GlobalVariable>(Operand))
469 Out << ")";
470}
471
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000472// nameAllUsedStructureTypes - If there are structure types in the module that
473// are used but do not have names assigned to them in the symbol table yet then
474// we assign them names now.
475//
476bool CWriter::nameAllUsedStructureTypes(Module &M) {
477 // Get a set of types that are used by the program...
478 std::set<const Type *> UT = getAnalysis<FindUsedTypes>().getTypes();
479
480 // Loop over the module symbol table, removing types from UT that are already
481 // named.
482 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000483 SymbolTable &MST = M.getSymbolTable();
484 if (MST.find(Type::TypeTy) != MST.end())
485 for (SymbolTable::type_iterator I = MST.type_begin(Type::TypeTy),
486 E = MST.type_end(Type::TypeTy); I != E; ++I)
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000487 UT.erase(cast<Type>(I->second));
488
489 // UT now contains types that are not named. Loop over it, naming structure
490 // types.
491 //
492 bool Changed = false;
493 for (std::set<const Type *>::const_iterator I = UT.begin(), E = UT.end();
494 I != E; ++I)
495 if (const StructType *ST = dyn_cast<StructType>(*I)) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000496 ((Value*)ST)->setName("unnamed", &MST);
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000497 Changed = true;
498 }
499 return Changed;
500}
501
Chris Lattneredd8ce12003-05-03 03:14:35 +0000502static void generateAllocaDecl(std::ostream& Out) {
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000503 // On SunOS, we need to insert the alloca macro & proto for the builtin.
504 Out << "#ifdef sun\n"
505 << "extern void *__builtin_alloca(unsigned long);\n"
506 << "#define alloca(x) __builtin_alloca(x)\n"
507 << "#else\n"
508 << "#include <alloca.h>\n"
509 << "#endif\n\n";
510}
511
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000512void CWriter::printModule(Module *M) {
Chris Lattner78771c82002-05-17 04:55:35 +0000513 // Calculate which global values have names that will collide when we throw
514 // away type information.
Chris Lattner594b9fa2002-05-21 21:10:04 +0000515 { // Scope to delete the FoundNames set when we are done with it...
Chris Lattneredd8ce12003-05-03 03:14:35 +0000516 std::set<std::string> FoundNames;
Chris Lattner78771c82002-05-17 04:55:35 +0000517 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000518 if (I->hasName()) // If the global has a name...
519 if (FoundNames.count(I->getName())) // And the name is already used
520 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000521 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000522 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000523
524 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000525 if (I->hasName()) // If the global has a name...
526 if (FoundNames.count(I->getName())) // And the name is already used
527 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000528 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000529 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000530 }
531
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000532 // printing stdlib inclusion
Nick Hildenbrandt98360a12002-10-11 21:40:44 +0000533 //Out << "#include <stdlib.h>\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000534
Chris Lattner16c7bb22002-05-09 02:28:59 +0000535 // get declaration for alloca
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000536 Out << "/* Provide Declarations */\n";
537 generateAllocaDecl(Out);
538
539 // Provide a definition for null if one does not already exist,
540 // and for `bool' if not compiling with a C++ compiler.
541 Out << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000542 << "#ifndef __cplusplus\ntypedef unsigned char bool;\n#endif\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000543
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000544 << "\n\n/* Support for floating point constants */\n"
545 << "typedef unsigned long long ConstantDoubleTy;\n"
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000546 << "typedef unsigned int ConstantFloatTy;\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000547
Chris Lattnera4d4a852002-08-19 21:48:40 +0000548 << "\n\n/* Global Declarations */\n";
549
550 // First output all the declarations for the program, because C requires
551 // Functions & globals to be declared before they are used.
552 //
Chris Lattner16c7bb22002-05-09 02:28:59 +0000553
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000554 // Loop over the symbol table, emitting all named constants...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000555 printSymbolTable(M->getSymbolTable());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000556
Chris Lattnera4d4a852002-08-19 21:48:40 +0000557 // Global variable declarations...
558 if (!M->gempty()) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000559 Out << "\n/* External Global Variable Declarations */\n";
Chris Lattnera4d4a852002-08-19 21:48:40 +0000560 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000561 if (I->hasExternalLinkage()) {
562 Out << "extern ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000563 printType(Out, I->getType()->getElementType(), getValueName(I));
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000564 Out << ";\n";
565 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000566 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000567 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000568
Chris Lattnera4d4a852002-08-19 21:48:40 +0000569 // Function declarations
570 if (!M->empty()) {
571 Out << "\n/* Function Declarations */\n";
Nick Hildenbrandt3cecdc52002-10-23 18:59:40 +0000572 needsMalloc = true;
Chris Lattner4cda8352002-08-20 16:55:48 +0000573 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
Nick Hildenbrandta1a64f82002-11-18 22:21:52 +0000574 // If the function is external and the name collides don't print it.
Chris Lattneredd8ce12003-05-03 03:14:35 +0000575 // Sometimes the bytecode likes to have multiple "declerations" for
576 // external functions
Nick Hildenbrandta1a64f82002-11-18 22:21:52 +0000577 if (I->hasInternalLinkage() || !MangledGlobals.count(I)){
578 printFunctionSignature(I, true);
579 Out << ";\n";
580 }
Chris Lattner4cda8352002-08-20 16:55:48 +0000581 }
Chris Lattnera4d4a852002-08-19 21:48:40 +0000582 }
583
Nick Hildenbrandt3cecdc52002-10-23 18:59:40 +0000584 // Print Malloc prototype if needed
585 if (needsMalloc){
586 Out << "\n/* Malloc to make sun happy */\n";
587 Out << "extern void * malloc(size_t);\n\n";
588 }
589
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +0000590 // Output the global variable declerations
591 if (!M->gempty()) {
592 Out << "\n\n/* Global Variable Declerations */\n";
593 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
594 if (!I->isExternal()) {
595 Out << "extern ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000596 printType(Out, I->getType()->getElementType(), getValueName(I));
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +0000597
598 Out << ";\n";
599 }
600 }
601
602
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000603 // Output the global variable definitions and contents...
Chris Lattnera4d4a852002-08-19 21:48:40 +0000604 if (!M->gempty()) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000605 Out << "\n\n/* Global Variable Definitions and Initialization */\n";
Chris Lattnere8e035b2002-10-16 20:08:47 +0000606 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
607 if (!I->isExternal()) {
608 if (I->hasInternalLinkage())
609 Out << "static ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000610 printType(Out, I->getType()->getElementType(), getValueName(I));
Chris Lattnera4d4a852002-08-19 21:48:40 +0000611
Chris Lattnera4d4a852002-08-19 21:48:40 +0000612 Out << " = " ;
613 writeOperand(I->getInitializer());
Chris Lattnere8e035b2002-10-16 20:08:47 +0000614 Out << ";\n";
Chris Lattnera4d4a852002-08-19 21:48:40 +0000615 }
Chris Lattnera4d4a852002-08-19 21:48:40 +0000616 }
617
Chris Lattner16c7bb22002-05-09 02:28:59 +0000618 // Output all of the functions...
Chris Lattnera4d4a852002-08-19 21:48:40 +0000619 if (!M->empty()) {
620 Out << "\n\n/* Function Bodies */\n";
621 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
622 printFunction(I);
623 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000624}
625
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000626
Chris Lattner2db41cd2002-09-20 15:12:13 +0000627/// printSymbolTable - Run through symbol table looking for type names. If a
628/// type name is found, emit it's declaration...
Chris Lattnera4c047e2002-09-20 14:56:54 +0000629///
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000630void CWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattner2db41cd2002-09-20 15:12:13 +0000631 // If there are no type names, exit early.
632 if (ST.find(Type::TypeTy) == ST.end())
633 return;
634
635 // We are only interested in the type plane of the symbol table...
636 SymbolTable::type_const_iterator I = ST.type_begin(Type::TypeTy);
637 SymbolTable::type_const_iterator End = ST.type_end(Type::TypeTy);
638
Chris Lattner58d04d42002-09-20 15:18:30 +0000639 // Print out forward declarations for structure types before anything else!
640 Out << "/* Structure forward decls */\n";
641 for (; I != End; ++I)
642 if (const Type *STy = dyn_cast<StructType>(I->second)) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000643 std::string Name = "struct l_" + makeNameProper(I->first);
Chris Lattner58d04d42002-09-20 15:18:30 +0000644 Out << Name << ";\n";
645 TypeNames.insert(std::make_pair(STy, Name));
646 }
647
648 Out << "\n";
649
650 // Now we can print out typedefs...
651 Out << "/* Typedefs */\n";
652 for (I = ST.type_begin(Type::TypeTy); I != End; ++I) {
653 const Type *Ty = cast<Type>(I->second);
Chris Lattneredd8ce12003-05-03 03:14:35 +0000654 std::string Name = "l_" + makeNameProper(I->first);
Chris Lattner58d04d42002-09-20 15:18:30 +0000655 Out << "typedef ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000656 printType(Out, Ty, Name);
Chris Lattner58d04d42002-09-20 15:18:30 +0000657 Out << ";\n";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000658 }
659
660 Out << "\n";
661
Chris Lattner2c601a72002-09-20 15:05:40 +0000662 // Keep track of which structures have been printed so far...
663 std::set<const StructType *> StructPrinted;
664
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000665 // Loop over all structures then push them into the stack so they are
666 // printed in the correct order.
Chris Lattner2db41cd2002-09-20 15:12:13 +0000667 //
Chris Lattner58d04d42002-09-20 15:18:30 +0000668 Out << "/* Structure contents */\n";
Chris Lattner2db41cd2002-09-20 15:12:13 +0000669 for (I = ST.type_begin(Type::TypeTy); I != End; ++I)
670 if (const StructType *STy = dyn_cast<StructType>(I->second))
671 printContainedStructs(STy, StructPrinted);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000672}
673
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000674// Push the struct onto the stack and recursively push all structs
675// this one depends on.
Chris Lattner2c601a72002-09-20 15:05:40 +0000676void CWriter::printContainedStructs(const Type *Ty,
677 std::set<const StructType*> &StructPrinted){
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000678 if (const StructType *STy = dyn_cast<StructType>(Ty)){
679 //Check to see if we have already printed this struct
Chris Lattner2c601a72002-09-20 15:05:40 +0000680 if (StructPrinted.count(STy) == 0) {
681 // Print all contained types first...
Chris Lattnera4c047e2002-09-20 14:56:54 +0000682 for (StructType::ElementTypes::const_iterator
683 I = STy->getElementTypes().begin(),
684 E = STy->getElementTypes().end(); I != E; ++I) {
Chris Lattner2c601a72002-09-20 15:05:40 +0000685 const Type *Ty1 = I->get();
Chris Lattnera4c047e2002-09-20 14:56:54 +0000686 if (isa<StructType>(Ty1) || isa<ArrayType>(Ty1))
Chris Lattner2c601a72002-09-20 15:05:40 +0000687 printContainedStructs(Ty1, StructPrinted);
Chris Lattnera4c047e2002-09-20 14:56:54 +0000688 }
689
Chris Lattner2c601a72002-09-20 15:05:40 +0000690 //Print structure type out..
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000691 StructPrinted.insert(STy);
Chris Lattneredd8ce12003-05-03 03:14:35 +0000692 std::string Name = TypeNames[STy];
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000693 printType(Out, STy, Name, true);
Chris Lattner58d04d42002-09-20 15:18:30 +0000694 Out << ";\n\n";
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000695 }
Chris Lattnera4c047e2002-09-20 14:56:54 +0000696
Chris Lattner2c601a72002-09-20 15:05:40 +0000697 // If it is an array, check contained types and continue
698 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)){
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000699 const Type *Ty1 = ATy->getElementType();
700 if (isa<StructType>(Ty1) || isa<ArrayType>(Ty1))
Chris Lattner2c601a72002-09-20 15:05:40 +0000701 printContainedStructs(Ty1, StructPrinted);
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000702 }
703}
704
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000705
Chris Lattner4cda8352002-08-20 16:55:48 +0000706void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
Nick Hildenbrandt3cecdc52002-10-23 18:59:40 +0000707 // If the program provides it's own malloc prototype we don't need
708 // to include the general one.
709 if (getValueName(F) == "malloc")
710 needsMalloc = false;
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000711 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000712 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000713 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000714
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000715 std::stringstream FunctionInards;
716
717 // Print out the name...
718 FunctionInards << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000719
Chris Lattner16c7bb22002-05-09 02:28:59 +0000720 if (!F->isExternal()) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000721 if (!F->aempty()) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000722 std::string ArgName;
Chris Lattner4cda8352002-08-20 16:55:48 +0000723 if (F->abegin()->hasName() || !Prototype)
724 ArgName = getValueName(F->abegin());
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000725 printType(FunctionInards, F->afront().getType(), ArgName);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000726 for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
727 I != E; ++I) {
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000728 FunctionInards << ", ";
Chris Lattner4cda8352002-08-20 16:55:48 +0000729 if (I->hasName() || !Prototype)
730 ArgName = getValueName(I);
731 else
732 ArgName = "";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000733 printType(FunctionInards, I->getType(), ArgName);
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000734 }
735 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000736 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000737 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000738 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000739 FT->getParamTypes().begin(),
740 E = FT->getParamTypes().end(); I != E; ++I) {
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000741 if (I != FT->getParamTypes().begin()) FunctionInards << ", ";
742 printType(FunctionInards, *I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000743 }
744 }
745
Chris Lattner1f8c4a12002-09-20 22:32:30 +0000746 // Finish printing arguments... if this is a vararg function, print the ...,
747 // unless there are no known types, in which case, we just emit ().
748 //
749 if (FT->isVarArg() && !FT->getParamTypes().empty()) {
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000750 if (FT->getParamTypes().size()) FunctionInards << ", ";
751 FunctionInards << "..."; // Output varargs portion of signature!
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000752 }
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000753 FunctionInards << ")";
754 // Print out the return type and the entire signature for that matter
755 printType(Out, F->getReturnType(), FunctionInards.str());
756
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000757}
758
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000759
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000760void CWriter::printFunction(Function *F) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000761 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000762
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000763 Table->incorporateFunction(F);
Chris Lattnerf34ee812002-05-09 03:12:34 +0000764
Chris Lattner4cda8352002-08-20 16:55:48 +0000765 printFunctionSignature(F, false);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000766 Out << " {\n";
767
Chris Lattner497e19a2002-05-09 20:39:03 +0000768 // print local variable information for the function
Chris Lattner7683a122002-05-09 20:33:35 +0000769 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000770 if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000771 Out << " ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000772 printType(Out, (*I)->getType(), getValueName(*I));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000773 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000774 }
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000775
776 Out << "\n";
777
778 // Scan the function for floating point constants. If any FP constant is used
779 // in the function, we want to redirect it here so that we do not depend on
780 // the precision of the printed form.
781 //
782 unsigned FPCounter = 0;
783 for (constant_iterator I = constant_begin(F), E = constant_end(F); I != E;++I)
784 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(*I))
785 if (FPConstantMap.find(FPC) == FPConstantMap.end()) {
786 double Val = FPC->getValue();
787
788 FPConstantMap[FPC] = FPCounter; // Number the FP constants
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000789
790 if (FPC->getType() == Type::DoubleTy)
791 Out << " const ConstantDoubleTy FloatConstant" << FPCounter++
792 << " = 0x" << std::hex << *(unsigned long long*)&Val << std::dec
793 << "; /* " << Val << " */\n";
Chris Lattner9c1338b2002-11-07 22:12:53 +0000794 else if (FPC->getType() == Type::FloatTy) {
795 float fVal = Val;
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000796 Out << " const ConstantFloatTy FloatConstant" << FPCounter++
Chris Lattner9c1338b2002-11-07 22:12:53 +0000797 << " = 0x" << std::hex << *(unsigned*)&fVal << std::dec
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000798 << "; /* " << Val << " */\n";
Chris Lattner9c1338b2002-11-07 22:12:53 +0000799 } else
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000800 assert(0 && "Unknown float type!");
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000801 }
802
803 Out << "\n";
Chris Lattner16c7bb22002-05-09 02:28:59 +0000804
805 // print the basic blocks
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000806 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
807 BasicBlock *Prev = BB->getPrev();
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000808
809 // Don't print the label for the basic block if there are no uses, or if the
810 // only terminator use is the precessor basic block's terminator. We have
811 // to scan the use list because PHI nodes use basic blocks too but do not
812 // require a label to be generated.
813 //
814 bool NeedsLabel = false;
815 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
816 UI != UE; ++UI)
817 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
Chris Lattnerf1acd962003-04-23 19:15:13 +0000818 if (TI != Prev->getTerminator() ||
819 isa<SwitchInst>(Prev->getTerminator())) {
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000820 NeedsLabel = true;
821 break;
822 }
823
824 if (NeedsLabel) Out << getValueName(BB) << ":\n";
825
826 // Output all of the instructions in the basic block...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000827 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II){
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000828 if (!isInlinableInst(*II) && !isa<PHINode>(*II)) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000829 if (II->getType() != Type::VoidTy)
830 outputLValue(II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000831 else
832 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000833 visit(*II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000834 Out << ";\n";
835 }
836 }
837
838 // Don't emit prefix or suffix for the terminator...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000839 visit(*BB->getTerminator());
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000840 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000841
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000842 Out << "}\n\n";
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000843 Table->purgeFunction();
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000844 FPConstantMap.clear();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000845}
846
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000847// Specific Instruction type classes... note that all of the casts are
848// neccesary because we use the instruction classes as opaque types...
849//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000850void CWriter::visitReturnInst(ReturnInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000851 // Don't output a void return if this is the last basic block in the function
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000852 if (I.getNumOperands() == 0 &&
853 &*--I.getParent()->getParent()->end() == I.getParent() &&
854 !I.getParent()->size() == 1) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000855 return;
Chris Lattner963b70b2002-05-21 18:05:19 +0000856 }
Chris Lattner44408262002-05-09 03:50:42 +0000857
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000858 Out << " return";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000859 if (I.getNumOperands()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000860 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000861 writeOperand(I.getOperand(0));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000862 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000863 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000864}
865
Chris Lattnera9f5e052003-04-22 20:19:52 +0000866void CWriter::visitSwitchInst(SwitchInst &SI) {
867 Out << " switch (";
868 writeOperand(SI.getOperand(0));
Chris Lattnerf1acd962003-04-23 19:15:13 +0000869 Out << ") {\n default:\n";
870 printBranchToBlock(SI.getParent(), SI.getDefaultDest(), 2);
Chris Lattnera9f5e052003-04-22 20:19:52 +0000871 Out << ";\n";
872 for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) {
873 Out << " case ";
874 writeOperand(SI.getOperand(i));
875 Out << ":\n";
876 BasicBlock *Succ = cast<BasicBlock>(SI.getOperand(i+1));
877 printBranchToBlock(SI.getParent(), Succ, 2);
878 if (Succ == SI.getParent()->getNext())
879 Out << " break;\n";
880 }
881 Out << " }\n";
882}
883
884
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000885static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
886 // If PHI nodes need copies, we need the copy code...
887 if (isa<PHINode>(To->front()) ||
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000888 From->getNext() != To) // Not directly successor, need goto
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000889 return true;
890
891 // Otherwise we don't need the code.
892 return false;
893}
894
895void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
896 unsigned Indent) {
897 for (BasicBlock::iterator I = Succ->begin();
Chris Lattner2ee82e02003-04-23 16:36:11 +0000898 PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000899 // now we have to do the printing
Chris Lattneredd8ce12003-05-03 03:14:35 +0000900 Out << std::string(Indent, ' ');
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000901 outputLValue(PN);
902 writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
903 Out << "; /* for PHI node */\n";
904 }
905
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000906 if (CurBB->getNext() != Succ) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000907 Out << std::string(Indent, ' ') << " goto ";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000908 writeOperand(Succ);
909 Out << ";\n";
910 }
911}
912
913// Brach instruction printing - Avoid printing out a brach to a basic block that
914// immediately succeeds the current one.
915//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000916void CWriter::visitBranchInst(BranchInst &I) {
917 if (I.isConditional()) {
918 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(0))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000919 Out << " if (";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000920 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000921 Out << ") {\n";
922
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000923 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000924
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000925 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(1))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000926 Out << " } else {\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000927 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000928 }
929 } else {
930 // First goto not neccesary, assume second one is...
931 Out << " if (!";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000932 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000933 Out << ") {\n";
934
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000935 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000936 }
937
938 Out << " }\n";
939 } else {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000940 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000941 }
942 Out << "\n";
943}
944
945
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000946void CWriter::visitBinaryOperator(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000947 // binary instructions, shift instructions, setCond instructions.
Chris Lattnerf5612b762003-04-23 19:09:22 +0000948 assert(!isa<PointerType>(I.getType()));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000949
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000950 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000951
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000952 switch (I.getOpcode()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000953 case Instruction::Add: Out << " + "; break;
954 case Instruction::Sub: Out << " - "; break;
955 case Instruction::Mul: Out << "*"; break;
956 case Instruction::Div: Out << "/"; break;
957 case Instruction::Rem: Out << "%"; break;
958 case Instruction::And: Out << " & "; break;
959 case Instruction::Or: Out << " | "; break;
960 case Instruction::Xor: Out << " ^ "; break;
961 case Instruction::SetEQ: Out << " == "; break;
962 case Instruction::SetNE: Out << " != "; break;
963 case Instruction::SetLE: Out << " <= "; break;
964 case Instruction::SetGE: Out << " >= "; break;
965 case Instruction::SetLT: Out << " < "; break;
966 case Instruction::SetGT: Out << " > "; break;
967 case Instruction::Shl : Out << " << "; break;
968 case Instruction::Shr : Out << " >> "; break;
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000969 default: std::cerr << "Invalid operator type!" << I; abort();
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000970 }
971
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000972 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000973}
974
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000975void CWriter::visitCastInst(CastInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000976 Out << "(";
Chris Lattneredd8ce12003-05-03 03:14:35 +0000977 printType(Out, I.getType(), "", /*ignoreName*/false, /*namedContext*/false);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000978 Out << ")";
Chris Lattnerf5612b762003-04-23 19:09:22 +0000979 if (isa<PointerType>(I.getType())&&I.getOperand(0)->getType()->isIntegral() ||
980 isa<PointerType>(I.getOperand(0)->getType())&&I.getType()->isIntegral()) {
981 // Avoid "cast to pointer from integer of different size" warnings
982 Out << "(long)";
983 }
984
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000985 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000986}
987
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000988void CWriter::visitCallInst(CallInst &I) {
989 const PointerType *PTy = cast<PointerType>(I.getCalledValue()->getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000990 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
991 const Type *RetTy = FTy->getReturnType();
992
Chris Lattnerfabc8802002-08-26 20:50:09 +0000993 writeOperand(I.getOperand(0));
994 Out << "(";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000995
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000996 if (I.getNumOperands() > 1) {
997 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000998
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000999 for (unsigned op = 2, Eop = I.getNumOperands(); op != Eop; ++op) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001000 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001001 writeOperand(I.getOperand(op));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001002 }
1003 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001004 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001005}
1006
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001007void CWriter::visitMallocInst(MallocInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001008 Out << "(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001009 printType(Out, I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001010 Out << ")malloc(sizeof(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001011 printType(Out, I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001012 Out << ")";
1013
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001014 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001015 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001016 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001017 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001018 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001019}
1020
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001021void CWriter::visitAllocaInst(AllocaInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001022 Out << "(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001023 printType(Out, I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001024 Out << ") alloca(sizeof(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001025 printType(Out, I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001026 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001027 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001028 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001029 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001030 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001031 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001032}
1033
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001034void CWriter::visitFreeInst(FreeInst &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001035 Out << "free(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001036 writeOperand(I.getOperand(0));
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001037 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001038}
1039
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001040void CWriter::printIndexingExpression(Value *Ptr, User::op_iterator I,
1041 User::op_iterator E) {
1042 bool HasImplicitAddress = false;
1043 // If accessing a global value with no indexing, avoid *(&GV) syndrome
1044 if (GlobalValue *V = dyn_cast<GlobalValue>(Ptr)) {
1045 HasImplicitAddress = true;
1046 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Ptr)) {
1047 HasImplicitAddress = true;
1048 Ptr = CPR->getValue(); // Get to the global...
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001049 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001050
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001051 if (I == E) {
1052 if (!HasImplicitAddress)
1053 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001054
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001055 writeOperandInternal(Ptr);
1056 return;
1057 }
1058
1059 const Constant *CI = dyn_cast<Constant>(I->get());
1060 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
1061 Out << "(&";
1062
1063 writeOperandInternal(Ptr);
1064
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001065 if (HasImplicitAddress && (!CI || !CI->isNullValue())) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001066 Out << ")";
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001067 HasImplicitAddress = false; // HIA is only true if we haven't addressed yet
1068 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001069
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001070 assert(!HasImplicitAddress || (CI && CI->isNullValue()) &&
1071 "Can only have implicit address with direct accessing");
1072
1073 if (HasImplicitAddress) {
1074 ++I;
1075 } else if (CI && CI->isNullValue() && I+1 != E) {
1076 // Print out the -> operator if possible...
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001077 if ((*(I+1))->getType() == Type::UByteTy) {
1078 Out << (HasImplicitAddress ? "." : "->");
1079 Out << "field" << cast<ConstantUInt>(*(I+1))->getValue();
1080 I += 2;
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001081 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001082 }
Chris Lattner5dfe7672002-08-22 22:48:55 +00001083
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001084 for (; I != E; ++I)
Chris Lattner106ff452002-09-11 01:21:29 +00001085 if ((*I)->getType() == Type::LongTy) {
Vikram S. Adve9d6f13f2002-09-15 21:51:04 +00001086 Out << "[";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001087 writeOperand(*I);
Vikram S. Adve9d6f13f2002-09-15 21:51:04 +00001088 Out << "]";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001089 } else {
Chris Lattner59afbf32002-09-12 20:34:47 +00001090 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001091 }
1092}
1093
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001094void CWriter::visitLoadInst(LoadInst &I) {
Nick Hildenbrandtca626922002-10-02 21:14:33 +00001095 Out << "*";
Chris Lattner5dfe7672002-08-22 22:48:55 +00001096 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001097}
1098
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001099void CWriter::visitStoreInst(StoreInst &I) {
Nick Hildenbrandtca626922002-10-02 21:14:33 +00001100 Out << "*";
Chris Lattner5dfe7672002-08-22 22:48:55 +00001101 writeOperand(I.getPointerOperand());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001102 Out << " = ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001103 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001104}
1105
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001106void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
Nick Hildenbrandtca626922002-10-02 21:14:33 +00001107 Out << "&";
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001108 printIndexingExpression(I.getPointerOperand(), I.idx_begin(), I.idx_end());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001109}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001110
1111//===----------------------------------------------------------------------===//
1112// External Interface declaration
1113//===----------------------------------------------------------------------===//
1114
Chris Lattner0e9f93e2002-08-31 00:29:16 +00001115Pass *createWriteToCPass(std::ostream &o) { return new CWriter(o); }