blob: bc3829f67cdbf5e8276a5f46233111e61754e37d [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//
3// This library implements the functionality defined in llvm/Assembly/CWriter.h
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00004//
5// TODO : Recursive types.
Chris Lattner16c7bb22002-05-09 02:28:59 +00006//
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00007//===-----------------------------------------------------------------------==//
Chris Lattner16c7bb22002-05-09 02:28:59 +00008
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00009#include "llvm/Assembly/CWriter.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000010#include "llvm/Constants.h"
Chris Lattner2f5f51a2002-05-09 03:28:37 +000011#include "llvm/DerivedTypes.h"
12#include "llvm/Module.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000013#include "llvm/iMemory.h"
14#include "llvm/iTerminators.h"
15#include "llvm/iPHINode.h"
16#include "llvm/iOther.h"
Chris Lattner2f5f51a2002-05-09 03:28:37 +000017#include "llvm/iOperators.h"
Chris Lattner0e9f93e2002-08-31 00:29:16 +000018#include "llvm/Pass.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000019#include "llvm/SymbolTable.h"
Chris Lattner3af3ba82002-05-09 21:31:18 +000020#include "llvm/SlotCalculator.h"
Chris Lattner0e9f93e2002-08-31 00:29:16 +000021#include "llvm/Analysis/FindUsedTypes.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000022#include "llvm/Support/InstVisitor.h"
Chris Lattner7683a122002-05-09 20:33:35 +000023#include "llvm/Support/InstIterator.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000024#include "Support/StringExtras.h"
25#include "Support/STLExtras.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000026#include <algorithm>
Chris Lattner78771c82002-05-17 04:55:35 +000027#include <set>
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000028using std::string;
29using std::map;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000030using std::ostream;
31
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000032namespace {
Chris Lattner0e9f93e2002-08-31 00:29:16 +000033 class CWriter : public Pass, public InstVisitor<CWriter> {
34 ostream &Out;
35 SlotCalculator *Table;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000036 const Module *TheModule;
37 map<const Type *, string> TypeNames;
Chris Lattner78771c82002-05-17 04:55:35 +000038 std::set<const Value*> MangledGlobals;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000039 public:
Chris Lattner0e9f93e2002-08-31 00:29:16 +000040 CWriter(ostream &o) : Out(o) {}
41
42 void getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.setPreservesAll();
44 AU.addRequired<FindUsedTypes>();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000045 }
Chris Lattner0e9f93e2002-08-31 00:29:16 +000046
47 virtual bool run(Module &M) {
48 // Initialize
49 Table = new SlotCalculator(&M, false);
50 TheModule = &M;
51
52 // Ensure that all structure types have names...
53 bool Changed = nameAllUsedStructureTypes(M);
54
55 // Run...
56 printModule(&M);
57
58 // Free memory...
59 delete Table;
60 TypeNames.clear();
61 MangledGlobals.clear();
62 return false;
63 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000064
Chris Lattner83c57752002-08-19 22:17:53 +000065 ostream &printType(const Type *Ty, const string &VariableName = "",
Vikram S. Adve969c4ad2002-08-25 20:00:08 +000066 bool IgnoreName = false, bool namedContext = true);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000067
Chris Lattnerbb03efd2002-06-25 15:57:03 +000068 void writeOperand(Value *Operand);
69 void writeOperandInternal(Value *Operand);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000070
71 string getValueName(const Value *V);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000072
Chris Lattner4fbf26d2002-05-09 20:53:56 +000073 private :
Chris Lattner0e9f93e2002-08-31 00:29:16 +000074 bool nameAllUsedStructureTypes(Module &M);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +000075 void printModule(Module *M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000076 void printSymbolTable(const SymbolTable &ST);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000077 void printGlobal(const GlobalVariable *GV);
Chris Lattner4cda8352002-08-20 16:55:48 +000078 void printFunctionSignature(const Function *F, bool Prototype);
79
Chris Lattner8c3c4bf2002-05-09 15:49:41 +000080 void printFunction(Function *);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000081
Chris Lattner6d492922002-08-19 21:32:41 +000082 void printConstant(Constant *CPV);
83 void printConstantArray(ConstantArray *CPA);
84
Chris Lattnerd0c668c2002-05-09 21:18:38 +000085 // isInlinableInst - Attempt to inline instructions into their uses to build
86 // trees as much as possible. To do this, we have to consistently decide
87 // what is acceptable to inline, so that variable declarations don't get
88 // printed and an extra copy of the expr is not emitted.
89 //
Chris Lattnerbb03efd2002-06-25 15:57:03 +000090 static bool isInlinableInst(const Instruction &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +000091 // Must be an expression, must be used exactly once. If it is dead, we
92 // emit it inline where it would go.
Chris Lattnerbb03efd2002-06-25 15:57:03 +000093 if (I.getType() == Type::VoidTy || I.use_size() != 1 ||
Chris Lattnerd0c668c2002-05-09 21:18:38 +000094 isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I))
95 return false;
96
97 // Only inline instruction it it's use is in the same BB as the inst.
Chris Lattnerbb03efd2002-06-25 15:57:03 +000098 return I.getParent() == cast<Instruction>(I.use_back())->getParent();
Chris Lattnerd0c668c2002-05-09 21:18:38 +000099 }
100
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000101 // Instruction visitation functions
102 friend class InstVisitor<CWriter>;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000103
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000104 void visitReturnInst(ReturnInst &I);
105 void visitBranchInst(BranchInst &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000106
Chris Lattnerbb03efd2002-06-25 15:57:03 +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 Lattner2f5f51a2002-05-09 03:28:37 +0000120
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000121 void visitInstruction(Instruction &I) {
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000122 std::cerr << "C Writer does not know about " << I;
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000123 abort();
124 }
125
126 void outputLValue(Instruction *I) {
127 Out << " " << getValueName(I) << " = ";
128 }
129 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
130 unsigned Indent);
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000131 void printIndexingExpression(Value *Ptr, User::op_iterator I,
132 User::op_iterator E);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000133 };
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000134}
135
Chris Lattner2f499022002-05-09 04:21:21 +0000136// We dont want identifier names with ., space, - in them.
137// So we replace them with _
138static string makeNameProper(string x) {
139 string tmp;
140 for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
141 switch (*sI) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000142 case '.': tmp += "d_"; break;
143 case ' ': tmp += "s_"; break;
144 case '-': tmp += "D_"; break;
Chris Lattner2f499022002-05-09 04:21:21 +0000145 default: tmp += *sI;
146 }
147
148 return tmp;
149}
150
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000151string CWriter::getValueName(const Value *V) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000152 if (V->hasName()) { // Print out the label if it exists...
153 if (isa<GlobalValue>(V) && // Do not mangle globals...
154 cast<GlobalValue>(V)->hasExternalLinkage() && // Unless it's internal or
155 !MangledGlobals.count(V)) // Unless the name would collide if we don't
Chris Lattner2f499022002-05-09 04:21:21 +0000156 return makeNameProper(V->getName());
157
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000158 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
159 makeNameProper(V->getName());
Chris Lattner2f499022002-05-09 04:21:21 +0000160 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000161
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000162 int Slot = Table->getValSlot(V);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000163 assert(Slot >= 0 && "Invalid value!");
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000164 return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000165}
166
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000167// A pointer type should not use parens around *'s alone, e.g., (**)
168inline bool ptrTypeNameNeedsParens(const string &NameSoFar) {
169 return (NameSoFar.find_last_not_of('*') != std::string::npos);
170}
171
Chris Lattner83c57752002-08-19 22:17:53 +0000172// Pass the Type* and the variable name and this prints out the variable
173// declaration.
174//
175ostream &CWriter::printType(const Type *Ty, const string &NameSoFar,
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000176 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.
197 if (!IgnoreName) {
198 map<const Type *, string>::iterator I = TypeNames.find(Ty);
199 if (I != TypeNames.end()) {
200 return Out << I->second << " " << NameSoFar;
201 }
202 }
203
Chris Lattner83c57752002-08-19 22:17:53 +0000204 switch (Ty->getPrimitiveID()) {
205 case Type::FunctionTyID: {
206 const FunctionType *MTy = cast<FunctionType>(Ty);
207 printType(MTy->getReturnType(), "");
208 Out << " " << NameSoFar << " (";
209
210 for (FunctionType::ParamTypes::const_iterator
211 I = MTy->getParamTypes().begin(),
212 E = MTy->getParamTypes().end(); I != E; ++I) {
213 if (I != MTy->getParamTypes().begin())
214 Out << ", ";
215 printType(*I, "");
216 }
217 if (MTy->isVarArg()) {
218 if (!MTy->getParamTypes().empty())
219 Out << ", ";
220 Out << "...";
221 }
222 return Out << ")";
223 }
224 case Type::StructTyID: {
225 const StructType *STy = cast<StructType>(Ty);
226 Out << NameSoFar + " {\n";
227 unsigned Idx = 0;
228 for (StructType::ElementTypes::const_iterator
229 I = STy->getElementTypes().begin(),
230 E = STy->getElementTypes().end(); I != E; ++I) {
231 Out << " ";
232 printType(*I, "field" + utostr(Idx++));
233 Out << ";\n";
234 }
235 return Out << "}";
236 }
237
238 case Type::PointerTyID: {
239 const PointerType *PTy = cast<PointerType>(Ty);
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000240 std::string ptrName = "*" + NameSoFar;
241
242 // Do not need parens around "* NameSoFar" if NameSoFar consists only
243 // of zero or more '*' chars *and* this is not an unnamed pointer type
244 // such as the result type in a cast statement. Otherwise, enclose in ( ).
245 if (ptrTypeNameNeedsParens(NameSoFar) || !namedContext)
246 ptrName = "(" + ptrName + ")"; //
247
Vikram S. Adve42eb2ba2002-08-24 14:44:23 +0000248 return printType(PTy->getElementType(), ptrName);
Chris Lattner83c57752002-08-19 22:17:53 +0000249 }
250
251 case Type::ArrayTyID: {
252 const ArrayType *ATy = cast<ArrayType>(Ty);
253 unsigned NumElements = ATy->getNumElements();
254 return printType(ATy->getElementType(),
255 NameSoFar + "[" + utostr(NumElements) + "]");
256 }
257 default:
258 assert(0 && "Unhandled case in getTypeProps!");
259 abort();
260 }
261
262 return Out;
263}
264
Chris Lattner6d492922002-08-19 21:32:41 +0000265void CWriter::printConstantArray(ConstantArray *CPA) {
266
267 // As a special case, print the array as a string if it is an array of
268 // ubytes or an array of sbytes with positive values.
269 //
270 const Type *ETy = CPA->getType()->getElementType();
271 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
272
273 // Make sure the last character is a null char, as automatically added by C
274 if (CPA->getNumOperands() == 0 ||
275 !cast<Constant>(*(CPA->op_end()-1))->isNullValue())
276 isString = false;
277
278 if (isString) {
279 Out << "\"";
280 // Do not include the last character, which we know is null
281 for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) {
282 unsigned char C = (ETy == Type::SByteTy) ?
283 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
284 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
285
286 if (isprint(C)) {
287 Out << C;
288 } else {
289 switch (C) {
290 case '\n': Out << "\\n"; break;
291 case '\t': Out << "\\t"; break;
292 case '\r': Out << "\\r"; break;
293 case '\v': Out << "\\v"; break;
294 case '\a': Out << "\\a"; break;
295 default:
296 Out << "\\x";
297 Out << ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
298 Out << ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
299 break;
300 }
301 }
302 }
303 Out << "\"";
304 } else {
305 Out << "{";
306 if (CPA->getNumOperands()) {
307 Out << " ";
308 printConstant(cast<Constant>(CPA->getOperand(0)));
309 for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
310 Out << ", ";
311 printConstant(cast<Constant>(CPA->getOperand(i)));
312 }
313 }
314 Out << " }";
315 }
316}
317
318
319// printConstant - The LLVM Constant to C Constant converter.
320void CWriter::printConstant(Constant *CPV) {
321 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
322 switch (CE->getOpcode()) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000323 case Instruction::Cast:
324 Out << "((";
325 printType(CPV->getType());
326 Out << ")";
327 printConstant(cast<Constant>(CPV->getOperand(0)));
328 Out << ")";
329 return;
330
331 case Instruction::GetElementPtr:
332 Out << "&(";
333 printIndexingExpression(CPV->getOperand(0),
334 CPV->op_begin()+1, CPV->op_end());
335 Out << ")";
336 return;
337 case Instruction::Add:
338 Out << "(";
339 printConstant(cast<Constant>(CPV->getOperand(0)));
340 Out << " + ";
341 printConstant(cast<Constant>(CPV->getOperand(1)));
342 Out << ")";
343 return;
344 case Instruction::Sub:
345 Out << "(";
346 printConstant(cast<Constant>(CPV->getOperand(0)));
347 Out << " - ";
348 printConstant(cast<Constant>(CPV->getOperand(1)));
349 Out << ")";
350 return;
351
Chris Lattner6d492922002-08-19 21:32:41 +0000352 default:
353 std::cerr << "CWriter Error: Unhandled constant expression: "
354 << CE << "\n";
355 abort();
356 }
357 }
358
359 switch (CPV->getType()->getPrimitiveID()) {
360 case Type::BoolTyID:
361 Out << (CPV == ConstantBool::False ? "0" : "1"); break;
362 case Type::SByteTyID:
363 case Type::ShortTyID:
364 case Type::IntTyID:
365 Out << cast<ConstantSInt>(CPV)->getValue(); break;
366 case Type::LongTyID:
367 Out << cast<ConstantSInt>(CPV)->getValue() << "ll"; break;
368
369 case Type::UByteTyID:
370 case Type::UShortTyID:
371 Out << cast<ConstantUInt>(CPV)->getValue(); break;
372 case Type::UIntTyID:
373 Out << cast<ConstantUInt>(CPV)->getValue() << "u"; break;
374 case Type::ULongTyID:
375 Out << cast<ConstantUInt>(CPV)->getValue() << "ull"; break;
376
377 case Type::FloatTyID:
378 case Type::DoubleTyID:
379 Out << cast<ConstantFP>(CPV)->getValue(); break;
380
381 case Type::ArrayTyID:
382 printConstantArray(cast<ConstantArray>(CPV));
383 break;
384
385 case Type::StructTyID: {
386 Out << "{";
387 if (CPV->getNumOperands()) {
388 Out << " ";
389 printConstant(cast<Constant>(CPV->getOperand(0)));
390 for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) {
391 Out << ", ";
392 printConstant(cast<Constant>(CPV->getOperand(i)));
393 }
394 }
395 Out << " }";
396 break;
397 }
398
399 case Type::PointerTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000400 if (isa<ConstantPointerNull>(CPV)) {
401 Out << "((";
Chris Lattner3af3ba82002-05-09 21:31:18 +0000402 printType(CPV->getType(), "");
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000403 Out << ")NULL)";
Chris Lattner6d492922002-08-19 21:32:41 +0000404 break;
405 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
406 writeOperand(CPR->getValue());
407 break;
408 }
409 // FALL THROUGH
410 default:
411 std::cerr << "Unknown constant type: " << CPV << "\n";
412 abort();
413 }
414}
415
416void CWriter::writeOperandInternal(Value *Operand) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000417 if (Instruction *I = dyn_cast<Instruction>(Operand))
418 if (isInlinableInst(*I)) {
419 // Should we inline this instruction to build a tree?
420 Out << "(";
421 visit(*I);
422 Out << ")";
423 return;
424 }
425
Chris Lattner6d492922002-08-19 21:32:41 +0000426 if (Operand->hasName()) {
427 Out << getValueName(Operand);
428 } else if (Constant *CPV = dyn_cast<Constant>(Operand)) {
429 printConstant(CPV);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000430 } else {
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000431 int Slot = Table->getValSlot(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000432 assert(Slot >= 0 && "Malformed LLVM!");
433 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
434 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000435}
436
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000437void CWriter::writeOperand(Value *Operand) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000438 if (isa<GlobalVariable>(Operand))
439 Out << "(&"; // Global variables are references as their addresses by llvm
440
441 writeOperandInternal(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000442
443 if (isa<GlobalVariable>(Operand))
444 Out << ")";
445}
446
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000447// nameAllUsedStructureTypes - If there are structure types in the module that
448// are used but do not have names assigned to them in the symbol table yet then
449// we assign them names now.
450//
451bool CWriter::nameAllUsedStructureTypes(Module &M) {
452 // Get a set of types that are used by the program...
453 std::set<const Type *> UT = getAnalysis<FindUsedTypes>().getTypes();
454
455 // Loop over the module symbol table, removing types from UT that are already
456 // named.
457 //
458 SymbolTable *MST = M.getSymbolTableSure();
459 if (MST->find(Type::TypeTy) != MST->end())
460 for (SymbolTable::type_iterator I = MST->type_begin(Type::TypeTy),
461 E = MST->type_end(Type::TypeTy); I != E; ++I)
462 UT.erase(cast<Type>(I->second));
463
464 // UT now contains types that are not named. Loop over it, naming structure
465 // types.
466 //
467 bool Changed = false;
468 for (std::set<const Type *>::const_iterator I = UT.begin(), E = UT.end();
469 I != E; ++I)
470 if (const StructType *ST = dyn_cast<StructType>(*I)) {
471 ((Value*)ST)->setName("unnamed", MST);
472 Changed = true;
473 }
474 return Changed;
475}
476
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000477void CWriter::printModule(Module *M) {
Chris Lattner78771c82002-05-17 04:55:35 +0000478 // Calculate which global values have names that will collide when we throw
479 // away type information.
Chris Lattner594b9fa2002-05-21 21:10:04 +0000480 { // Scope to delete the FoundNames set when we are done with it...
Chris Lattner78771c82002-05-17 04:55:35 +0000481 std::set<string> FoundNames;
482 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000483 if (I->hasName()) // If the global has a name...
484 if (FoundNames.count(I->getName())) // And the name is already used
485 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000486 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000487 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000488
489 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000490 if (I->hasName()) // If the global has a name...
491 if (FoundNames.count(I->getName())) // And the name is already used
492 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000493 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000494 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000495 }
496
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000497 // printing stdlib inclusion
498 // Out << "#include <stdlib.h>\n";
499
Chris Lattner16c7bb22002-05-09 02:28:59 +0000500 // get declaration for alloca
501 Out << "/* Provide Declarations */\n"
Chris Lattner594b9fa2002-05-21 21:10:04 +0000502 << "#include <malloc.h>\n"
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000503 << "#include <alloca.h>\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000504
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000505 // Provide a definition for null if one does not already exist,
506 // and for `bool' if not compiling with a C++ compiler.
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000507 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000508 << "#ifndef __cplusplus\ntypedef unsigned char bool;\n#endif\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000509
Chris Lattnera4d4a852002-08-19 21:48:40 +0000510 << "\n\n/* Global Declarations */\n";
511
512 // First output all the declarations for the program, because C requires
513 // Functions & globals to be declared before they are used.
514 //
Chris Lattner16c7bb22002-05-09 02:28:59 +0000515
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000516 // Loop over the symbol table, emitting all named constants...
517 if (M->hasSymbolTable())
518 printSymbolTable(*M->getSymbolTable());
519
Chris Lattnera4d4a852002-08-19 21:48:40 +0000520 // Global variable declarations...
521 if (!M->gempty()) {
522 Out << "\n/* Global Variable Declarations */\n";
523 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
524 Out << (I->hasExternalLinkage() ? "extern " : "static ");
525 printType(I->getType()->getElementType(), getValueName(I));
526 Out << ";\n";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000527 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000528 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000529
Chris Lattnera4d4a852002-08-19 21:48:40 +0000530 // Function declarations
531 if (!M->empty()) {
532 Out << "\n/* Function Declarations */\n";
Chris Lattner4cda8352002-08-20 16:55:48 +0000533 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
534 printFunctionSignature(I, true);
535 Out << ";\n";
536 }
Chris Lattnera4d4a852002-08-19 21:48:40 +0000537 }
538
539 // Output the global variable contents...
540 if (!M->gempty()) {
541 Out << "\n\n/* Global Data */\n";
542 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
543 if (I->hasInternalLinkage()) Out << "static ";
544 printType(I->getType()->getElementType(), getValueName(I));
545
546 if (I->hasInitializer()) {
547 Out << " = " ;
548 writeOperand(I->getInitializer());
549 }
550 Out << ";\n";
551 }
552 }
553
Chris Lattner16c7bb22002-05-09 02:28:59 +0000554 // Output all of the functions...
Chris Lattnera4d4a852002-08-19 21:48:40 +0000555 if (!M->empty()) {
556 Out << "\n\n/* Function Bodies */\n";
557 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
558 printFunction(I);
559 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000560}
561
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000562
563// printSymbolTable - Run through symbol table looking for named constants
564// if a named constant is found, emit it's declaration...
565// Assuming that symbol table has only types and constants.
566void CWriter::printSymbolTable(const SymbolTable &ST) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000567 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
568 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
569 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
570
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000571 for (; I != End; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000572 if (const Type *Ty = dyn_cast<StructType>(I->second)) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000573 string Name = "struct l_" + makeNameProper(I->first);
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000574 Out << Name << ";\n";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000575 TypeNames.insert(std::make_pair(Ty, Name));
576 }
577 }
578
579 Out << "\n";
580
581 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
582 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
583 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
584
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000585 for (; I != End; ++I) {
586 const Value *V = I->second;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000587 if (const Type *Ty = dyn_cast<Type>(V)) {
588 string Name = "l_" + makeNameProper(I->first);
Chris Lattner1f02c892002-05-09 20:14:10 +0000589 if (isa<StructType>(Ty))
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000590 Name = "struct " + makeNameProper(Name);
Chris Lattner1f02c892002-05-09 20:14:10 +0000591 else
592 Out << "typedef ";
593
Chris Lattner83c57752002-08-19 22:17:53 +0000594 printType(Ty, Name, true);
595 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000596 }
597 }
598 }
599}
600
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000601
Chris Lattner4cda8352002-08-20 16:55:48 +0000602void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000603 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000604
605 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000606 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000607
Chris Lattner16c7bb22002-05-09 02:28:59 +0000608 // Print out the return type and name...
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000609 printType(F->getReturnType());
Chris Lattner1f02c892002-05-09 20:14:10 +0000610 Out << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000611
Chris Lattner16c7bb22002-05-09 02:28:59 +0000612 if (!F->isExternal()) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000613 if (!F->aempty()) {
Chris Lattner4cda8352002-08-20 16:55:48 +0000614 string ArgName;
615 if (F->abegin()->hasName() || !Prototype)
616 ArgName = getValueName(F->abegin());
617
618 printType(F->afront().getType(), ArgName);
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000619
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000620 for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
621 I != E; ++I) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000622 Out << ", ";
Chris Lattner4cda8352002-08-20 16:55:48 +0000623 if (I->hasName() || !Prototype)
624 ArgName = getValueName(I);
625 else
626 ArgName = "";
627 printType(I->getType(), ArgName);
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000628 }
629 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000630 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000631 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000632 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000633 FT->getParamTypes().begin(),
634 E = FT->getParamTypes().end(); I != E; ++I) {
635 if (I != FT->getParamTypes().begin()) Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000636 printType(*I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000637 }
638 }
639
640 // Finish printing arguments...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000641 if (FT->isVarArg()) {
642 if (FT->getParamTypes().size()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000643 Out << "..."; // Output varargs portion of signature!
644 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000645 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000646}
647
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000648
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000649void CWriter::printFunction(Function *F) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000650 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000651
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000652 Table->incorporateFunction(F);
Chris Lattnerf34ee812002-05-09 03:12:34 +0000653
Chris Lattner4cda8352002-08-20 16:55:48 +0000654 printFunctionSignature(F, false);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000655 Out << " {\n";
656
Chris Lattner497e19a2002-05-09 20:39:03 +0000657 // print local variable information for the function
Chris Lattner7683a122002-05-09 20:33:35 +0000658 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000659 if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000660 Out << " ";
Chris Lattner3af3ba82002-05-09 21:31:18 +0000661 printType((*I)->getType(), getValueName(*I));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000662 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000663 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000664
665 // print the basic blocks
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000666 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
667 BasicBlock *Prev = BB->getPrev();
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000668
669 // Don't print the label for the basic block if there are no uses, or if the
670 // only terminator use is the precessor basic block's terminator. We have
671 // to scan the use list because PHI nodes use basic blocks too but do not
672 // require a label to be generated.
673 //
674 bool NeedsLabel = false;
675 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
676 UI != UE; ++UI)
677 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
678 if (TI != Prev->getTerminator()) {
679 NeedsLabel = true;
680 break;
681 }
682
683 if (NeedsLabel) Out << getValueName(BB) << ":\n";
684
685 // Output all of the instructions in the basic block...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000686 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II){
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000687 if (!isInlinableInst(*II) && !isa<PHINode>(*II)) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000688 if (II->getType() != Type::VoidTy)
689 outputLValue(II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000690 else
691 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000692 visit(*II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000693 Out << ";\n";
694 }
695 }
696
697 // Don't emit prefix or suffix for the terminator...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000698 visit(*BB->getTerminator());
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000699 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000700
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000701 Out << "}\n\n";
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000702 Table->purgeFunction();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000703}
704
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000705// Specific Instruction type classes... note that all of the casts are
706// neccesary because we use the instruction classes as opaque types...
707//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000708void CWriter::visitReturnInst(ReturnInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000709 // Don't output a void return if this is the last basic block in the function
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000710 if (I.getNumOperands() == 0 &&
711 &*--I.getParent()->getParent()->end() == I.getParent() &&
712 !I.getParent()->size() == 1) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000713 return;
Chris Lattner963b70b2002-05-21 18:05:19 +0000714 }
Chris Lattner44408262002-05-09 03:50:42 +0000715
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000716 Out << " return";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000717 if (I.getNumOperands()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000718 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000719 writeOperand(I.getOperand(0));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000720 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000721 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000722}
723
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000724static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
725 // If PHI nodes need copies, we need the copy code...
726 if (isa<PHINode>(To->front()) ||
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000727 From->getNext() != To) // Not directly successor, need goto
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000728 return true;
729
730 // Otherwise we don't need the code.
731 return false;
732}
733
734void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
735 unsigned Indent) {
736 for (BasicBlock::iterator I = Succ->begin();
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000737 PHINode *PN = dyn_cast<PHINode>(&*I); ++I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000738 // now we have to do the printing
739 Out << string(Indent, ' ');
740 outputLValue(PN);
741 writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
742 Out << "; /* for PHI node */\n";
743 }
744
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000745 if (CurBB->getNext() != Succ) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000746 Out << string(Indent, ' ') << " goto ";
747 writeOperand(Succ);
748 Out << ";\n";
749 }
750}
751
752// Brach instruction printing - Avoid printing out a brach to a basic block that
753// immediately succeeds the current one.
754//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000755void CWriter::visitBranchInst(BranchInst &I) {
756 if (I.isConditional()) {
757 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(0))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000758 Out << " if (";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000759 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000760 Out << ") {\n";
761
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000762 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000763
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000764 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(1))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000765 Out << " } else {\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000766 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000767 }
768 } else {
769 // First goto not neccesary, assume second one is...
770 Out << " if (!";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000771 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000772 Out << ") {\n";
773
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000774 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000775 }
776
777 Out << " }\n";
778 } else {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000779 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000780 }
781 Out << "\n";
782}
783
784
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000785void CWriter::visitBinaryOperator(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000786 // binary instructions, shift instructions, setCond instructions.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000787 if (isa<PointerType>(I.getType())) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000788 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000789 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000790 Out << ")";
791 }
792
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000793 if (isa<PointerType>(I.getType())) Out << "(long long)";
794 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000795
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000796 switch (I.getOpcode()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000797 case Instruction::Add: Out << " + "; break;
798 case Instruction::Sub: Out << " - "; break;
799 case Instruction::Mul: Out << "*"; break;
800 case Instruction::Div: Out << "/"; break;
801 case Instruction::Rem: Out << "%"; break;
802 case Instruction::And: Out << " & "; break;
803 case Instruction::Or: Out << " | "; break;
804 case Instruction::Xor: Out << " ^ "; break;
805 case Instruction::SetEQ: Out << " == "; break;
806 case Instruction::SetNE: Out << " != "; break;
807 case Instruction::SetLE: Out << " <= "; break;
808 case Instruction::SetGE: Out << " >= "; break;
809 case Instruction::SetLT: Out << " < "; break;
810 case Instruction::SetGT: Out << " > "; break;
811 case Instruction::Shl : Out << " << "; break;
812 case Instruction::Shr : Out << " >> "; break;
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000813 default: std::cerr << "Invalid operator type!" << I; abort();
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000814 }
815
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000816 if (isa<PointerType>(I.getType())) Out << "(long long)";
817 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000818}
819
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000820void CWriter::visitCastInst(CastInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000821 Out << "(";
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000822 printType(I.getType(), string(""),/*ignoreName*/false, /*namedContext*/false);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000823 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000824 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000825}
826
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000827void CWriter::visitCallInst(CallInst &I) {
828 const PointerType *PTy = cast<PointerType>(I.getCalledValue()->getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000829 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
830 const Type *RetTy = FTy->getReturnType();
831
Chris Lattnerfabc8802002-08-26 20:50:09 +0000832 writeOperand(I.getOperand(0));
833 Out << "(";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000834
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000835 if (I.getNumOperands() > 1) {
836 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000837
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000838 for (unsigned op = 2, Eop = I.getNumOperands(); op != Eop; ++op) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000839 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000840 writeOperand(I.getOperand(op));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000841 }
842 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000843 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000844}
845
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000846void CWriter::visitMallocInst(MallocInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000847 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000848 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000849 Out << ")malloc(sizeof(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000850 printType(I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000851 Out << ")";
852
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000853 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000854 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000855 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000856 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000857 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000858}
859
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000860void CWriter::visitAllocaInst(AllocaInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000861 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000862 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000863 Out << ") alloca(sizeof(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000864 printType(I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000865 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000866 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000867 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000868 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000869 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000870 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000871}
872
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000873void CWriter::visitFreeInst(FreeInst &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000874 Out << "free(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000875 writeOperand(I.getOperand(0));
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000876 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000877}
878
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000879void CWriter::printIndexingExpression(Value *Ptr, User::op_iterator I,
880 User::op_iterator E) {
881 bool HasImplicitAddress = false;
882 // If accessing a global value with no indexing, avoid *(&GV) syndrome
883 if (GlobalValue *V = dyn_cast<GlobalValue>(Ptr)) {
884 HasImplicitAddress = true;
885 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Ptr)) {
886 HasImplicitAddress = true;
887 Ptr = CPR->getValue(); // Get to the global...
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000888 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000889
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000890 if (I == E) {
891 if (!HasImplicitAddress)
892 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000893
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000894 writeOperandInternal(Ptr);
895 return;
896 }
897
898 const Constant *CI = dyn_cast<Constant>(I->get());
899 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
900 Out << "(&";
901
902 writeOperandInternal(Ptr);
903
904 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
905 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000906
907 // Print out the -> operator if possible...
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000908 if (CI && CI->isNullValue() && I+1 != E) {
909 if ((*(I+1))->getType() == Type::UByteTy) {
910 Out << (HasImplicitAddress ? "." : "->");
911 Out << "field" << cast<ConstantUInt>(*(I+1))->getValue();
912 I += 2;
Vikram S. Adve42eb2ba2002-08-24 14:44:23 +0000913 } else { // First array index of 0: Just skip it
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000914 ++I;
915 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000916 }
Chris Lattner5dfe7672002-08-22 22:48:55 +0000917
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000918 for (; I != E; ++I)
Chris Lattner106ff452002-09-11 01:21:29 +0000919 if ((*I)->getType() == Type::LongTy) {
Vikram S. Adve42eb2ba2002-08-24 14:44:23 +0000920 Out << "[((int) ("; // sign-extend from 32 (to 64) bits
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000921 writeOperand(*I);
Vikram S. Adve42eb2ba2002-08-24 14:44:23 +0000922 Out << " * sizeof(";
923 printType(cast<PointerType>(Ptr->getType())->getElementType());
924 Out << "))) / sizeof(";
925 printType(cast<PointerType>(Ptr->getType())->getElementType());
926 Out << ")]";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000927 } else {
Chris Lattner106ff452002-09-11 01:21:29 +0000928 Out << ".field" << cast<ConstantSInt>(*I)->getValue();
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000929 }
930}
931
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000932void CWriter::visitLoadInst(LoadInst &I) {
Chris Lattner5dfe7672002-08-22 22:48:55 +0000933 Out << "*";
934 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000935}
936
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000937void CWriter::visitStoreInst(StoreInst &I) {
Chris Lattner5dfe7672002-08-22 22:48:55 +0000938 Out << "*";
939 writeOperand(I.getPointerOperand());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000940 Out << " = ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000941 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000942}
943
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000944void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000945 Out << "&";
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000946 printIndexingExpression(I.getPointerOperand(), I.idx_begin(), I.idx_end());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000947}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000948
949//===----------------------------------------------------------------------===//
950// External Interface declaration
951//===----------------------------------------------------------------------===//
952
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000953Pass *createWriteToCPass(std::ostream &o) { return new CWriter(o); }