blob: f2adf95940cba34b30b4ca53e711fe26f356e0f8 [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
Chris Lattnera4d4a852002-08-19 21:48:40 +0000417 << "\n\n/* Global Declarations */\n";
418
419 // First output all the declarations for the program, because C requires
420 // Functions & globals to be declared before they are used.
421 //
Chris Lattner16c7bb22002-05-09 02:28:59 +0000422
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000423 // Loop over the symbol table, emitting all named constants...
424 if (M->hasSymbolTable())
425 printSymbolTable(*M->getSymbolTable());
426
Chris Lattnera4d4a852002-08-19 21:48:40 +0000427 // Global variable declarations...
428 if (!M->gempty()) {
429 Out << "\n/* Global Variable Declarations */\n";
430 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
431 Out << (I->hasExternalLinkage() ? "extern " : "static ");
432 printType(I->getType()->getElementType(), getValueName(I));
433 Out << ";\n";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000434 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000435 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000436
Chris Lattnera4d4a852002-08-19 21:48:40 +0000437 // Function declarations
438 if (!M->empty()) {
439 Out << "\n/* Function Declarations */\n";
440 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
441 printFunctionDecl(I);
442 }
443
444 // Output the global variable contents...
445 if (!M->gempty()) {
446 Out << "\n\n/* Global Data */\n";
447 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
448 if (I->hasInternalLinkage()) Out << "static ";
449 printType(I->getType()->getElementType(), getValueName(I));
450
451 if (I->hasInitializer()) {
452 Out << " = " ;
453 writeOperand(I->getInitializer());
454 }
455 Out << ";\n";
456 }
457 }
458
Chris Lattner16c7bb22002-05-09 02:28:59 +0000459 // Output all of the functions...
Chris Lattnera4d4a852002-08-19 21:48:40 +0000460 if (!M->empty()) {
461 Out << "\n\n/* Function Bodies */\n";
462 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
463 printFunction(I);
464 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000465}
466
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000467
468// printSymbolTable - Run through symbol table looking for named constants
469// if a named constant is found, emit it's declaration...
470// Assuming that symbol table has only types and constants.
471void CWriter::printSymbolTable(const SymbolTable &ST) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000472 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
473 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
474 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
475
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000476 for (; I != End; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000477 if (const Type *Ty = dyn_cast<StructType>(I->second)) {
478 string Name = "struct l_" + makeNameProper(I->first);
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000479 Out << Name << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000480
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000481 TypeNames.insert(std::make_pair(Ty, Name));
482 }
483 }
484
485 Out << "\n";
486
487 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
488 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
489 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
490
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000491 for (; I != End; ++I) {
492 const Value *V = I->second;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000493 if (const Type *Ty = dyn_cast<Type>(V)) {
494 string Name = "l_" + makeNameProper(I->first);
Chris Lattner1f02c892002-05-09 20:14:10 +0000495 if (isa<StructType>(Ty))
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000496 Name = "struct " + makeNameProper(Name);
Chris Lattner1f02c892002-05-09 20:14:10 +0000497 else
498 Out << "typedef ";
499
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000500 Out << calcTypeNameVar(Ty, TypeNames, Name, true) << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000501 }
502 }
503 }
504}
505
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000506
Chris Lattner16c7bb22002-05-09 02:28:59 +0000507// printFunctionDecl - Print function declaration
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000508//
Chris Lattner16c7bb22002-05-09 02:28:59 +0000509void CWriter::printFunctionDecl(const Function *F) {
510 printFunctionSignature(F);
511 Out << ";\n";
512}
513
514void CWriter::printFunctionSignature(const Function *F) {
515 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000516
517 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000518 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000519
Chris Lattner16c7bb22002-05-09 02:28:59 +0000520 // Print out the return type and name...
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000521 printType(F->getReturnType());
Chris Lattner1f02c892002-05-09 20:14:10 +0000522 Out << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000523
Chris Lattner16c7bb22002-05-09 02:28:59 +0000524 if (!F->isExternal()) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000525 if (!F->aempty()) {
526 printType(F->afront().getType(), getValueName(F->abegin()));
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000527
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000528 for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
529 I != E; ++I) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000530 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000531 printType(I->getType(), getValueName(I));
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000532 }
533 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000534 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000535 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000536 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000537 FT->getParamTypes().begin(),
538 E = FT->getParamTypes().end(); I != E; ++I) {
539 if (I != FT->getParamTypes().begin()) Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000540 printType(*I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000541 }
542 }
543
544 // Finish printing arguments...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000545 if (FT->isVarArg()) {
546 if (FT->getParamTypes().size()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000547 Out << "..."; // Output varargs portion of signature!
548 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000549 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000550}
551
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000552
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000553void CWriter::printFunction(Function *F) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000554 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000555
Chris Lattnerf34ee812002-05-09 03:12:34 +0000556 Table.incorporateFunction(F);
557
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000558 printFunctionSignature(F);
559 Out << " {\n";
560
Chris Lattner497e19a2002-05-09 20:39:03 +0000561 // print local variable information for the function
Chris Lattner7683a122002-05-09 20:33:35 +0000562 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000563 if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000564 Out << " ";
Chris Lattner3af3ba82002-05-09 21:31:18 +0000565 printType((*I)->getType(), getValueName(*I));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000566 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000567 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000568
569 // print the basic blocks
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000570 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
571 BasicBlock *Prev = BB->getPrev();
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000572
573 // Don't print the label for the basic block if there are no uses, or if the
574 // only terminator use is the precessor basic block's terminator. We have
575 // to scan the use list because PHI nodes use basic blocks too but do not
576 // require a label to be generated.
577 //
578 bool NeedsLabel = false;
579 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
580 UI != UE; ++UI)
581 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
582 if (TI != Prev->getTerminator()) {
583 NeedsLabel = true;
584 break;
585 }
586
587 if (NeedsLabel) Out << getValueName(BB) << ":\n";
588
589 // Output all of the instructions in the basic block...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000590 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II){
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000591 if (!isInlinableInst(*II) && !isa<PHINode>(*II)) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000592 if (II->getType() != Type::VoidTy)
593 outputLValue(II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000594 else
595 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000596 visit(*II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000597 Out << ";\n";
598 }
599 }
600
601 // Don't emit prefix or suffix for the terminator...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000602 visit(*BB->getTerminator());
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000603 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000604
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000605 Out << "}\n\n";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000606 Table.purgeFunction();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000607}
608
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000609// Specific Instruction type classes... note that all of the casts are
610// neccesary because we use the instruction classes as opaque types...
611//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000612void CWriter::visitReturnInst(ReturnInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000613 // Don't output a void return if this is the last basic block in the function
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000614 if (I.getNumOperands() == 0 &&
615 &*--I.getParent()->getParent()->end() == I.getParent() &&
616 !I.getParent()->size() == 1) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000617 return;
Chris Lattner963b70b2002-05-21 18:05:19 +0000618 }
Chris Lattner44408262002-05-09 03:50:42 +0000619
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000620 Out << " return";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000621 if (I.getNumOperands()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000622 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000623 writeOperand(I.getOperand(0));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000624 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000625 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000626}
627
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000628static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
629 // If PHI nodes need copies, we need the copy code...
630 if (isa<PHINode>(To->front()) ||
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000631 From->getNext() != To) // Not directly successor, need goto
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000632 return true;
633
634 // Otherwise we don't need the code.
635 return false;
636}
637
638void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
639 unsigned Indent) {
640 for (BasicBlock::iterator I = Succ->begin();
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000641 PHINode *PN = dyn_cast<PHINode>(&*I); ++I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000642 // now we have to do the printing
643 Out << string(Indent, ' ');
644 outputLValue(PN);
645 writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
646 Out << "; /* for PHI node */\n";
647 }
648
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000649 if (CurBB->getNext() != Succ) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000650 Out << string(Indent, ' ') << " goto ";
651 writeOperand(Succ);
652 Out << ";\n";
653 }
654}
655
656// Brach instruction printing - Avoid printing out a brach to a basic block that
657// immediately succeeds the current one.
658//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000659void CWriter::visitBranchInst(BranchInst &I) {
660 if (I.isConditional()) {
661 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(0))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000662 Out << " if (";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000663 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000664 Out << ") {\n";
665
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000666 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000667
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000668 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(1))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000669 Out << " } else {\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000670 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000671 }
672 } else {
673 // First goto not neccesary, assume second one is...
674 Out << " if (!";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000675 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000676 Out << ") {\n";
677
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000678 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000679 }
680
681 Out << " }\n";
682 } else {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000683 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000684 }
685 Out << "\n";
686}
687
688
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000689void CWriter::visitBinaryOperator(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000690 // binary instructions, shift instructions, setCond instructions.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000691 if (isa<PointerType>(I.getType())) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000692 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000693 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000694 Out << ")";
695 }
696
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000697 if (isa<PointerType>(I.getType())) Out << "(long long)";
698 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000699
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000700 switch (I.getOpcode()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000701 case Instruction::Add: Out << " + "; break;
702 case Instruction::Sub: Out << " - "; break;
703 case Instruction::Mul: Out << "*"; break;
704 case Instruction::Div: Out << "/"; break;
705 case Instruction::Rem: Out << "%"; break;
706 case Instruction::And: Out << " & "; break;
707 case Instruction::Or: Out << " | "; break;
708 case Instruction::Xor: Out << " ^ "; break;
709 case Instruction::SetEQ: Out << " == "; break;
710 case Instruction::SetNE: Out << " != "; break;
711 case Instruction::SetLE: Out << " <= "; break;
712 case Instruction::SetGE: Out << " >= "; break;
713 case Instruction::SetLT: Out << " < "; break;
714 case Instruction::SetGT: Out << " > "; break;
715 case Instruction::Shl : Out << " << "; break;
716 case Instruction::Shr : Out << " >> "; break;
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000717 default: std::cerr << "Invalid operator type!" << I; abort();
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000718 }
719
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000720 if (isa<PointerType>(I.getType())) Out << "(long long)";
721 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000722}
723
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000724void CWriter::visitCastInst(CastInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000725 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000726 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000727 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000728 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000729}
730
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000731void CWriter::visitCallInst(CallInst &I) {
732 const PointerType *PTy = cast<PointerType>(I.getCalledValue()->getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000733 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
734 const Type *RetTy = FTy->getReturnType();
735
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000736 Out << getValueName(I.getOperand(0)) << "(";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000737
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000738 if (I.getNumOperands() > 1) {
739 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000740
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000741 for (unsigned op = 2, Eop = I.getNumOperands(); op != Eop; ++op) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000742 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000743 writeOperand(I.getOperand(op));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000744 }
745 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000746 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000747}
748
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000749void CWriter::visitMallocInst(MallocInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000750 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000751 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000752 Out << ")malloc(sizeof(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000753 printType(I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000754 Out << ")";
755
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000756 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000757 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000758 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000759 }
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::visitAllocaInst(AllocaInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000764 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000765 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000766 Out << ") alloca(sizeof(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000767 printType(I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000768 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000769 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000770 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000771 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000772 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000773 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000774}
775
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000776void CWriter::visitFreeInst(FreeInst &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000777 Out << "free(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000778 writeOperand(I.getOperand(0));
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000779 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000780}
781
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000782void CWriter::printIndexingExpr(MemAccessInst &MAI) {
783 MemAccessInst::op_iterator I = MAI.idx_begin(), E = MAI.idx_end();
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000784 if (I == E) {
785 // If accessing a global value with no indexing, avoid *(&GV) syndrome
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000786 if (GlobalValue *V = dyn_cast<GlobalValue>(MAI.getPointerOperand())) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000787 writeOperandInternal(V);
788 return;
789 }
790
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000791 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000792 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000793
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000794 writeOperand(MAI.getPointerOperand());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000795
796 if (I == E) return;
797
798 // Print out the -> operator if possible...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000799 const Constant *CI = dyn_cast<Constant>(I->get());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000800 if (CI && CI->isNullValue() && I+1 != E &&
801 (*(I+1))->getType() == Type::UByteTy) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000802 Out << "->field" << cast<ConstantUInt>(*(I+1))->getValue();
803 I += 2;
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000804 }
805
806 for (; I != E; ++I)
807 if ((*I)->getType() == Type::UIntTy) {
808 Out << "[";
809 writeOperand(*I);
810 Out << "]";
811 } else {
812 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
813 }
814}
815
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000816void CWriter::visitLoadInst(LoadInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000817 printIndexingExpr(I);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000818}
819
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000820void CWriter::visitStoreInst(StoreInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000821 printIndexingExpr(I);
822 Out << " = ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000823 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000824}
825
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000826void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000827 Out << "&";
828 printIndexingExpr(I);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000829}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000830
831//===----------------------------------------------------------------------===//
832// External Interface declaration
833//===----------------------------------------------------------------------===//
834
Chris Lattnerf34ee812002-05-09 03:12:34 +0000835void WriteToC(const Module *M, ostream &Out) {
836 assert(M && "You can't write a null module!!");
837 SlotCalculator SlotTable(M, false);
838 CWriter W(Out, SlotTable, M);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000839 W.write((Module*)M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000840 Out.flush();
841}