blob: e993b740f412f620b9fe79f21aecffbbae6afd85 [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
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000040 public:
Chris Lattner0e9f93e2002-08-31 00:29:16 +000041 CWriter(ostream &o) : Out(o) {}
42
43 void getAnalysisUsage(AnalysisUsage &AU) const {
44 AU.setPreservesAll();
45 AU.addRequired<FindUsedTypes>();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000046 }
Chris Lattner0e9f93e2002-08-31 00:29:16 +000047
48 virtual bool run(Module &M) {
49 // Initialize
50 Table = new SlotCalculator(&M, false);
51 TheModule = &M;
52
53 // Ensure that all structure types have names...
54 bool Changed = nameAllUsedStructureTypes(M);
55
56 // Run...
57 printModule(&M);
58
59 // Free memory...
60 delete Table;
61 TypeNames.clear();
62 MangledGlobals.clear();
63 return false;
64 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000065
Chris Lattner83c57752002-08-19 22:17:53 +000066 ostream &printType(const Type *Ty, const string &VariableName = "",
Vikram S. Adve969c4ad2002-08-25 20:00:08 +000067 bool IgnoreName = false, bool namedContext = true);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000068
Chris Lattnerbb03efd2002-06-25 15:57:03 +000069 void writeOperand(Value *Operand);
70 void writeOperandInternal(Value *Operand);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000071
72 string getValueName(const Value *V);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000073
Chris Lattner4fbf26d2002-05-09 20:53:56 +000074 private :
Chris Lattner0e9f93e2002-08-31 00:29:16 +000075 bool nameAllUsedStructureTypes(Module &M);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +000076 void printModule(Module *M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000077 void printSymbolTable(const SymbolTable &ST);
Chris Lattner2c601a72002-09-20 15:05:40 +000078 void printContainedStructs(const Type *Ty, std::set<const StructType *> &);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000079 void printGlobal(const GlobalVariable *GV);
Chris Lattner4cda8352002-08-20 16:55:48 +000080 void printFunctionSignature(const Function *F, bool Prototype);
81
Chris Lattner8c3c4bf2002-05-09 15:49:41 +000082 void printFunction(Function *);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000083
Chris Lattner6d492922002-08-19 21:32:41 +000084 void printConstant(Constant *CPV);
85 void printConstantArray(ConstantArray *CPA);
86
Chris Lattnerd0c668c2002-05-09 21:18:38 +000087 // isInlinableInst - Attempt to inline instructions into their uses to build
88 // trees as much as possible. To do this, we have to consistently decide
89 // what is acceptable to inline, so that variable declarations don't get
90 // printed and an extra copy of the expr is not emitted.
91 //
Chris Lattnerbb03efd2002-06-25 15:57:03 +000092 static bool isInlinableInst(const Instruction &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +000093 // Must be an expression, must be used exactly once. If it is dead, we
94 // emit it inline where it would go.
Chris Lattnerbb03efd2002-06-25 15:57:03 +000095 if (I.getType() == Type::VoidTy || I.use_size() != 1 ||
Chris Lattnerd0c668c2002-05-09 21:18:38 +000096 isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I))
97 return false;
98
99 // Only inline instruction it it's use is in the same BB as the inst.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000100 return I.getParent() == cast<Instruction>(I.use_back())->getParent();
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000101 }
102
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000103 // Instruction visitation functions
104 friend class InstVisitor<CWriter>;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000105
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000106 void visitReturnInst(ReturnInst &I);
107 void visitBranchInst(BranchInst &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000108
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000109 void visitPHINode(PHINode &I) {}
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000110 void visitBinaryOperator(Instruction &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000111
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000112 void visitCastInst (CastInst &I);
113 void visitCallInst (CallInst &I);
114 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000115
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000116 void visitMallocInst(MallocInst &I);
117 void visitAllocaInst(AllocaInst &I);
118 void visitFreeInst (FreeInst &I);
119 void visitLoadInst (LoadInst &I);
120 void visitStoreInst (StoreInst &I);
121 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000122
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000123 void visitInstruction(Instruction &I) {
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000124 std::cerr << "C Writer does not know about " << I;
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000125 abort();
126 }
127
128 void outputLValue(Instruction *I) {
129 Out << " " << getValueName(I) << " = ";
130 }
131 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
132 unsigned Indent);
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000133 void printIndexingExpression(Value *Ptr, User::op_iterator I,
134 User::op_iterator E);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000135 };
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000136}
137
Chris Lattner2f499022002-05-09 04:21:21 +0000138// We dont want identifier names with ., space, - in them.
139// So we replace them with _
140static string makeNameProper(string x) {
141 string tmp;
142 for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
143 switch (*sI) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000144 case '.': tmp += "d_"; break;
145 case ' ': tmp += "s_"; break;
146 case '-': tmp += "D_"; break;
Chris Lattner2f499022002-05-09 04:21:21 +0000147 default: tmp += *sI;
148 }
149
150 return tmp;
151}
152
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000153string CWriter::getValueName(const Value *V) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000154 if (V->hasName()) { // Print out the label if it exists...
155 if (isa<GlobalValue>(V) && // Do not mangle globals...
156 cast<GlobalValue>(V)->hasExternalLinkage() && // Unless it's internal or
157 !MangledGlobals.count(V)) // Unless the name would collide if we don't
Chris Lattner2f499022002-05-09 04:21:21 +0000158 return makeNameProper(V->getName());
159
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000160 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
161 makeNameProper(V->getName());
Chris Lattner2f499022002-05-09 04:21:21 +0000162 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000163
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000164 int Slot = Table->getValSlot(V);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000165 assert(Slot >= 0 && "Invalid value!");
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000166 return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000167}
168
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000169// A pointer type should not use parens around *'s alone, e.g., (**)
170inline bool ptrTypeNameNeedsParens(const string &NameSoFar) {
171 return (NameSoFar.find_last_not_of('*') != std::string::npos);
172}
173
Chris Lattner83c57752002-08-19 22:17:53 +0000174// Pass the Type* and the variable name and this prints out the variable
175// declaration.
176//
177ostream &CWriter::printType(const Type *Ty, const string &NameSoFar,
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000178 bool IgnoreName, bool namedContext) {
Chris Lattner83c57752002-08-19 22:17:53 +0000179 if (Ty->isPrimitiveType())
180 switch (Ty->getPrimitiveID()) {
181 case Type::VoidTyID: return Out << "void " << NameSoFar;
182 case Type::BoolTyID: return Out << "bool " << NameSoFar;
183 case Type::UByteTyID: return Out << "unsigned char " << NameSoFar;
184 case Type::SByteTyID: return Out << "signed char " << NameSoFar;
185 case Type::UShortTyID: return Out << "unsigned short " << NameSoFar;
186 case Type::ShortTyID: return Out << "short " << NameSoFar;
187 case Type::UIntTyID: return Out << "unsigned " << NameSoFar;
188 case Type::IntTyID: return Out << "int " << NameSoFar;
189 case Type::ULongTyID: return Out << "unsigned long long " << NameSoFar;
190 case Type::LongTyID: return Out << "signed long long " << NameSoFar;
191 case Type::FloatTyID: return Out << "float " << NameSoFar;
192 case Type::DoubleTyID: return Out << "double " << NameSoFar;
193 default :
194 std::cerr << "Unknown primitive type: " << Ty << "\n";
195 abort();
196 }
197
198 // Check to see if the type is named.
199 if (!IgnoreName) {
200 map<const Type *, string>::iterator I = TypeNames.find(Ty);
201 if (I != TypeNames.end()) {
202 return Out << I->second << " " << NameSoFar;
203 }
204 }
205
Chris Lattner83c57752002-08-19 22:17:53 +0000206 switch (Ty->getPrimitiveID()) {
207 case Type::FunctionTyID: {
208 const FunctionType *MTy = cast<FunctionType>(Ty);
209 printType(MTy->getReturnType(), "");
210 Out << " " << NameSoFar << " (";
211
212 for (FunctionType::ParamTypes::const_iterator
213 I = MTy->getParamTypes().begin(),
214 E = MTy->getParamTypes().end(); I != E; ++I) {
215 if (I != MTy->getParamTypes().begin())
216 Out << ", ";
217 printType(*I, "");
218 }
219 if (MTy->isVarArg()) {
220 if (!MTy->getParamTypes().empty())
221 Out << ", ";
222 Out << "...";
223 }
224 return Out << ")";
225 }
226 case Type::StructTyID: {
227 const StructType *STy = cast<StructType>(Ty);
228 Out << NameSoFar + " {\n";
229 unsigned Idx = 0;
230 for (StructType::ElementTypes::const_iterator
231 I = STy->getElementTypes().begin(),
232 E = STy->getElementTypes().end(); I != E; ++I) {
233 Out << " ";
234 printType(*I, "field" + utostr(Idx++));
235 Out << ";\n";
236 }
237 return Out << "}";
238 }
239
240 case Type::PointerTyID: {
241 const PointerType *PTy = cast<PointerType>(Ty);
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000242 std::string ptrName = "*" + NameSoFar;
243
244 // Do not need parens around "* NameSoFar" if NameSoFar consists only
245 // of zero or more '*' chars *and* this is not an unnamed pointer type
246 // such as the result type in a cast statement. Otherwise, enclose in ( ).
247 if (ptrTypeNameNeedsParens(NameSoFar) || !namedContext)
248 ptrName = "(" + ptrName + ")"; //
249
Vikram S. Adve42eb2ba2002-08-24 14:44:23 +0000250 return printType(PTy->getElementType(), ptrName);
Chris Lattner83c57752002-08-19 22:17:53 +0000251 }
252
253 case Type::ArrayTyID: {
254 const ArrayType *ATy = cast<ArrayType>(Ty);
255 unsigned NumElements = ATy->getNumElements();
256 return printType(ATy->getElementType(),
257 NameSoFar + "[" + utostr(NumElements) + "]");
258 }
259 default:
260 assert(0 && "Unhandled case in getTypeProps!");
261 abort();
262 }
263
264 return Out;
265}
266
Chris Lattner6d492922002-08-19 21:32:41 +0000267void CWriter::printConstantArray(ConstantArray *CPA) {
268
269 // As a special case, print the array as a string if it is an array of
270 // ubytes or an array of sbytes with positive values.
271 //
272 const Type *ETy = CPA->getType()->getElementType();
273 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
274
275 // Make sure the last character is a null char, as automatically added by C
276 if (CPA->getNumOperands() == 0 ||
277 !cast<Constant>(*(CPA->op_end()-1))->isNullValue())
278 isString = false;
279
280 if (isString) {
281 Out << "\"";
282 // Do not include the last character, which we know is null
283 for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) {
284 unsigned char C = (ETy == Type::SByteTy) ?
285 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
286 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
287
288 if (isprint(C)) {
289 Out << C;
290 } else {
291 switch (C) {
292 case '\n': Out << "\\n"; break;
293 case '\t': Out << "\\t"; break;
294 case '\r': Out << "\\r"; break;
295 case '\v': Out << "\\v"; break;
296 case '\a': Out << "\\a"; break;
297 default:
298 Out << "\\x";
299 Out << ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
300 Out << ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
301 break;
302 }
303 }
304 }
305 Out << "\"";
306 } else {
307 Out << "{";
308 if (CPA->getNumOperands()) {
309 Out << " ";
310 printConstant(cast<Constant>(CPA->getOperand(0)));
311 for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
312 Out << ", ";
313 printConstant(cast<Constant>(CPA->getOperand(i)));
314 }
315 }
316 Out << " }";
317 }
318}
319
320
321// printConstant - The LLVM Constant to C Constant converter.
322void CWriter::printConstant(Constant *CPV) {
323 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
324 switch (CE->getOpcode()) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000325 case Instruction::Cast:
326 Out << "((";
327 printType(CPV->getType());
328 Out << ")";
329 printConstant(cast<Constant>(CPV->getOperand(0)));
330 Out << ")";
331 return;
332
333 case Instruction::GetElementPtr:
334 Out << "&(";
335 printIndexingExpression(CPV->getOperand(0),
336 CPV->op_begin()+1, CPV->op_end());
337 Out << ")";
338 return;
339 case Instruction::Add:
340 Out << "(";
341 printConstant(cast<Constant>(CPV->getOperand(0)));
342 Out << " + ";
343 printConstant(cast<Constant>(CPV->getOperand(1)));
344 Out << ")";
345 return;
346 case Instruction::Sub:
347 Out << "(";
348 printConstant(cast<Constant>(CPV->getOperand(0)));
349 Out << " - ";
350 printConstant(cast<Constant>(CPV->getOperand(1)));
351 Out << ")";
352 return;
353
Chris Lattner6d492922002-08-19 21:32:41 +0000354 default:
355 std::cerr << "CWriter Error: Unhandled constant expression: "
356 << CE << "\n";
357 abort();
358 }
359 }
360
361 switch (CPV->getType()->getPrimitiveID()) {
362 case Type::BoolTyID:
363 Out << (CPV == ConstantBool::False ? "0" : "1"); break;
364 case Type::SByteTyID:
365 case Type::ShortTyID:
366 case Type::IntTyID:
367 Out << cast<ConstantSInt>(CPV)->getValue(); break;
368 case Type::LongTyID:
369 Out << cast<ConstantSInt>(CPV)->getValue() << "ll"; break;
370
371 case Type::UByteTyID:
372 case Type::UShortTyID:
373 Out << cast<ConstantUInt>(CPV)->getValue(); break;
374 case Type::UIntTyID:
375 Out << cast<ConstantUInt>(CPV)->getValue() << "u"; break;
376 case Type::ULongTyID:
377 Out << cast<ConstantUInt>(CPV)->getValue() << "ull"; break;
378
379 case Type::FloatTyID:
380 case Type::DoubleTyID:
381 Out << cast<ConstantFP>(CPV)->getValue(); break;
382
383 case Type::ArrayTyID:
384 printConstantArray(cast<ConstantArray>(CPV));
385 break;
386
387 case Type::StructTyID: {
388 Out << "{";
389 if (CPV->getNumOperands()) {
390 Out << " ";
391 printConstant(cast<Constant>(CPV->getOperand(0)));
392 for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) {
393 Out << ", ";
394 printConstant(cast<Constant>(CPV->getOperand(i)));
395 }
396 }
397 Out << " }";
398 break;
399 }
400
401 case Type::PointerTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000402 if (isa<ConstantPointerNull>(CPV)) {
403 Out << "((";
Chris Lattner3af3ba82002-05-09 21:31:18 +0000404 printType(CPV->getType(), "");
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000405 Out << ")NULL)";
Chris Lattner6d492922002-08-19 21:32:41 +0000406 break;
407 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
408 writeOperand(CPR->getValue());
409 break;
410 }
411 // FALL THROUGH
412 default:
413 std::cerr << "Unknown constant type: " << CPV << "\n";
414 abort();
415 }
416}
417
418void CWriter::writeOperandInternal(Value *Operand) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000419 if (Instruction *I = dyn_cast<Instruction>(Operand))
420 if (isInlinableInst(*I)) {
421 // Should we inline this instruction to build a tree?
422 Out << "(";
423 visit(*I);
424 Out << ")";
425 return;
426 }
427
Chris Lattner6d492922002-08-19 21:32:41 +0000428 if (Operand->hasName()) {
429 Out << getValueName(Operand);
430 } else if (Constant *CPV = dyn_cast<Constant>(Operand)) {
431 printConstant(CPV);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000432 } else {
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000433 int Slot = Table->getValSlot(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000434 assert(Slot >= 0 && "Malformed LLVM!");
435 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
436 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000437}
438
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000439void CWriter::writeOperand(Value *Operand) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000440 if (isa<GlobalVariable>(Operand))
441 Out << "(&"; // Global variables are references as their addresses by llvm
442
443 writeOperandInternal(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000444
445 if (isa<GlobalVariable>(Operand))
446 Out << ")";
447}
448
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000449// nameAllUsedStructureTypes - If there are structure types in the module that
450// are used but do not have names assigned to them in the symbol table yet then
451// we assign them names now.
452//
453bool CWriter::nameAllUsedStructureTypes(Module &M) {
454 // Get a set of types that are used by the program...
455 std::set<const Type *> UT = getAnalysis<FindUsedTypes>().getTypes();
456
457 // Loop over the module symbol table, removing types from UT that are already
458 // named.
459 //
460 SymbolTable *MST = M.getSymbolTableSure();
461 if (MST->find(Type::TypeTy) != MST->end())
462 for (SymbolTable::type_iterator I = MST->type_begin(Type::TypeTy),
463 E = MST->type_end(Type::TypeTy); I != E; ++I)
464 UT.erase(cast<Type>(I->second));
465
466 // UT now contains types that are not named. Loop over it, naming structure
467 // types.
468 //
469 bool Changed = false;
470 for (std::set<const Type *>::const_iterator I = UT.begin(), E = UT.end();
471 I != E; ++I)
472 if (const StructType *ST = dyn_cast<StructType>(*I)) {
473 ((Value*)ST)->setName("unnamed", MST);
474 Changed = true;
475 }
476 return Changed;
477}
478
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000479void CWriter::printModule(Module *M) {
Chris Lattner78771c82002-05-17 04:55:35 +0000480 // Calculate which global values have names that will collide when we throw
481 // away type information.
Chris Lattner594b9fa2002-05-21 21:10:04 +0000482 { // Scope to delete the FoundNames set when we are done with it...
Chris Lattner78771c82002-05-17 04:55:35 +0000483 std::set<string> FoundNames;
484 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000485 if (I->hasName()) // If the global has a name...
486 if (FoundNames.count(I->getName())) // And the name is already used
487 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000488 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000489 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000490
491 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000492 if (I->hasName()) // If the global has a name...
493 if (FoundNames.count(I->getName())) // And the name is already used
494 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000495 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000496 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000497 }
498
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000499 // printing stdlib inclusion
500 // Out << "#include <stdlib.h>\n";
501
Chris Lattner16c7bb22002-05-09 02:28:59 +0000502 // get declaration for alloca
503 Out << "/* Provide Declarations */\n"
Chris Lattner594b9fa2002-05-21 21:10:04 +0000504 << "#include <malloc.h>\n"
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000505 << "#include <alloca.h>\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000506
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000507 // Provide a definition for null if one does not already exist,
508 // and for `bool' if not compiling with a C++ compiler.
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000509 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000510 << "#ifndef __cplusplus\ntypedef unsigned char bool;\n#endif\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000511
Chris Lattnera4d4a852002-08-19 21:48:40 +0000512 << "\n\n/* Global Declarations */\n";
513
514 // First output all the declarations for the program, because C requires
515 // Functions & globals to be declared before they are used.
516 //
Chris Lattner16c7bb22002-05-09 02:28:59 +0000517
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000518 // Loop over the symbol table, emitting all named constants...
519 if (M->hasSymbolTable())
520 printSymbolTable(*M->getSymbolTable());
521
Chris Lattnera4d4a852002-08-19 21:48:40 +0000522 // Global variable declarations...
523 if (!M->gempty()) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000524 Out << "\n/* External Global Variable Declarations */\n";
Chris Lattnera4d4a852002-08-19 21:48:40 +0000525 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000526 if (I->hasExternalLinkage()) {
527 Out << "extern ";
528 printType(I->getType()->getElementType(), getValueName(I));
529 Out << ";\n";
530 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000531 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000532 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000533
Chris Lattnera4d4a852002-08-19 21:48:40 +0000534 // Function declarations
535 if (!M->empty()) {
536 Out << "\n/* Function Declarations */\n";
Chris Lattner4cda8352002-08-20 16:55:48 +0000537 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
538 printFunctionSignature(I, true);
539 Out << ";\n";
540 }
Chris Lattnera4d4a852002-08-19 21:48:40 +0000541 }
542
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000543 // Output the global variable definitions and contents...
Chris Lattnera4d4a852002-08-19 21:48:40 +0000544 if (!M->gempty()) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000545 Out << "\n\n/* Global Variable Definitions and Initialization */\n";
Chris Lattnera4d4a852002-08-19 21:48:40 +0000546 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000547 if (I->hasExternalLinkage())
548 continue; // printed above!
549 Out << "static ";
Chris Lattnera4d4a852002-08-19 21:48:40 +0000550 printType(I->getType()->getElementType(), getValueName(I));
551
552 if (I->hasInitializer()) {
553 Out << " = " ;
554 writeOperand(I->getInitializer());
555 }
556 Out << ";\n";
557 }
558 }
559
Chris Lattner16c7bb22002-05-09 02:28:59 +0000560 // Output all of the functions...
Chris Lattnera4d4a852002-08-19 21:48:40 +0000561 if (!M->empty()) {
562 Out << "\n\n/* Function Bodies */\n";
563 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
564 printFunction(I);
565 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000566}
567
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000568
Chris Lattner2db41cd2002-09-20 15:12:13 +0000569/// printSymbolTable - Run through symbol table looking for type names. If a
570/// type name is found, emit it's declaration...
Chris Lattnera4c047e2002-09-20 14:56:54 +0000571///
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000572void CWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattner2db41cd2002-09-20 15:12:13 +0000573 // If there are no type names, exit early.
574 if (ST.find(Type::TypeTy) == ST.end())
575 return;
576
577 // We are only interested in the type plane of the symbol table...
578 SymbolTable::type_const_iterator I = ST.type_begin(Type::TypeTy);
579 SymbolTable::type_const_iterator End = ST.type_end(Type::TypeTy);
580
Chris Lattner58d04d42002-09-20 15:18:30 +0000581 // Print out forward declarations for structure types before anything else!
582 Out << "/* Structure forward decls */\n";
583 for (; I != End; ++I)
584 if (const Type *STy = dyn_cast<StructType>(I->second)) {
585 string Name = "struct l_" + makeNameProper(I->first);
586 Out << Name << ";\n";
587 TypeNames.insert(std::make_pair(STy, Name));
588 }
589
590 Out << "\n";
591
592 // Now we can print out typedefs...
593 Out << "/* Typedefs */\n";
594 for (I = ST.type_begin(Type::TypeTy); I != End; ++I) {
595 const Type *Ty = cast<Type>(I->second);
596 string Name = "l_" + makeNameProper(I->first);
597 Out << "typedef ";
Chris Lattner270d78a2002-09-20 15:20:24 +0000598 printType(Ty, Name);
Chris Lattner58d04d42002-09-20 15:18:30 +0000599 Out << ";\n";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000600 }
601
602 Out << "\n";
603
Chris Lattner2c601a72002-09-20 15:05:40 +0000604 // Keep track of which structures have been printed so far...
605 std::set<const StructType *> StructPrinted;
606
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000607 // Loop over all structures then push them into the stack so they are
608 // printed in the correct order.
Chris Lattner2db41cd2002-09-20 15:12:13 +0000609 //
Chris Lattner58d04d42002-09-20 15:18:30 +0000610 Out << "/* Structure contents */\n";
Chris Lattner2db41cd2002-09-20 15:12:13 +0000611 for (I = ST.type_begin(Type::TypeTy); I != End; ++I)
612 if (const StructType *STy = dyn_cast<StructType>(I->second))
613 printContainedStructs(STy, StructPrinted);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000614}
615
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000616// Push the struct onto the stack and recursively push all structs
617// this one depends on.
Chris Lattner2c601a72002-09-20 15:05:40 +0000618void CWriter::printContainedStructs(const Type *Ty,
619 std::set<const StructType*> &StructPrinted){
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000620 if (const StructType *STy = dyn_cast<StructType>(Ty)){
621 //Check to see if we have already printed this struct
Chris Lattner2c601a72002-09-20 15:05:40 +0000622 if (StructPrinted.count(STy) == 0) {
623 // Print all contained types first...
Chris Lattnera4c047e2002-09-20 14:56:54 +0000624 for (StructType::ElementTypes::const_iterator
625 I = STy->getElementTypes().begin(),
626 E = STy->getElementTypes().end(); I != E; ++I) {
Chris Lattner2c601a72002-09-20 15:05:40 +0000627 const Type *Ty1 = I->get();
Chris Lattnera4c047e2002-09-20 14:56:54 +0000628 if (isa<StructType>(Ty1) || isa<ArrayType>(Ty1))
Chris Lattner2c601a72002-09-20 15:05:40 +0000629 printContainedStructs(Ty1, StructPrinted);
Chris Lattnera4c047e2002-09-20 14:56:54 +0000630 }
631
Chris Lattner2c601a72002-09-20 15:05:40 +0000632 //Print structure type out..
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000633 StructPrinted.insert(STy);
634 string Name = TypeNames[STy];
635 printType(STy, Name, true);
Chris Lattner58d04d42002-09-20 15:18:30 +0000636 Out << ";\n\n";
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000637 }
Chris Lattnera4c047e2002-09-20 14:56:54 +0000638
Chris Lattner2c601a72002-09-20 15:05:40 +0000639 // If it is an array, check contained types and continue
640 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)){
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000641 const Type *Ty1 = ATy->getElementType();
642 if (isa<StructType>(Ty1) || isa<ArrayType>(Ty1))
Chris Lattner2c601a72002-09-20 15:05:40 +0000643 printContainedStructs(Ty1, StructPrinted);
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000644 }
645}
646
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000647
Chris Lattner4cda8352002-08-20 16:55:48 +0000648void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000649 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000650
651 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000652 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000653
Chris Lattner16c7bb22002-05-09 02:28:59 +0000654 // Print out the return type and name...
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000655 printType(F->getReturnType());
Chris Lattner1f02c892002-05-09 20:14:10 +0000656 Out << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000657
Chris Lattner16c7bb22002-05-09 02:28:59 +0000658 if (!F->isExternal()) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000659 if (!F->aempty()) {
Chris Lattner4cda8352002-08-20 16:55:48 +0000660 string ArgName;
661 if (F->abegin()->hasName() || !Prototype)
662 ArgName = getValueName(F->abegin());
663
664 printType(F->afront().getType(), ArgName);
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000665
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000666 for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
667 I != E; ++I) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000668 Out << ", ";
Chris Lattner4cda8352002-08-20 16:55:48 +0000669 if (I->hasName() || !Prototype)
670 ArgName = getValueName(I);
671 else
672 ArgName = "";
673 printType(I->getType(), ArgName);
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000674 }
675 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000676 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000677 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000678 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000679 FT->getParamTypes().begin(),
680 E = FT->getParamTypes().end(); I != E; ++I) {
681 if (I != FT->getParamTypes().begin()) Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000682 printType(*I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000683 }
684 }
685
Chris Lattner1f8c4a12002-09-20 22:32:30 +0000686 // Finish printing arguments... if this is a vararg function, print the ...,
687 // unless there are no known types, in which case, we just emit ().
688 //
689 if (FT->isVarArg() && !FT->getParamTypes().empty()) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000690 if (FT->getParamTypes().size()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000691 Out << "..."; // Output varargs portion of signature!
692 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000693 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000694}
695
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000696
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000697void CWriter::printFunction(Function *F) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000698 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000699
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000700 Table->incorporateFunction(F);
Chris Lattnerf34ee812002-05-09 03:12:34 +0000701
Chris Lattner4cda8352002-08-20 16:55:48 +0000702 printFunctionSignature(F, false);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000703 Out << " {\n";
704
Chris Lattner497e19a2002-05-09 20:39:03 +0000705 // print local variable information for the function
Chris Lattner7683a122002-05-09 20:33:35 +0000706 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000707 if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000708 Out << " ";
Chris Lattner3af3ba82002-05-09 21:31:18 +0000709 printType((*I)->getType(), getValueName(*I));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000710 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000711 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000712
713 // print the basic blocks
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000714 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
715 BasicBlock *Prev = BB->getPrev();
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000716
717 // Don't print the label for the basic block if there are no uses, or if the
718 // only terminator use is the precessor basic block's terminator. We have
719 // to scan the use list because PHI nodes use basic blocks too but do not
720 // require a label to be generated.
721 //
722 bool NeedsLabel = false;
723 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
724 UI != UE; ++UI)
725 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
726 if (TI != Prev->getTerminator()) {
727 NeedsLabel = true;
728 break;
729 }
730
731 if (NeedsLabel) Out << getValueName(BB) << ":\n";
732
733 // Output all of the instructions in the basic block...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000734 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II){
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000735 if (!isInlinableInst(*II) && !isa<PHINode>(*II)) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000736 if (II->getType() != Type::VoidTy)
737 outputLValue(II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000738 else
739 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000740 visit(*II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000741 Out << ";\n";
742 }
743 }
744
745 // Don't emit prefix or suffix for the terminator...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000746 visit(*BB->getTerminator());
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000747 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000748
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000749 Out << "}\n\n";
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000750 Table->purgeFunction();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000751}
752
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000753// Specific Instruction type classes... note that all of the casts are
754// neccesary because we use the instruction classes as opaque types...
755//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000756void CWriter::visitReturnInst(ReturnInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000757 // Don't output a void return if this is the last basic block in the function
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000758 if (I.getNumOperands() == 0 &&
759 &*--I.getParent()->getParent()->end() == I.getParent() &&
760 !I.getParent()->size() == 1) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000761 return;
Chris Lattner963b70b2002-05-21 18:05:19 +0000762 }
Chris Lattner44408262002-05-09 03:50:42 +0000763
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000764 Out << " return";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000765 if (I.getNumOperands()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000766 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000767 writeOperand(I.getOperand(0));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000768 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000769 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000770}
771
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000772static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
773 // If PHI nodes need copies, we need the copy code...
774 if (isa<PHINode>(To->front()) ||
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000775 From->getNext() != To) // Not directly successor, need goto
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000776 return true;
777
778 // Otherwise we don't need the code.
779 return false;
780}
781
782void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
783 unsigned Indent) {
784 for (BasicBlock::iterator I = Succ->begin();
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000785 PHINode *PN = dyn_cast<PHINode>(&*I); ++I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000786 // now we have to do the printing
787 Out << string(Indent, ' ');
788 outputLValue(PN);
789 writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
790 Out << "; /* for PHI node */\n";
791 }
792
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000793 if (CurBB->getNext() != Succ) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000794 Out << string(Indent, ' ') << " goto ";
795 writeOperand(Succ);
796 Out << ";\n";
797 }
798}
799
800// Brach instruction printing - Avoid printing out a brach to a basic block that
801// immediately succeeds the current one.
802//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000803void CWriter::visitBranchInst(BranchInst &I) {
804 if (I.isConditional()) {
805 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(0))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000806 Out << " if (";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000807 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000808 Out << ") {\n";
809
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000810 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000811
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000812 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(1))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000813 Out << " } else {\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000814 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000815 }
816 } else {
817 // First goto not neccesary, assume second one is...
818 Out << " if (!";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000819 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000820 Out << ") {\n";
821
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000822 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000823 }
824
825 Out << " }\n";
826 } else {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000827 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000828 }
829 Out << "\n";
830}
831
832
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000833void CWriter::visitBinaryOperator(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000834 // binary instructions, shift instructions, setCond instructions.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000835 if (isa<PointerType>(I.getType())) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000836 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000837 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000838 Out << ")";
839 }
840
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000841 if (isa<PointerType>(I.getType())) Out << "(long long)";
842 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000843
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000844 switch (I.getOpcode()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000845 case Instruction::Add: Out << " + "; break;
846 case Instruction::Sub: Out << " - "; break;
847 case Instruction::Mul: Out << "*"; break;
848 case Instruction::Div: Out << "/"; break;
849 case Instruction::Rem: Out << "%"; break;
850 case Instruction::And: Out << " & "; break;
851 case Instruction::Or: Out << " | "; break;
852 case Instruction::Xor: Out << " ^ "; break;
853 case Instruction::SetEQ: Out << " == "; break;
854 case Instruction::SetNE: Out << " != "; break;
855 case Instruction::SetLE: Out << " <= "; break;
856 case Instruction::SetGE: Out << " >= "; break;
857 case Instruction::SetLT: Out << " < "; break;
858 case Instruction::SetGT: Out << " > "; break;
859 case Instruction::Shl : Out << " << "; break;
860 case Instruction::Shr : Out << " >> "; break;
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000861 default: std::cerr << "Invalid operator type!" << I; abort();
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000862 }
863
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000864 if (isa<PointerType>(I.getType())) Out << "(long long)";
865 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000866}
867
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000868void CWriter::visitCastInst(CastInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000869 Out << "(";
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000870 printType(I.getType(), string(""),/*ignoreName*/false, /*namedContext*/false);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000871 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000872 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000873}
874
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000875void CWriter::visitCallInst(CallInst &I) {
876 const PointerType *PTy = cast<PointerType>(I.getCalledValue()->getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000877 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
878 const Type *RetTy = FTy->getReturnType();
879
Chris Lattnerfabc8802002-08-26 20:50:09 +0000880 writeOperand(I.getOperand(0));
881 Out << "(";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000882
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000883 if (I.getNumOperands() > 1) {
884 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000885
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000886 for (unsigned op = 2, Eop = I.getNumOperands(); op != Eop; ++op) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000887 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000888 writeOperand(I.getOperand(op));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000889 }
890 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000891 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000892}
893
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000894void CWriter::visitMallocInst(MallocInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000895 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000896 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000897 Out << ")malloc(sizeof(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000898 printType(I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000899 Out << ")";
900
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000901 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000902 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000903 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000904 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000905 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000906}
907
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000908void CWriter::visitAllocaInst(AllocaInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000909 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000910 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000911 Out << ") alloca(sizeof(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000912 printType(I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000913 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000914 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000915 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000916 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000917 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000918 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000919}
920
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000921void CWriter::visitFreeInst(FreeInst &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000922 Out << "free(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000923 writeOperand(I.getOperand(0));
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000924 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000925}
926
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000927void CWriter::printIndexingExpression(Value *Ptr, User::op_iterator I,
928 User::op_iterator E) {
929 bool HasImplicitAddress = false;
930 // If accessing a global value with no indexing, avoid *(&GV) syndrome
931 if (GlobalValue *V = dyn_cast<GlobalValue>(Ptr)) {
932 HasImplicitAddress = true;
933 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Ptr)) {
934 HasImplicitAddress = true;
935 Ptr = CPR->getValue(); // Get to the global...
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000936 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000937
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000938 if (I == E) {
939 if (!HasImplicitAddress)
940 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000941
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000942 writeOperandInternal(Ptr);
943 return;
944 }
945
946 const Constant *CI = dyn_cast<Constant>(I->get());
947 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
948 Out << "(&";
949
950 writeOperandInternal(Ptr);
951
952 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
953 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000954
955 // Print out the -> operator if possible...
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000956 if (CI && CI->isNullValue() && I+1 != E) {
957 if ((*(I+1))->getType() == Type::UByteTy) {
958 Out << (HasImplicitAddress ? "." : "->");
959 Out << "field" << cast<ConstantUInt>(*(I+1))->getValue();
960 I += 2;
Vikram S. Adve42eb2ba2002-08-24 14:44:23 +0000961 } else { // First array index of 0: Just skip it
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000962 ++I;
963 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000964 }
Chris Lattner5dfe7672002-08-22 22:48:55 +0000965
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000966 for (; I != E; ++I)
Chris Lattner106ff452002-09-11 01:21:29 +0000967 if ((*I)->getType() == Type::LongTy) {
Vikram S. Adve9d6f13f2002-09-15 21:51:04 +0000968 Out << "[";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000969 writeOperand(*I);
Vikram S. Adve9d6f13f2002-09-15 21:51:04 +0000970 Out << "]";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000971 } else {
Chris Lattner59afbf32002-09-12 20:34:47 +0000972 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000973 }
974}
975
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000976void CWriter::visitLoadInst(LoadInst &I) {
Chris Lattner5dfe7672002-08-22 22:48:55 +0000977 Out << "*";
978 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000979}
980
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000981void CWriter::visitStoreInst(StoreInst &I) {
Chris Lattner5dfe7672002-08-22 22:48:55 +0000982 Out << "*";
983 writeOperand(I.getPointerOperand());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000984 Out << " = ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000985 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000986}
987
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000988void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000989 Out << "&";
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000990 printIndexingExpression(I.getPointerOperand(), I.idx_begin(), I.idx_end());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000991}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000992
993//===----------------------------------------------------------------------===//
994// External Interface declaration
995//===----------------------------------------------------------------------===//
996
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000997Pass *createWriteToCPass(std::ostream &o) { return new CWriter(o); }