blob: 4a38db7d42e22e4b33b5f395e43603d70639061d [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"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000018#include "llvm/SymbolTable.h"
Chris Lattner3af3ba82002-05-09 21:31:18 +000019#include "llvm/SlotCalculator.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000020#include "llvm/Support/InstVisitor.h"
Chris Lattner7683a122002-05-09 20:33:35 +000021#include "llvm/Support/InstIterator.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000022#include "Support/StringExtras.h"
23#include "Support/STLExtras.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000024#include <algorithm>
Chris Lattner78771c82002-05-17 04:55:35 +000025#include <set>
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000026using std::string;
27using std::map;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000028using std::ostream;
29
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000030
31
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000032// Pass the Type* variable and and the variable name and this prints out the
33// variable declaration.
Chris Lattnerdeed7a52002-05-09 15:18:52 +000034//
Chris Lattner16c7bb22002-05-09 02:28:59 +000035static string calcTypeNameVar(const Type *Ty,
Chris Lattner3ef6dc72002-05-09 14:40:11 +000036 map<const Type *, string> &TypeNames,
37 const string &NameSoFar, bool ignoreName = false){
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000038 if (Ty->isPrimitiveType())
39 switch (Ty->getPrimitiveID()) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +000040 case Type::VoidTyID: return "void " + NameSoFar;
41 case Type::BoolTyID: return "bool " + NameSoFar;
42 case Type::UByteTyID: return "unsigned char " + NameSoFar;
43 case Type::SByteTyID: return "signed char " + NameSoFar;
44 case Type::UShortTyID: return "unsigned short " + NameSoFar;
45 case Type::ShortTyID: return "short " + NameSoFar;
46 case Type::UIntTyID: return "unsigned " + NameSoFar;
47 case Type::IntTyID: return "int " + NameSoFar;
48 case Type::ULongTyID: return "unsigned long long " + NameSoFar;
49 case Type::LongTyID: return "signed long long " + NameSoFar;
Chris Lattner1f02c892002-05-09 20:14:10 +000050 case Type::FloatTyID: return "float " + NameSoFar;
51 case Type::DoubleTyID: return "double " + NameSoFar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000052 default :
Chris Lattnere7f65d3b2002-06-30 16:07:20 +000053 std::cerr << "Unknown primitive type: " << Ty << "\n";
Chris Lattnerdeed7a52002-05-09 15:18:52 +000054 abort();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000055 }
56
57 // Check to see if the type is named.
Chris Lattner3ef6dc72002-05-09 14:40:11 +000058 if (!ignoreName) {
59 map<const Type *, string>::iterator I = TypeNames.find(Ty);
60 if (I != TypeNames.end())
61 return I->second + " " + NameSoFar;
62 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000063
Chris Lattner3ef6dc72002-05-09 14:40:11 +000064 string Result;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000065 switch (Ty->getPrimitiveID()) {
66 case Type::FunctionTyID: {
Chris Lattner7683a122002-05-09 20:33:35 +000067 const FunctionType *MTy = cast<FunctionType>(Ty);
Chris Lattner3ef6dc72002-05-09 14:40:11 +000068 Result += calcTypeNameVar(MTy->getReturnType(), TypeNames, "");
Chris Lattner7683a122002-05-09 20:33:35 +000069 Result += " " + NameSoFar + " (";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000070 for (FunctionType::ParamTypes::const_iterator
71 I = MTy->getParamTypes().begin(),
72 E = MTy->getParamTypes().end(); I != E; ++I) {
73 if (I != MTy->getParamTypes().begin())
74 Result += ", ";
Chris Lattner3ef6dc72002-05-09 14:40:11 +000075 Result += calcTypeNameVar(*I, TypeNames, "");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000076 }
77 if (MTy->isVarArg()) {
78 if (!MTy->getParamTypes().empty())
79 Result += ", ";
80 Result += "...";
81 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +000082 return Result + ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000083 }
84 case Type::StructTyID: {
85 const StructType *STy = cast<const StructType>(Ty);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +000086 Result = NameSoFar + " {\n";
87 unsigned indx = 0;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000088 for (StructType::ElementTypes::const_iterator
89 I = STy->getElementTypes().begin(),
90 E = STy->getElementTypes().end(); I != E; ++I) {
Chris Lattner8c3c4bf2002-05-09 15:49:41 +000091 Result += " " +calcTypeNameVar(*I, TypeNames, "field" + utostr(indx++));
92 Result += ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000093 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +000094 return Result + "}";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000095 }
96
Chris Lattner3af3ba82002-05-09 21:31:18 +000097 case Type::PointerTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +000098 return calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(),
99 TypeNames, "*" + NameSoFar);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000100
101 case Type::ArrayTyID: {
102 const ArrayType *ATy = cast<const ArrayType>(Ty);
103 int NumElements = ATy->getNumElements();
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000104 return calcTypeNameVar(ATy->getElementType(), TypeNames,
105 NameSoFar + "[" + itostr(NumElements) + "]");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000106 }
107 default:
108 assert(0 && "Unhandled case in getTypeProps!");
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000109 abort();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000110 }
111
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000112 return Result;
113}
114
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000115namespace {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000116 class CWriter : public InstVisitor<CWriter> {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000117 ostream& Out;
118 SlotCalculator &Table;
119 const Module *TheModule;
120 map<const Type *, string> TypeNames;
Chris Lattner78771c82002-05-17 04:55:35 +0000121 std::set<const Value*> MangledGlobals;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000122 public:
123 inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M)
124 : Out(o), Table(Tab), TheModule(M) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000125 }
126
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000127 inline void write(Module *M) { printModule(M); }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000128
Chris Lattner3af3ba82002-05-09 21:31:18 +0000129 ostream& printType(const Type *Ty, const string &VariableName = "") {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000130 return Out << calcTypeNameVar(Ty, TypeNames, VariableName);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000131 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000132
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000133 void writeOperand(Value *Operand);
134 void writeOperandInternal(Value *Operand);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000135
136 string getValueName(const Value *V);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000137
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000138 private :
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000139 void printModule(Module *M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000140 void printSymbolTable(const SymbolTable &ST);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000141 void printGlobal(const GlobalVariable *GV);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000142 void printFunctionSignature(const Function *F);
143 void printFunctionDecl(const Function *F); // Print just the forward decl
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000144
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000145 void printFunction(Function *);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000146
Chris Lattner6d492922002-08-19 21:32:41 +0000147 void printConstant(Constant *CPV);
148 void printConstantArray(ConstantArray *CPA);
149
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000150 // isInlinableInst - Attempt to inline instructions into their uses to build
151 // trees as much as possible. To do this, we have to consistently decide
152 // what is acceptable to inline, so that variable declarations don't get
153 // printed and an extra copy of the expr is not emitted.
154 //
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000155 static bool isInlinableInst(const Instruction &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000156 // Must be an expression, must be used exactly once. If it is dead, we
157 // emit it inline where it would go.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000158 if (I.getType() == Type::VoidTy || I.use_size() != 1 ||
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000159 isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I))
160 return false;
161
162 // Only inline instruction it it's use is in the same BB as the inst.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000163 return I.getParent() == cast<Instruction>(I.use_back())->getParent();
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000164 }
165
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000166 // Instruction visitation functions
167 friend class InstVisitor<CWriter>;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000168
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000169 void visitReturnInst(ReturnInst &I);
170 void visitBranchInst(BranchInst &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000171
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000172 void visitPHINode(PHINode &I) {}
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000173 void visitBinaryOperator(Instruction &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000174
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000175 void visitCastInst (CastInst &I);
176 void visitCallInst (CallInst &I);
177 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000178
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000179 void visitMallocInst(MallocInst &I);
180 void visitAllocaInst(AllocaInst &I);
181 void visitFreeInst (FreeInst &I);
182 void visitLoadInst (LoadInst &I);
183 void visitStoreInst (StoreInst &I);
184 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000185
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000186 void visitInstruction(Instruction &I) {
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000187 std::cerr << "C Writer does not know about " << I;
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000188 abort();
189 }
190
191 void outputLValue(Instruction *I) {
192 Out << " " << getValueName(I) << " = ";
193 }
194 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
195 unsigned Indent);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000196 void printIndexingExpr(MemAccessInst &MAI);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000197 };
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000198}
199
Chris Lattner2f499022002-05-09 04:21:21 +0000200// We dont want identifier names with ., space, - in them.
201// So we replace them with _
202static string makeNameProper(string x) {
203 string tmp;
204 for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
205 switch (*sI) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000206 case '.': tmp += "d_"; break;
207 case ' ': tmp += "s_"; break;
208 case '-': tmp += "D_"; break;
Chris Lattner2f499022002-05-09 04:21:21 +0000209 default: tmp += *sI;
210 }
211
212 return tmp;
213}
214
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000215string CWriter::getValueName(const Value *V) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000216 if (V->hasName()) { // Print out the label if it exists...
217 if (isa<GlobalValue>(V) && // Do not mangle globals...
218 cast<GlobalValue>(V)->hasExternalLinkage() && // Unless it's internal or
219 !MangledGlobals.count(V)) // Unless the name would collide if we don't
Chris Lattner2f499022002-05-09 04:21:21 +0000220 return makeNameProper(V->getName());
221
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000222 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
223 makeNameProper(V->getName());
Chris Lattner2f499022002-05-09 04:21:21 +0000224 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000225
226 int Slot = Table.getValSlot(V);
227 assert(Slot >= 0 && "Invalid value!");
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000228 return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000229}
230
Chris Lattner6d492922002-08-19 21:32:41 +0000231void CWriter::printConstantArray(ConstantArray *CPA) {
232
233 // As a special case, print the array as a string if it is an array of
234 // ubytes or an array of sbytes with positive values.
235 //
236 const Type *ETy = CPA->getType()->getElementType();
237 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
238
239 // Make sure the last character is a null char, as automatically added by C
240 if (CPA->getNumOperands() == 0 ||
241 !cast<Constant>(*(CPA->op_end()-1))->isNullValue())
242 isString = false;
243
244 if (isString) {
245 Out << "\"";
246 // Do not include the last character, which we know is null
247 for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) {
248 unsigned char C = (ETy == Type::SByteTy) ?
249 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
250 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
251
252 if (isprint(C)) {
253 Out << C;
254 } else {
255 switch (C) {
256 case '\n': Out << "\\n"; break;
257 case '\t': Out << "\\t"; break;
258 case '\r': Out << "\\r"; break;
259 case '\v': Out << "\\v"; break;
260 case '\a': Out << "\\a"; break;
261 default:
262 Out << "\\x";
263 Out << ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
264 Out << ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
265 break;
266 }
267 }
268 }
269 Out << "\"";
270 } else {
271 Out << "{";
272 if (CPA->getNumOperands()) {
273 Out << " ";
274 printConstant(cast<Constant>(CPA->getOperand(0)));
275 for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
276 Out << ", ";
277 printConstant(cast<Constant>(CPA->getOperand(i)));
278 }
279 }
280 Out << " }";
281 }
282}
283
284
285// printConstant - The LLVM Constant to C Constant converter.
286void CWriter::printConstant(Constant *CPV) {
287 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
288 switch (CE->getOpcode()) {
289 default:
290 std::cerr << "CWriter Error: Unhandled constant expression: "
291 << CE << "\n";
292 abort();
293 }
294 }
295
296 switch (CPV->getType()->getPrimitiveID()) {
297 case Type::BoolTyID:
298 Out << (CPV == ConstantBool::False ? "0" : "1"); break;
299 case Type::SByteTyID:
300 case Type::ShortTyID:
301 case Type::IntTyID:
302 Out << cast<ConstantSInt>(CPV)->getValue(); break;
303 case Type::LongTyID:
304 Out << cast<ConstantSInt>(CPV)->getValue() << "ll"; break;
305
306 case Type::UByteTyID:
307 case Type::UShortTyID:
308 Out << cast<ConstantUInt>(CPV)->getValue(); break;
309 case Type::UIntTyID:
310 Out << cast<ConstantUInt>(CPV)->getValue() << "u"; break;
311 case Type::ULongTyID:
312 Out << cast<ConstantUInt>(CPV)->getValue() << "ull"; break;
313
314 case Type::FloatTyID:
315 case Type::DoubleTyID:
316 Out << cast<ConstantFP>(CPV)->getValue(); break;
317
318 case Type::ArrayTyID:
319 printConstantArray(cast<ConstantArray>(CPV));
320 break;
321
322 case Type::StructTyID: {
323 Out << "{";
324 if (CPV->getNumOperands()) {
325 Out << " ";
326 printConstant(cast<Constant>(CPV->getOperand(0)));
327 for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) {
328 Out << ", ";
329 printConstant(cast<Constant>(CPV->getOperand(i)));
330 }
331 }
332 Out << " }";
333 break;
334 }
335
336 case Type::PointerTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000337 if (isa<ConstantPointerNull>(CPV)) {
338 Out << "((";
Chris Lattner3af3ba82002-05-09 21:31:18 +0000339 printType(CPV->getType(), "");
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000340 Out << ")NULL)";
Chris Lattner6d492922002-08-19 21:32:41 +0000341 break;
342 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
343 writeOperand(CPR->getValue());
344 break;
345 }
346 // FALL THROUGH
347 default:
348 std::cerr << "Unknown constant type: " << CPV << "\n";
349 abort();
350 }
351}
352
353void CWriter::writeOperandInternal(Value *Operand) {
354 if (Operand->hasName()) {
355 Out << getValueName(Operand);
356 } else if (Constant *CPV = dyn_cast<Constant>(Operand)) {
357 printConstant(CPV);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000358 } else {
359 int Slot = Table.getValSlot(Operand);
360 assert(Slot >= 0 && "Malformed LLVM!");
361 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
362 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000363}
364
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000365void CWriter::writeOperand(Value *Operand) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000366 if (Instruction *I = dyn_cast<Instruction>(Operand))
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000367 if (isInlinableInst(*I)) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000368 // Should we inline this instruction to build a tree?
369 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000370 visit(*I);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000371 Out << ")";
372 return;
373 }
374
375 if (isa<GlobalVariable>(Operand))
376 Out << "(&"; // Global variables are references as their addresses by llvm
377
378 writeOperandInternal(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000379
380 if (isa<GlobalVariable>(Operand))
381 Out << ")";
382}
383
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000384void CWriter::printModule(Module *M) {
Chris Lattner78771c82002-05-17 04:55:35 +0000385 // Calculate which global values have names that will collide when we throw
386 // away type information.
Chris Lattner594b9fa2002-05-21 21:10:04 +0000387 { // Scope to delete the FoundNames set when we are done with it...
Chris Lattner78771c82002-05-17 04:55:35 +0000388 std::set<string> FoundNames;
389 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000390 if (I->hasName()) // If the global has a name...
391 if (FoundNames.count(I->getName())) // And the name is already used
392 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000393 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000394 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000395
396 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000397 if (I->hasName()) // If the global has a name...
398 if (FoundNames.count(I->getName())) // And the name is already used
399 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000400 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000401 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000402 }
403
404
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000405 // printing stdlib inclusion
406 // Out << "#include <stdlib.h>\n";
407
Chris Lattner16c7bb22002-05-09 02:28:59 +0000408 // get declaration for alloca
409 Out << "/* Provide Declarations */\n"
Chris Lattner594b9fa2002-05-21 21:10:04 +0000410 << "#include <malloc.h>\n"
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000411 << "#include <alloca.h>\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000412
413 // Provide a definition for null if one does not already exist.
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000414 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000415 << "typedef unsigned char bool;\n"
416
417 << "\n\n/* Global Symbols */\n";
418
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000419 // Loop over the symbol table, emitting all named constants...
420 if (M->hasSymbolTable())
421 printSymbolTable(*M->getSymbolTable());
422
Chris Lattner16c7bb22002-05-09 02:28:59 +0000423 Out << "\n\n/* Global Data */\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000424 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
425 if (I->hasInternalLinkage()) Out << "static ";
426 printType(I->getType()->getElementType(), getValueName(I));
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000427
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000428 if (I->hasInitializer()) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000429 Out << " = " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000430 writeOperand(I->getInitializer());
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000431 }
432 Out << ";\n";
433 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000434
Chris Lattner16c7bb22002-05-09 02:28:59 +0000435 // First output all the declarations of the functions as C requires Functions
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000436 // be declared before they are used.
Chris Lattner16c7bb22002-05-09 02:28:59 +0000437 //
438 Out << "\n\n/* Function Declarations */\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000439 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
440 printFunctionDecl(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000441
Chris Lattner16c7bb22002-05-09 02:28:59 +0000442 // Output all of the functions...
443 Out << "\n\n/* Function Bodies */\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000444 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
445 printFunction(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000446}
447
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000448
449// printSymbolTable - Run through symbol table looking for named constants
450// if a named constant is found, emit it's declaration...
451// Assuming that symbol table has only types and constants.
452void CWriter::printSymbolTable(const SymbolTable &ST) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000453 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
454 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
455 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
456
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000457 for (; I != End; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000458 if (const Type *Ty = dyn_cast<StructType>(I->second)) {
459 string Name = "struct l_" + makeNameProper(I->first);
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000460 Out << Name << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000461
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000462 TypeNames.insert(std::make_pair(Ty, Name));
463 }
464 }
465
466 Out << "\n";
467
468 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
469 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
470 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
471
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000472 for (; I != End; ++I) {
473 const Value *V = I->second;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000474 if (const Type *Ty = dyn_cast<Type>(V)) {
475 string Name = "l_" + makeNameProper(I->first);
Chris Lattner1f02c892002-05-09 20:14:10 +0000476 if (isa<StructType>(Ty))
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000477 Name = "struct " + makeNameProper(Name);
Chris Lattner1f02c892002-05-09 20:14:10 +0000478 else
479 Out << "typedef ";
480
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000481 Out << calcTypeNameVar(Ty, TypeNames, Name, true) << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000482 }
483 }
484 }
485}
486
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000487
Chris Lattner16c7bb22002-05-09 02:28:59 +0000488// printFunctionDecl - Print function declaration
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000489//
Chris Lattner16c7bb22002-05-09 02:28:59 +0000490void CWriter::printFunctionDecl(const Function *F) {
491 printFunctionSignature(F);
492 Out << ";\n";
493}
494
495void CWriter::printFunctionSignature(const Function *F) {
496 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000497
498 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000499 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000500
Chris Lattner16c7bb22002-05-09 02:28:59 +0000501 // Print out the return type and name...
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000502 printType(F->getReturnType());
Chris Lattner1f02c892002-05-09 20:14:10 +0000503 Out << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000504
Chris Lattner16c7bb22002-05-09 02:28:59 +0000505 if (!F->isExternal()) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000506 if (!F->aempty()) {
507 printType(F->afront().getType(), getValueName(F->abegin()));
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000508
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000509 for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
510 I != E; ++I) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000511 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000512 printType(I->getType(), getValueName(I));
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000513 }
514 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000515 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000516 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000517 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000518 FT->getParamTypes().begin(),
519 E = FT->getParamTypes().end(); I != E; ++I) {
520 if (I != FT->getParamTypes().begin()) Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000521 printType(*I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000522 }
523 }
524
525 // Finish printing arguments...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000526 if (FT->isVarArg()) {
527 if (FT->getParamTypes().size()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000528 Out << "..."; // Output varargs portion of signature!
529 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000530 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000531}
532
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000533
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000534void CWriter::printFunction(Function *F) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000535 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000536
Chris Lattnerf34ee812002-05-09 03:12:34 +0000537 Table.incorporateFunction(F);
538
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000539 printFunctionSignature(F);
540 Out << " {\n";
541
Chris Lattner497e19a2002-05-09 20:39:03 +0000542 // print local variable information for the function
Chris Lattner7683a122002-05-09 20:33:35 +0000543 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000544 if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000545 Out << " ";
Chris Lattner3af3ba82002-05-09 21:31:18 +0000546 printType((*I)->getType(), getValueName(*I));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000547 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000548 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000549
550 // print the basic blocks
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000551 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
552 BasicBlock *Prev = BB->getPrev();
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000553
554 // Don't print the label for the basic block if there are no uses, or if the
555 // only terminator use is the precessor basic block's terminator. We have
556 // to scan the use list because PHI nodes use basic blocks too but do not
557 // require a label to be generated.
558 //
559 bool NeedsLabel = false;
560 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
561 UI != UE; ++UI)
562 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
563 if (TI != Prev->getTerminator()) {
564 NeedsLabel = true;
565 break;
566 }
567
568 if (NeedsLabel) Out << getValueName(BB) << ":\n";
569
570 // Output all of the instructions in the basic block...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000571 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II){
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000572 if (!isInlinableInst(*II) && !isa<PHINode>(*II)) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000573 if (II->getType() != Type::VoidTy)
574 outputLValue(II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000575 else
576 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000577 visit(*II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000578 Out << ";\n";
579 }
580 }
581
582 // Don't emit prefix or suffix for the terminator...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000583 visit(*BB->getTerminator());
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000584 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000585
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000586 Out << "}\n\n";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000587 Table.purgeFunction();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000588}
589
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000590// Specific Instruction type classes... note that all of the casts are
591// neccesary because we use the instruction classes as opaque types...
592//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000593void CWriter::visitReturnInst(ReturnInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000594 // Don't output a void return if this is the last basic block in the function
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000595 if (I.getNumOperands() == 0 &&
596 &*--I.getParent()->getParent()->end() == I.getParent() &&
597 !I.getParent()->size() == 1) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000598 return;
Chris Lattner963b70b2002-05-21 18:05:19 +0000599 }
Chris Lattner44408262002-05-09 03:50:42 +0000600
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000601 Out << " return";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000602 if (I.getNumOperands()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000603 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000604 writeOperand(I.getOperand(0));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000605 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000606 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000607}
608
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000609static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
610 // If PHI nodes need copies, we need the copy code...
611 if (isa<PHINode>(To->front()) ||
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000612 From->getNext() != To) // Not directly successor, need goto
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000613 return true;
614
615 // Otherwise we don't need the code.
616 return false;
617}
618
619void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
620 unsigned Indent) {
621 for (BasicBlock::iterator I = Succ->begin();
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000622 PHINode *PN = dyn_cast<PHINode>(&*I); ++I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000623 // now we have to do the printing
624 Out << string(Indent, ' ');
625 outputLValue(PN);
626 writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
627 Out << "; /* for PHI node */\n";
628 }
629
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000630 if (CurBB->getNext() != Succ) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000631 Out << string(Indent, ' ') << " goto ";
632 writeOperand(Succ);
633 Out << ";\n";
634 }
635}
636
637// Brach instruction printing - Avoid printing out a brach to a basic block that
638// immediately succeeds the current one.
639//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000640void CWriter::visitBranchInst(BranchInst &I) {
641 if (I.isConditional()) {
642 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(0))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000643 Out << " if (";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000644 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000645 Out << ") {\n";
646
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000647 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000648
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000649 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(1))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000650 Out << " } else {\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000651 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000652 }
653 } else {
654 // First goto not neccesary, assume second one is...
655 Out << " if (!";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000656 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000657 Out << ") {\n";
658
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000659 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000660 }
661
662 Out << " }\n";
663 } else {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000664 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000665 }
666 Out << "\n";
667}
668
669
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000670void CWriter::visitBinaryOperator(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000671 // binary instructions, shift instructions, setCond instructions.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000672 if (isa<PointerType>(I.getType())) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000673 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000674 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000675 Out << ")";
676 }
677
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000678 if (isa<PointerType>(I.getType())) Out << "(long long)";
679 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000680
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000681 switch (I.getOpcode()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000682 case Instruction::Add: Out << " + "; break;
683 case Instruction::Sub: Out << " - "; break;
684 case Instruction::Mul: Out << "*"; break;
685 case Instruction::Div: Out << "/"; break;
686 case Instruction::Rem: Out << "%"; break;
687 case Instruction::And: Out << " & "; break;
688 case Instruction::Or: Out << " | "; break;
689 case Instruction::Xor: Out << " ^ "; break;
690 case Instruction::SetEQ: Out << " == "; break;
691 case Instruction::SetNE: Out << " != "; break;
692 case Instruction::SetLE: Out << " <= "; break;
693 case Instruction::SetGE: Out << " >= "; break;
694 case Instruction::SetLT: Out << " < "; break;
695 case Instruction::SetGT: Out << " > "; break;
696 case Instruction::Shl : Out << " << "; break;
697 case Instruction::Shr : Out << " >> "; break;
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000698 default: std::cerr << "Invalid operator type!" << I; abort();
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000699 }
700
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000701 if (isa<PointerType>(I.getType())) Out << "(long long)";
702 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000703}
704
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000705void CWriter::visitCastInst(CastInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000706 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000707 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000708 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000709 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000710}
711
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000712void CWriter::visitCallInst(CallInst &I) {
713 const PointerType *PTy = cast<PointerType>(I.getCalledValue()->getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000714 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
715 const Type *RetTy = FTy->getReturnType();
716
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000717 Out << getValueName(I.getOperand(0)) << "(";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000718
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000719 if (I.getNumOperands() > 1) {
720 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000721
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000722 for (unsigned op = 2, Eop = I.getNumOperands(); op != Eop; ++op) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000723 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000724 writeOperand(I.getOperand(op));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000725 }
726 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000727 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000728}
729
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000730void CWriter::visitMallocInst(MallocInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000731 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000732 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000733 Out << ")malloc(sizeof(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000734 printType(I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000735 Out << ")";
736
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000737 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000738 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000739 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000740 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000741 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000742}
743
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000744void CWriter::visitAllocaInst(AllocaInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000745 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000746 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000747 Out << ") alloca(sizeof(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000748 printType(I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000749 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000750 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000751 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000752 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000753 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000754 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000755}
756
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000757void CWriter::visitFreeInst(FreeInst &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000758 Out << "free(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000759 writeOperand(I.getOperand(0));
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000760 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000761}
762
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000763void CWriter::printIndexingExpr(MemAccessInst &MAI) {
764 MemAccessInst::op_iterator I = MAI.idx_begin(), E = MAI.idx_end();
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000765 if (I == E) {
766 // If accessing a global value with no indexing, avoid *(&GV) syndrome
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000767 if (GlobalValue *V = dyn_cast<GlobalValue>(MAI.getPointerOperand())) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000768 writeOperandInternal(V);
769 return;
770 }
771
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000772 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000773 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000774
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000775 writeOperand(MAI.getPointerOperand());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000776
777 if (I == E) return;
778
779 // Print out the -> operator if possible...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000780 const Constant *CI = dyn_cast<Constant>(I->get());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000781 if (CI && CI->isNullValue() && I+1 != E &&
782 (*(I+1))->getType() == Type::UByteTy) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000783 Out << "->field" << cast<ConstantUInt>(*(I+1))->getValue();
784 I += 2;
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000785 }
786
787 for (; I != E; ++I)
788 if ((*I)->getType() == Type::UIntTy) {
789 Out << "[";
790 writeOperand(*I);
791 Out << "]";
792 } else {
793 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
794 }
795}
796
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000797void CWriter::visitLoadInst(LoadInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000798 printIndexingExpr(I);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000799}
800
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000801void CWriter::visitStoreInst(StoreInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000802 printIndexingExpr(I);
803 Out << " = ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000804 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000805}
806
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000807void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000808 Out << "&";
809 printIndexingExpr(I);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000810}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000811
812//===----------------------------------------------------------------------===//
813// External Interface declaration
814//===----------------------------------------------------------------------===//
815
Chris Lattnerf34ee812002-05-09 03:12:34 +0000816void WriteToC(const Module *M, ostream &Out) {
817 assert(M && "You can't write a null module!!");
818 SlotCalculator SlotTable(M, false);
819 CWriter W(Out, SlotTable, M);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000820 W.write((Module*)M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000821 Out.flush();
822}