blob: 222bad3dd2994e42c94e86d5d4062a1fafcd70e1 [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;
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +000039 std::set<const StructType *> StructPrinted;
40
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000041 public:
Chris Lattner0e9f93e2002-08-31 00:29:16 +000042 CWriter(ostream &o) : Out(o) {}
43
44 void getAnalysisUsage(AnalysisUsage &AU) const {
45 AU.setPreservesAll();
46 AU.addRequired<FindUsedTypes>();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000047 }
Chris Lattner0e9f93e2002-08-31 00:29:16 +000048
49 virtual bool run(Module &M) {
50 // Initialize
51 Table = new SlotCalculator(&M, false);
52 TheModule = &M;
53
54 // Ensure that all structure types have names...
55 bool Changed = nameAllUsedStructureTypes(M);
56
57 // Run...
58 printModule(&M);
59
60 // Free memory...
61 delete Table;
62 TypeNames.clear();
63 MangledGlobals.clear();
64 return false;
65 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000066
Chris Lattner83c57752002-08-19 22:17:53 +000067 ostream &printType(const Type *Ty, const string &VariableName = "",
Vikram S. Adve969c4ad2002-08-25 20:00:08 +000068 bool IgnoreName = false, bool namedContext = true);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000069
Chris Lattnerbb03efd2002-06-25 15:57:03 +000070 void writeOperand(Value *Operand);
71 void writeOperandInternal(Value *Operand);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000072
73 string getValueName(const Value *V);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000074
Chris Lattner4fbf26d2002-05-09 20:53:56 +000075 private :
Chris Lattner0e9f93e2002-08-31 00:29:16 +000076 bool nameAllUsedStructureTypes(Module &M);
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +000077 void parseStruct(const Type *Ty);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +000078 void printModule(Module *M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000079 void printSymbolTable(const SymbolTable &ST);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000080 void printGlobal(const GlobalVariable *GV);
Chris Lattner4cda8352002-08-20 16:55:48 +000081 void printFunctionSignature(const Function *F, bool Prototype);
82
Chris Lattner8c3c4bf2002-05-09 15:49:41 +000083 void printFunction(Function *);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000084
Chris Lattner6d492922002-08-19 21:32:41 +000085 void printConstant(Constant *CPV);
86 void printConstantArray(ConstantArray *CPA);
87
Chris Lattnerd0c668c2002-05-09 21:18:38 +000088 // isInlinableInst - Attempt to inline instructions into their uses to build
89 // trees as much as possible. To do this, we have to consistently decide
90 // what is acceptable to inline, so that variable declarations don't get
91 // printed and an extra copy of the expr is not emitted.
92 //
Chris Lattnerbb03efd2002-06-25 15:57:03 +000093 static bool isInlinableInst(const Instruction &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +000094 // Must be an expression, must be used exactly once. If it is dead, we
95 // emit it inline where it would go.
Chris Lattnerbb03efd2002-06-25 15:57:03 +000096 if (I.getType() == Type::VoidTy || I.use_size() != 1 ||
Chris Lattnerd0c668c2002-05-09 21:18:38 +000097 isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I))
98 return false;
99
100 // Only inline instruction it it's use is in the same BB as the inst.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000101 return I.getParent() == cast<Instruction>(I.use_back())->getParent();
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000102 }
103
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000104 // Instruction visitation functions
105 friend class InstVisitor<CWriter>;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000106
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000107 void visitReturnInst(ReturnInst &I);
108 void visitBranchInst(BranchInst &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000109
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000110 void visitPHINode(PHINode &I) {}
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000111 void visitBinaryOperator(Instruction &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000112
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000113 void visitCastInst (CastInst &I);
114 void visitCallInst (CallInst &I);
115 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000116
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000117 void visitMallocInst(MallocInst &I);
118 void visitAllocaInst(AllocaInst &I);
119 void visitFreeInst (FreeInst &I);
120 void visitLoadInst (LoadInst &I);
121 void visitStoreInst (StoreInst &I);
122 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000123
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000124 void visitInstruction(Instruction &I) {
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000125 std::cerr << "C Writer does not know about " << I;
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000126 abort();
127 }
128
129 void outputLValue(Instruction *I) {
130 Out << " " << getValueName(I) << " = ";
131 }
132 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
133 unsigned Indent);
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000134 void printIndexingExpression(Value *Ptr, User::op_iterator I,
135 User::op_iterator E);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000136 };
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000137}
138
Chris Lattner2f499022002-05-09 04:21:21 +0000139// We dont want identifier names with ., space, - in them.
140// So we replace them with _
141static string makeNameProper(string x) {
142 string tmp;
143 for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
144 switch (*sI) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000145 case '.': tmp += "d_"; break;
146 case ' ': tmp += "s_"; break;
147 case '-': tmp += "D_"; break;
Chris Lattner2f499022002-05-09 04:21:21 +0000148 default: tmp += *sI;
149 }
150
151 return tmp;
152}
153
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000154string CWriter::getValueName(const Value *V) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000155 if (V->hasName()) { // Print out the label if it exists...
156 if (isa<GlobalValue>(V) && // Do not mangle globals...
157 cast<GlobalValue>(V)->hasExternalLinkage() && // Unless it's internal or
158 !MangledGlobals.count(V)) // Unless the name would collide if we don't
Chris Lattner2f499022002-05-09 04:21:21 +0000159 return makeNameProper(V->getName());
160
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000161 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
162 makeNameProper(V->getName());
Chris Lattner2f499022002-05-09 04:21:21 +0000163 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000164
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000165 int Slot = Table->getValSlot(V);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000166 assert(Slot >= 0 && "Invalid value!");
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000167 return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000168}
169
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000170// A pointer type should not use parens around *'s alone, e.g., (**)
171inline bool ptrTypeNameNeedsParens(const string &NameSoFar) {
172 return (NameSoFar.find_last_not_of('*') != std::string::npos);
173}
174
Chris Lattner83c57752002-08-19 22:17:53 +0000175// Pass the Type* and the variable name and this prints out the variable
176// declaration.
177//
178ostream &CWriter::printType(const Type *Ty, const string &NameSoFar,
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000179 bool IgnoreName, bool namedContext) {
Chris Lattner83c57752002-08-19 22:17:53 +0000180 if (Ty->isPrimitiveType())
181 switch (Ty->getPrimitiveID()) {
182 case Type::VoidTyID: return Out << "void " << NameSoFar;
183 case Type::BoolTyID: return Out << "bool " << NameSoFar;
184 case Type::UByteTyID: return Out << "unsigned char " << NameSoFar;
185 case Type::SByteTyID: return Out << "signed char " << NameSoFar;
186 case Type::UShortTyID: return Out << "unsigned short " << NameSoFar;
187 case Type::ShortTyID: return Out << "short " << NameSoFar;
188 case Type::UIntTyID: return Out << "unsigned " << NameSoFar;
189 case Type::IntTyID: return Out << "int " << NameSoFar;
190 case Type::ULongTyID: return Out << "unsigned long long " << NameSoFar;
191 case Type::LongTyID: return Out << "signed long long " << NameSoFar;
192 case Type::FloatTyID: return Out << "float " << NameSoFar;
193 case Type::DoubleTyID: return Out << "double " << NameSoFar;
194 default :
195 std::cerr << "Unknown primitive type: " << Ty << "\n";
196 abort();
197 }
198
199 // Check to see if the type is named.
200 if (!IgnoreName) {
201 map<const Type *, string>::iterator I = TypeNames.find(Ty);
202 if (I != TypeNames.end()) {
203 return Out << I->second << " " << NameSoFar;
204 }
205 }
206
Chris Lattner83c57752002-08-19 22:17:53 +0000207 switch (Ty->getPrimitiveID()) {
208 case Type::FunctionTyID: {
209 const FunctionType *MTy = cast<FunctionType>(Ty);
210 printType(MTy->getReturnType(), "");
211 Out << " " << NameSoFar << " (";
212
213 for (FunctionType::ParamTypes::const_iterator
214 I = MTy->getParamTypes().begin(),
215 E = MTy->getParamTypes().end(); I != E; ++I) {
216 if (I != MTy->getParamTypes().begin())
217 Out << ", ";
218 printType(*I, "");
219 }
220 if (MTy->isVarArg()) {
221 if (!MTy->getParamTypes().empty())
222 Out << ", ";
223 Out << "...";
224 }
225 return Out << ")";
226 }
227 case Type::StructTyID: {
228 const StructType *STy = cast<StructType>(Ty);
229 Out << NameSoFar + " {\n";
230 unsigned Idx = 0;
231 for (StructType::ElementTypes::const_iterator
232 I = STy->getElementTypes().begin(),
233 E = STy->getElementTypes().end(); I != E; ++I) {
234 Out << " ";
235 printType(*I, "field" + utostr(Idx++));
236 Out << ";\n";
237 }
238 return Out << "}";
239 }
240
241 case Type::PointerTyID: {
242 const PointerType *PTy = cast<PointerType>(Ty);
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000243 std::string ptrName = "*" + NameSoFar;
244
245 // Do not need parens around "* NameSoFar" if NameSoFar consists only
246 // of zero or more '*' chars *and* this is not an unnamed pointer type
247 // such as the result type in a cast statement. Otherwise, enclose in ( ).
248 if (ptrTypeNameNeedsParens(NameSoFar) || !namedContext)
249 ptrName = "(" + ptrName + ")"; //
250
Vikram S. Adve42eb2ba2002-08-24 14:44:23 +0000251 return printType(PTy->getElementType(), ptrName);
Chris Lattner83c57752002-08-19 22:17:53 +0000252 }
253
254 case Type::ArrayTyID: {
255 const ArrayType *ATy = cast<ArrayType>(Ty);
256 unsigned NumElements = ATy->getNumElements();
257 return printType(ATy->getElementType(),
258 NameSoFar + "[" + utostr(NumElements) + "]");
259 }
260 default:
261 assert(0 && "Unhandled case in getTypeProps!");
262 abort();
263 }
264
265 return Out;
266}
267
Chris Lattner6d492922002-08-19 21:32:41 +0000268void CWriter::printConstantArray(ConstantArray *CPA) {
269
270 // As a special case, print the array as a string if it is an array of
271 // ubytes or an array of sbytes with positive values.
272 //
273 const Type *ETy = CPA->getType()->getElementType();
274 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
275
276 // Make sure the last character is a null char, as automatically added by C
277 if (CPA->getNumOperands() == 0 ||
278 !cast<Constant>(*(CPA->op_end()-1))->isNullValue())
279 isString = false;
280
281 if (isString) {
282 Out << "\"";
283 // Do not include the last character, which we know is null
284 for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) {
285 unsigned char C = (ETy == Type::SByteTy) ?
286 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
287 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
288
289 if (isprint(C)) {
290 Out << C;
291 } else {
292 switch (C) {
293 case '\n': Out << "\\n"; break;
294 case '\t': Out << "\\t"; break;
295 case '\r': Out << "\\r"; break;
296 case '\v': Out << "\\v"; break;
297 case '\a': Out << "\\a"; break;
298 default:
299 Out << "\\x";
300 Out << ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
301 Out << ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
302 break;
303 }
304 }
305 }
306 Out << "\"";
307 } else {
308 Out << "{";
309 if (CPA->getNumOperands()) {
310 Out << " ";
311 printConstant(cast<Constant>(CPA->getOperand(0)));
312 for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
313 Out << ", ";
314 printConstant(cast<Constant>(CPA->getOperand(i)));
315 }
316 }
317 Out << " }";
318 }
319}
320
321
322// printConstant - The LLVM Constant to C Constant converter.
323void CWriter::printConstant(Constant *CPV) {
324 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
325 switch (CE->getOpcode()) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000326 case Instruction::Cast:
327 Out << "((";
328 printType(CPV->getType());
329 Out << ")";
330 printConstant(cast<Constant>(CPV->getOperand(0)));
331 Out << ")";
332 return;
333
334 case Instruction::GetElementPtr:
335 Out << "&(";
336 printIndexingExpression(CPV->getOperand(0),
337 CPV->op_begin()+1, CPV->op_end());
338 Out << ")";
339 return;
340 case Instruction::Add:
341 Out << "(";
342 printConstant(cast<Constant>(CPV->getOperand(0)));
343 Out << " + ";
344 printConstant(cast<Constant>(CPV->getOperand(1)));
345 Out << ")";
346 return;
347 case Instruction::Sub:
348 Out << "(";
349 printConstant(cast<Constant>(CPV->getOperand(0)));
350 Out << " - ";
351 printConstant(cast<Constant>(CPV->getOperand(1)));
352 Out << ")";
353 return;
354
Chris Lattner6d492922002-08-19 21:32:41 +0000355 default:
356 std::cerr << "CWriter Error: Unhandled constant expression: "
357 << CE << "\n";
358 abort();
359 }
360 }
361
362 switch (CPV->getType()->getPrimitiveID()) {
363 case Type::BoolTyID:
364 Out << (CPV == ConstantBool::False ? "0" : "1"); break;
365 case Type::SByteTyID:
366 case Type::ShortTyID:
367 case Type::IntTyID:
368 Out << cast<ConstantSInt>(CPV)->getValue(); break;
369 case Type::LongTyID:
370 Out << cast<ConstantSInt>(CPV)->getValue() << "ll"; break;
371
372 case Type::UByteTyID:
373 case Type::UShortTyID:
374 Out << cast<ConstantUInt>(CPV)->getValue(); break;
375 case Type::UIntTyID:
376 Out << cast<ConstantUInt>(CPV)->getValue() << "u"; break;
377 case Type::ULongTyID:
378 Out << cast<ConstantUInt>(CPV)->getValue() << "ull"; break;
379
380 case Type::FloatTyID:
381 case Type::DoubleTyID:
382 Out << cast<ConstantFP>(CPV)->getValue(); break;
383
384 case Type::ArrayTyID:
385 printConstantArray(cast<ConstantArray>(CPV));
386 break;
387
388 case Type::StructTyID: {
389 Out << "{";
390 if (CPV->getNumOperands()) {
391 Out << " ";
392 printConstant(cast<Constant>(CPV->getOperand(0)));
393 for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) {
394 Out << ", ";
395 printConstant(cast<Constant>(CPV->getOperand(i)));
396 }
397 }
398 Out << " }";
399 break;
400 }
401
402 case Type::PointerTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000403 if (isa<ConstantPointerNull>(CPV)) {
404 Out << "((";
Chris Lattner3af3ba82002-05-09 21:31:18 +0000405 printType(CPV->getType(), "");
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000406 Out << ")NULL)";
Chris Lattner6d492922002-08-19 21:32:41 +0000407 break;
408 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
409 writeOperand(CPR->getValue());
410 break;
411 }
412 // FALL THROUGH
413 default:
414 std::cerr << "Unknown constant type: " << CPV << "\n";
415 abort();
416 }
417}
418
419void CWriter::writeOperandInternal(Value *Operand) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000420 if (Instruction *I = dyn_cast<Instruction>(Operand))
421 if (isInlinableInst(*I)) {
422 // Should we inline this instruction to build a tree?
423 Out << "(";
424 visit(*I);
425 Out << ")";
426 return;
427 }
428
Chris Lattner6d492922002-08-19 21:32:41 +0000429 if (Operand->hasName()) {
430 Out << getValueName(Operand);
431 } else if (Constant *CPV = dyn_cast<Constant>(Operand)) {
432 printConstant(CPV);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000433 } else {
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000434 int Slot = Table->getValSlot(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000435 assert(Slot >= 0 && "Malformed LLVM!");
436 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
437 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000438}
439
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000440void CWriter::writeOperand(Value *Operand) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000441 if (isa<GlobalVariable>(Operand))
442 Out << "(&"; // Global variables are references as their addresses by llvm
443
444 writeOperandInternal(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000445
446 if (isa<GlobalVariable>(Operand))
447 Out << ")";
448}
449
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000450// nameAllUsedStructureTypes - If there are structure types in the module that
451// are used but do not have names assigned to them in the symbol table yet then
452// we assign them names now.
453//
454bool CWriter::nameAllUsedStructureTypes(Module &M) {
455 // Get a set of types that are used by the program...
456 std::set<const Type *> UT = getAnalysis<FindUsedTypes>().getTypes();
457
458 // Loop over the module symbol table, removing types from UT that are already
459 // named.
460 //
461 SymbolTable *MST = M.getSymbolTableSure();
462 if (MST->find(Type::TypeTy) != MST->end())
463 for (SymbolTable::type_iterator I = MST->type_begin(Type::TypeTy),
464 E = MST->type_end(Type::TypeTy); I != E; ++I)
465 UT.erase(cast<Type>(I->second));
466
467 // UT now contains types that are not named. Loop over it, naming structure
468 // types.
469 //
470 bool Changed = false;
471 for (std::set<const Type *>::const_iterator I = UT.begin(), E = UT.end();
472 I != E; ++I)
473 if (const StructType *ST = dyn_cast<StructType>(*I)) {
474 ((Value*)ST)->setName("unnamed", MST);
475 Changed = true;
476 }
477 return Changed;
478}
479
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000480void CWriter::printModule(Module *M) {
Chris Lattner78771c82002-05-17 04:55:35 +0000481 // Calculate which global values have names that will collide when we throw
482 // away type information.
Chris Lattner594b9fa2002-05-21 21:10:04 +0000483 { // Scope to delete the FoundNames set when we are done with it...
Chris Lattner78771c82002-05-17 04:55:35 +0000484 std::set<string> FoundNames;
485 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000486 if (I->hasName()) // If the global has a name...
487 if (FoundNames.count(I->getName())) // And the name is already used
488 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000489 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000490 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000491
492 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000493 if (I->hasName()) // If the global has a name...
494 if (FoundNames.count(I->getName())) // And the name is already used
495 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000496 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000497 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000498 }
499
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000500 // printing stdlib inclusion
501 // Out << "#include <stdlib.h>\n";
502
Chris Lattner16c7bb22002-05-09 02:28:59 +0000503 // get declaration for alloca
504 Out << "/* Provide Declarations */\n"
Chris Lattner594b9fa2002-05-21 21:10:04 +0000505 << "#include <malloc.h>\n"
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000506 << "#include <alloca.h>\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000507
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000508 // Provide a definition for null if one does not already exist,
509 // and for `bool' if not compiling with a C++ compiler.
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000510 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000511 << "#ifndef __cplusplus\ntypedef unsigned char bool;\n#endif\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000512
Chris Lattnera4d4a852002-08-19 21:48:40 +0000513 << "\n\n/* Global Declarations */\n";
514
515 // First output all the declarations for the program, because C requires
516 // Functions & globals to be declared before they are used.
517 //
Chris Lattner16c7bb22002-05-09 02:28:59 +0000518
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000519 // Loop over the symbol table, emitting all named constants...
520 if (M->hasSymbolTable())
521 printSymbolTable(*M->getSymbolTable());
522
Chris Lattnera4d4a852002-08-19 21:48:40 +0000523 // Global variable declarations...
524 if (!M->gempty()) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000525 Out << "\n/* External Global Variable Declarations */\n";
Chris Lattnera4d4a852002-08-19 21:48:40 +0000526 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000527 if (I->hasExternalLinkage()) {
528 Out << "extern ";
529 printType(I->getType()->getElementType(), getValueName(I));
530 Out << ";\n";
531 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000532 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000533 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000534
Chris Lattnera4d4a852002-08-19 21:48:40 +0000535 // Function declarations
536 if (!M->empty()) {
537 Out << "\n/* Function Declarations */\n";
Chris Lattner4cda8352002-08-20 16:55:48 +0000538 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
539 printFunctionSignature(I, true);
540 Out << ";\n";
541 }
Chris Lattnera4d4a852002-08-19 21:48:40 +0000542 }
543
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000544 // Output the global variable definitions and contents...
Chris Lattnera4d4a852002-08-19 21:48:40 +0000545 if (!M->gempty()) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000546 Out << "\n\n/* Global Variable Definitions and Initialization */\n";
Chris Lattnera4d4a852002-08-19 21:48:40 +0000547 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000548 if (I->hasExternalLinkage())
549 continue; // printed above!
550 Out << "static ";
Chris Lattnera4d4a852002-08-19 21:48:40 +0000551 printType(I->getType()->getElementType(), getValueName(I));
552
553 if (I->hasInitializer()) {
554 Out << " = " ;
555 writeOperand(I->getInitializer());
556 }
557 Out << ";\n";
558 }
559 }
560
Chris Lattner16c7bb22002-05-09 02:28:59 +0000561 // Output all of the functions...
Chris Lattnera4d4a852002-08-19 21:48:40 +0000562 if (!M->empty()) {
563 Out << "\n\n/* Function Bodies */\n";
564 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
565 printFunction(I);
566 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000567}
568
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000569
Chris Lattnera4c047e2002-09-20 14:56:54 +0000570/// printSymbolTable - Run through symbol table looking for named constants
571/// if a named constant is found, emit it's declaration...
572/// Assuming that symbol table has only types and constants.
573///
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000574void CWriter::printSymbolTable(const SymbolTable &ST) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000575 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
Chris Lattnera4c047e2002-09-20 14:56:54 +0000576 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000577 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
578
Chris Lattnera4c047e2002-09-20 14:56:54 +0000579 for (; I != End; ++I) {
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000580 const Value *V = I->second;
Chris Lattnera4c047e2002-09-20 14:56:54 +0000581 if (const Type *Ty = dyn_cast<Type>(V))
582 if (const Type *STy = dyn_cast<StructType>(Ty)) {
583 string Name = "struct l_" + makeNameProper(I->first);
584 Out << Name << ";\n";
585 TypeNames.insert(std::make_pair(STy, Name));
586 } else {
587 string Name = "l_" + makeNameProper(I->first);
588 Out << "typedef ";
589 printType(Ty, Name, true);
590 Out << ";\n";
591 }
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000592 }
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000593 }
594
595 Out << "\n";
596
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000597 // Loop over all structures then push them into the stack so they are
598 // printed in the correct order.
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000599 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
600 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
601 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
602
Chris Lattnera4c047e2002-09-20 14:56:54 +0000603 for (; I != End; ++I)
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000604 if (const StructType *STy = dyn_cast<StructType>(I->second))
Chris Lattnera4c047e2002-09-20 14:56:54 +0000605 parseStruct(STy);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000606 }
607}
608
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000609// Push the struct onto the stack and recursively push all structs
610// this one depends on.
611void CWriter::parseStruct(const Type *Ty) {
612 if (const StructType *STy = dyn_cast<StructType>(Ty)){
613 //Check to see if we have already printed this struct
614 if (StructPrinted.find(STy) == StructPrinted.end()){
Chris Lattnera4c047e2002-09-20 14:56:54 +0000615 for (StructType::ElementTypes::const_iterator
616 I = STy->getElementTypes().begin(),
617 E = STy->getElementTypes().end(); I != E; ++I) {
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000618 const Type *Ty1 = dyn_cast<Type>(I->get());
Chris Lattnera4c047e2002-09-20 14:56:54 +0000619 if (isa<StructType>(Ty1) || isa<ArrayType>(Ty1))
620 parseStruct(Ty1);
621 }
622
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000623 //Print struct
624 StructPrinted.insert(STy);
625 string Name = TypeNames[STy];
626 printType(STy, Name, true);
627 Out << ";\n";
628 }
629 }
Chris Lattnera4c047e2002-09-20 14:56:54 +0000630
631 // If it is an array, check it's type and continue
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000632 else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)){
633 const Type *Ty1 = ATy->getElementType();
634 if (isa<StructType>(Ty1) || isa<ArrayType>(Ty1))
635 parseStruct(Ty1);
636 }
637}
638
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000639
Chris Lattner4cda8352002-08-20 16:55:48 +0000640void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000641 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000642
643 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000644 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000645
Chris Lattner16c7bb22002-05-09 02:28:59 +0000646 // Print out the return type and name...
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000647 printType(F->getReturnType());
Chris Lattner1f02c892002-05-09 20:14:10 +0000648 Out << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000649
Chris Lattner16c7bb22002-05-09 02:28:59 +0000650 if (!F->isExternal()) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000651 if (!F->aempty()) {
Chris Lattner4cda8352002-08-20 16:55:48 +0000652 string ArgName;
653 if (F->abegin()->hasName() || !Prototype)
654 ArgName = getValueName(F->abegin());
655
656 printType(F->afront().getType(), ArgName);
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000657
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000658 for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
659 I != E; ++I) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000660 Out << ", ";
Chris Lattner4cda8352002-08-20 16:55:48 +0000661 if (I->hasName() || !Prototype)
662 ArgName = getValueName(I);
663 else
664 ArgName = "";
665 printType(I->getType(), ArgName);
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000666 }
667 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000668 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000669 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000670 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000671 FT->getParamTypes().begin(),
672 E = FT->getParamTypes().end(); I != E; ++I) {
673 if (I != FT->getParamTypes().begin()) Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000674 printType(*I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000675 }
676 }
677
678 // Finish printing arguments...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000679 if (FT->isVarArg()) {
680 if (FT->getParamTypes().size()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000681 Out << "..."; // Output varargs portion of signature!
682 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000683 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000684}
685
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000686
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000687void CWriter::printFunction(Function *F) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000688 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000689
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000690 Table->incorporateFunction(F);
Chris Lattnerf34ee812002-05-09 03:12:34 +0000691
Chris Lattner4cda8352002-08-20 16:55:48 +0000692 printFunctionSignature(F, false);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000693 Out << " {\n";
694
Chris Lattner497e19a2002-05-09 20:39:03 +0000695 // print local variable information for the function
Chris Lattner7683a122002-05-09 20:33:35 +0000696 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000697 if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000698 Out << " ";
Chris Lattner3af3ba82002-05-09 21:31:18 +0000699 printType((*I)->getType(), getValueName(*I));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000700 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000701 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000702
703 // print the basic blocks
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000704 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
705 BasicBlock *Prev = BB->getPrev();
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000706
707 // Don't print the label for the basic block if there are no uses, or if the
708 // only terminator use is the precessor basic block's terminator. We have
709 // to scan the use list because PHI nodes use basic blocks too but do not
710 // require a label to be generated.
711 //
712 bool NeedsLabel = false;
713 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
714 UI != UE; ++UI)
715 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
716 if (TI != Prev->getTerminator()) {
717 NeedsLabel = true;
718 break;
719 }
720
721 if (NeedsLabel) Out << getValueName(BB) << ":\n";
722
723 // Output all of the instructions in the basic block...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000724 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II){
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000725 if (!isInlinableInst(*II) && !isa<PHINode>(*II)) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000726 if (II->getType() != Type::VoidTy)
727 outputLValue(II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000728 else
729 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000730 visit(*II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000731 Out << ";\n";
732 }
733 }
734
735 // Don't emit prefix or suffix for the terminator...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000736 visit(*BB->getTerminator());
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000737 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000738
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000739 Out << "}\n\n";
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000740 Table->purgeFunction();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000741}
742
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000743// Specific Instruction type classes... note that all of the casts are
744// neccesary because we use the instruction classes as opaque types...
745//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000746void CWriter::visitReturnInst(ReturnInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000747 // Don't output a void return if this is the last basic block in the function
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000748 if (I.getNumOperands() == 0 &&
749 &*--I.getParent()->getParent()->end() == I.getParent() &&
750 !I.getParent()->size() == 1) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000751 return;
Chris Lattner963b70b2002-05-21 18:05:19 +0000752 }
Chris Lattner44408262002-05-09 03:50:42 +0000753
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000754 Out << " return";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000755 if (I.getNumOperands()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000756 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000757 writeOperand(I.getOperand(0));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000758 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000759 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000760}
761
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000762static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
763 // If PHI nodes need copies, we need the copy code...
764 if (isa<PHINode>(To->front()) ||
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000765 From->getNext() != To) // Not directly successor, need goto
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000766 return true;
767
768 // Otherwise we don't need the code.
769 return false;
770}
771
772void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
773 unsigned Indent) {
774 for (BasicBlock::iterator I = Succ->begin();
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000775 PHINode *PN = dyn_cast<PHINode>(&*I); ++I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000776 // now we have to do the printing
777 Out << string(Indent, ' ');
778 outputLValue(PN);
779 writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
780 Out << "; /* for PHI node */\n";
781 }
782
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000783 if (CurBB->getNext() != Succ) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000784 Out << string(Indent, ' ') << " goto ";
785 writeOperand(Succ);
786 Out << ";\n";
787 }
788}
789
790// Brach instruction printing - Avoid printing out a brach to a basic block that
791// immediately succeeds the current one.
792//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000793void CWriter::visitBranchInst(BranchInst &I) {
794 if (I.isConditional()) {
795 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(0))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000796 Out << " if (";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000797 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000798 Out << ") {\n";
799
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000800 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000801
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000802 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(1))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000803 Out << " } else {\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000804 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000805 }
806 } else {
807 // First goto not neccesary, assume second one is...
808 Out << " if (!";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000809 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000810 Out << ") {\n";
811
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000812 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000813 }
814
815 Out << " }\n";
816 } else {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000817 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000818 }
819 Out << "\n";
820}
821
822
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000823void CWriter::visitBinaryOperator(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000824 // binary instructions, shift instructions, setCond instructions.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000825 if (isa<PointerType>(I.getType())) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000826 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000827 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000828 Out << ")";
829 }
830
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000831 if (isa<PointerType>(I.getType())) Out << "(long long)";
832 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000833
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000834 switch (I.getOpcode()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000835 case Instruction::Add: Out << " + "; break;
836 case Instruction::Sub: Out << " - "; break;
837 case Instruction::Mul: Out << "*"; break;
838 case Instruction::Div: Out << "/"; break;
839 case Instruction::Rem: Out << "%"; break;
840 case Instruction::And: Out << " & "; break;
841 case Instruction::Or: Out << " | "; break;
842 case Instruction::Xor: Out << " ^ "; break;
843 case Instruction::SetEQ: Out << " == "; break;
844 case Instruction::SetNE: Out << " != "; break;
845 case Instruction::SetLE: Out << " <= "; break;
846 case Instruction::SetGE: Out << " >= "; break;
847 case Instruction::SetLT: Out << " < "; break;
848 case Instruction::SetGT: Out << " > "; break;
849 case Instruction::Shl : Out << " << "; break;
850 case Instruction::Shr : Out << " >> "; break;
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000851 default: std::cerr << "Invalid operator type!" << I; abort();
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000852 }
853
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000854 if (isa<PointerType>(I.getType())) Out << "(long long)";
855 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000856}
857
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000858void CWriter::visitCastInst(CastInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000859 Out << "(";
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000860 printType(I.getType(), string(""),/*ignoreName*/false, /*namedContext*/false);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000861 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000862 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000863}
864
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000865void CWriter::visitCallInst(CallInst &I) {
866 const PointerType *PTy = cast<PointerType>(I.getCalledValue()->getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000867 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
868 const Type *RetTy = FTy->getReturnType();
869
Chris Lattnerfabc8802002-08-26 20:50:09 +0000870 writeOperand(I.getOperand(0));
871 Out << "(";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000872
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000873 if (I.getNumOperands() > 1) {
874 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000875
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000876 for (unsigned op = 2, Eop = I.getNumOperands(); op != Eop; ++op) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000877 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000878 writeOperand(I.getOperand(op));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000879 }
880 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000881 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000882}
883
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000884void CWriter::visitMallocInst(MallocInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000885 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000886 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000887 Out << ")malloc(sizeof(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000888 printType(I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000889 Out << ")";
890
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000891 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000892 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000893 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000894 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000895 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000896}
897
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000898void CWriter::visitAllocaInst(AllocaInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000899 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000900 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000901 Out << ") alloca(sizeof(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000902 printType(I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000903 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000904 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000905 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000906 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000907 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000908 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000909}
910
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000911void CWriter::visitFreeInst(FreeInst &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000912 Out << "free(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000913 writeOperand(I.getOperand(0));
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000914 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000915}
916
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000917void CWriter::printIndexingExpression(Value *Ptr, User::op_iterator I,
918 User::op_iterator E) {
919 bool HasImplicitAddress = false;
920 // If accessing a global value with no indexing, avoid *(&GV) syndrome
921 if (GlobalValue *V = dyn_cast<GlobalValue>(Ptr)) {
922 HasImplicitAddress = true;
923 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Ptr)) {
924 HasImplicitAddress = true;
925 Ptr = CPR->getValue(); // Get to the global...
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000926 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000927
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000928 if (I == E) {
929 if (!HasImplicitAddress)
930 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000931
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000932 writeOperandInternal(Ptr);
933 return;
934 }
935
936 const Constant *CI = dyn_cast<Constant>(I->get());
937 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
938 Out << "(&";
939
940 writeOperandInternal(Ptr);
941
942 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
943 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000944
945 // Print out the -> operator if possible...
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000946 if (CI && CI->isNullValue() && I+1 != E) {
947 if ((*(I+1))->getType() == Type::UByteTy) {
948 Out << (HasImplicitAddress ? "." : "->");
949 Out << "field" << cast<ConstantUInt>(*(I+1))->getValue();
950 I += 2;
Vikram S. Adve42eb2ba2002-08-24 14:44:23 +0000951 } else { // First array index of 0: Just skip it
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000952 ++I;
953 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000954 }
Chris Lattner5dfe7672002-08-22 22:48:55 +0000955
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000956 for (; I != E; ++I)
Chris Lattner106ff452002-09-11 01:21:29 +0000957 if ((*I)->getType() == Type::LongTy) {
Vikram S. Adve9d6f13f2002-09-15 21:51:04 +0000958 Out << "[";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000959 writeOperand(*I);
Vikram S. Adve9d6f13f2002-09-15 21:51:04 +0000960 Out << "]";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000961 } else {
Chris Lattner59afbf32002-09-12 20:34:47 +0000962 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000963 }
964}
965
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000966void CWriter::visitLoadInst(LoadInst &I) {
Chris Lattner5dfe7672002-08-22 22:48:55 +0000967 Out << "*";
968 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000969}
970
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000971void CWriter::visitStoreInst(StoreInst &I) {
Chris Lattner5dfe7672002-08-22 22:48:55 +0000972 Out << "*";
973 writeOperand(I.getPointerOperand());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000974 Out << " = ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000975 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000976}
977
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000978void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000979 Out << "&";
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000980 printIndexingExpression(I.getPointerOperand(), I.idx_begin(), I.idx_end());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000981}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000982
983//===----------------------------------------------------------------------===//
984// External Interface declaration
985//===----------------------------------------------------------------------===//
986
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000987Pass *createWriteToCPass(std::ostream &o) { return new CWriter(o); }