blob: e52c15b0f178c9748b34cd87cdaa006683d3f94a [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 +000030namespace {
Chris Lattner4fbf26d2002-05-09 20:53:56 +000031 class CWriter : public InstVisitor<CWriter> {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000032 ostream& Out;
33 SlotCalculator &Table;
34 const Module *TheModule;
35 map<const Type *, string> TypeNames;
Chris Lattner78771c82002-05-17 04:55:35 +000036 std::set<const Value*> MangledGlobals;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000037 public:
38 inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M)
39 : Out(o), Table(Tab), TheModule(M) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000040 }
41
Chris Lattner8c3c4bf2002-05-09 15:49:41 +000042 inline void write(Module *M) { printModule(M); }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000043
Chris Lattner83c57752002-08-19 22:17:53 +000044 ostream &printType(const Type *Ty, const string &VariableName = "",
Vikram S. Adve969c4ad2002-08-25 20:00:08 +000045 bool IgnoreName = false, bool namedContext = true);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000046
Chris Lattnerbb03efd2002-06-25 15:57:03 +000047 void writeOperand(Value *Operand);
48 void writeOperandInternal(Value *Operand);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000049
50 string getValueName(const Value *V);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000051
Chris Lattner4fbf26d2002-05-09 20:53:56 +000052 private :
Chris Lattner8c3c4bf2002-05-09 15:49:41 +000053 void printModule(Module *M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000054 void printSymbolTable(const SymbolTable &ST);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000055 void printGlobal(const GlobalVariable *GV);
Chris Lattner4cda8352002-08-20 16:55:48 +000056 void printFunctionSignature(const Function *F, bool Prototype);
57
Chris Lattner8c3c4bf2002-05-09 15:49:41 +000058 void printFunction(Function *);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000059
Chris Lattner6d492922002-08-19 21:32:41 +000060 void printConstant(Constant *CPV);
61 void printConstantArray(ConstantArray *CPA);
62
Chris Lattnerd0c668c2002-05-09 21:18:38 +000063 // isInlinableInst - Attempt to inline instructions into their uses to build
64 // trees as much as possible. To do this, we have to consistently decide
65 // what is acceptable to inline, so that variable declarations don't get
66 // printed and an extra copy of the expr is not emitted.
67 //
Chris Lattnerbb03efd2002-06-25 15:57:03 +000068 static bool isInlinableInst(const Instruction &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +000069 // Must be an expression, must be used exactly once. If it is dead, we
70 // emit it inline where it would go.
Chris Lattnerbb03efd2002-06-25 15:57:03 +000071 if (I.getType() == Type::VoidTy || I.use_size() != 1 ||
Chris Lattnerd0c668c2002-05-09 21:18:38 +000072 isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I))
73 return false;
74
75 // Only inline instruction it it's use is in the same BB as the inst.
Chris Lattnerbb03efd2002-06-25 15:57:03 +000076 return I.getParent() == cast<Instruction>(I.use_back())->getParent();
Chris Lattnerd0c668c2002-05-09 21:18:38 +000077 }
78
Chris Lattner4fbf26d2002-05-09 20:53:56 +000079 // Instruction visitation functions
80 friend class InstVisitor<CWriter>;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000081
Chris Lattnerbb03efd2002-06-25 15:57:03 +000082 void visitReturnInst(ReturnInst &I);
83 void visitBranchInst(BranchInst &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000084
Chris Lattnerbb03efd2002-06-25 15:57:03 +000085 void visitPHINode(PHINode &I) {}
Chris Lattnerbb03efd2002-06-25 15:57:03 +000086 void visitBinaryOperator(Instruction &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000087
Chris Lattnerbb03efd2002-06-25 15:57:03 +000088 void visitCastInst (CastInst &I);
89 void visitCallInst (CallInst &I);
90 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Chris Lattner4fbf26d2002-05-09 20:53:56 +000091
Chris Lattnerbb03efd2002-06-25 15:57:03 +000092 void visitMallocInst(MallocInst &I);
93 void visitAllocaInst(AllocaInst &I);
94 void visitFreeInst (FreeInst &I);
95 void visitLoadInst (LoadInst &I);
96 void visitStoreInst (StoreInst &I);
97 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +000098
Chris Lattnerbb03efd2002-06-25 15:57:03 +000099 void visitInstruction(Instruction &I) {
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000100 std::cerr << "C Writer does not know about " << I;
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000101 abort();
102 }
103
104 void outputLValue(Instruction *I) {
105 Out << " " << getValueName(I) << " = ";
106 }
107 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
108 unsigned Indent);
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000109 void printIndexingExpression(Value *Ptr, User::op_iterator I,
110 User::op_iterator E);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000111 };
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000112}
113
Chris Lattner2f499022002-05-09 04:21:21 +0000114// We dont want identifier names with ., space, - in them.
115// So we replace them with _
116static string makeNameProper(string x) {
117 string tmp;
118 for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
119 switch (*sI) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000120 case '.': tmp += "d_"; break;
121 case ' ': tmp += "s_"; break;
122 case '-': tmp += "D_"; break;
Chris Lattner2f499022002-05-09 04:21:21 +0000123 default: tmp += *sI;
124 }
125
126 return tmp;
127}
128
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000129string CWriter::getValueName(const Value *V) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000130 if (V->hasName()) { // Print out the label if it exists...
131 if (isa<GlobalValue>(V) && // Do not mangle globals...
132 cast<GlobalValue>(V)->hasExternalLinkage() && // Unless it's internal or
133 !MangledGlobals.count(V)) // Unless the name would collide if we don't
Chris Lattner2f499022002-05-09 04:21:21 +0000134 return makeNameProper(V->getName());
135
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000136 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
137 makeNameProper(V->getName());
Chris Lattner2f499022002-05-09 04:21:21 +0000138 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000139
140 int Slot = Table.getValSlot(V);
141 assert(Slot >= 0 && "Invalid value!");
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000142 return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000143}
144
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000145// A pointer type should not use parens around *'s alone, e.g., (**)
146inline bool ptrTypeNameNeedsParens(const string &NameSoFar) {
147 return (NameSoFar.find_last_not_of('*') != std::string::npos);
148}
149
Chris Lattner83c57752002-08-19 22:17:53 +0000150// Pass the Type* and the variable name and this prints out the variable
151// declaration.
152//
153ostream &CWriter::printType(const Type *Ty, const string &NameSoFar,
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000154 bool IgnoreName, bool namedContext) {
Chris Lattner83c57752002-08-19 22:17:53 +0000155 if (Ty->isPrimitiveType())
156 switch (Ty->getPrimitiveID()) {
157 case Type::VoidTyID: return Out << "void " << NameSoFar;
158 case Type::BoolTyID: return Out << "bool " << NameSoFar;
159 case Type::UByteTyID: return Out << "unsigned char " << NameSoFar;
160 case Type::SByteTyID: return Out << "signed char " << NameSoFar;
161 case Type::UShortTyID: return Out << "unsigned short " << NameSoFar;
162 case Type::ShortTyID: return Out << "short " << NameSoFar;
163 case Type::UIntTyID: return Out << "unsigned " << NameSoFar;
164 case Type::IntTyID: return Out << "int " << NameSoFar;
165 case Type::ULongTyID: return Out << "unsigned long long " << NameSoFar;
166 case Type::LongTyID: return Out << "signed long long " << NameSoFar;
167 case Type::FloatTyID: return Out << "float " << NameSoFar;
168 case Type::DoubleTyID: return Out << "double " << NameSoFar;
169 default :
170 std::cerr << "Unknown primitive type: " << Ty << "\n";
171 abort();
172 }
173
174 // Check to see if the type is named.
175 if (!IgnoreName) {
176 map<const Type *, string>::iterator I = TypeNames.find(Ty);
177 if (I != TypeNames.end()) {
178 return Out << I->second << " " << NameSoFar;
179 }
180 }
181
Chris Lattner83c57752002-08-19 22:17:53 +0000182 switch (Ty->getPrimitiveID()) {
183 case Type::FunctionTyID: {
184 const FunctionType *MTy = cast<FunctionType>(Ty);
185 printType(MTy->getReturnType(), "");
186 Out << " " << NameSoFar << " (";
187
188 for (FunctionType::ParamTypes::const_iterator
189 I = MTy->getParamTypes().begin(),
190 E = MTy->getParamTypes().end(); I != E; ++I) {
191 if (I != MTy->getParamTypes().begin())
192 Out << ", ";
193 printType(*I, "");
194 }
195 if (MTy->isVarArg()) {
196 if (!MTy->getParamTypes().empty())
197 Out << ", ";
198 Out << "...";
199 }
200 return Out << ")";
201 }
202 case Type::StructTyID: {
203 const StructType *STy = cast<StructType>(Ty);
204 Out << NameSoFar + " {\n";
205 unsigned Idx = 0;
206 for (StructType::ElementTypes::const_iterator
207 I = STy->getElementTypes().begin(),
208 E = STy->getElementTypes().end(); I != E; ++I) {
209 Out << " ";
210 printType(*I, "field" + utostr(Idx++));
211 Out << ";\n";
212 }
213 return Out << "}";
214 }
215
216 case Type::PointerTyID: {
217 const PointerType *PTy = cast<PointerType>(Ty);
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000218 std::string ptrName = "*" + NameSoFar;
219
220 // Do not need parens around "* NameSoFar" if NameSoFar consists only
221 // of zero or more '*' chars *and* this is not an unnamed pointer type
222 // such as the result type in a cast statement. Otherwise, enclose in ( ).
223 if (ptrTypeNameNeedsParens(NameSoFar) || !namedContext)
224 ptrName = "(" + ptrName + ")"; //
225
Vikram S. Adve42eb2ba2002-08-24 14:44:23 +0000226 return printType(PTy->getElementType(), ptrName);
Chris Lattner83c57752002-08-19 22:17:53 +0000227 }
228
229 case Type::ArrayTyID: {
230 const ArrayType *ATy = cast<ArrayType>(Ty);
231 unsigned NumElements = ATy->getNumElements();
232 return printType(ATy->getElementType(),
233 NameSoFar + "[" + utostr(NumElements) + "]");
234 }
235 default:
236 assert(0 && "Unhandled case in getTypeProps!");
237 abort();
238 }
239
240 return Out;
241}
242
Chris Lattner6d492922002-08-19 21:32:41 +0000243void CWriter::printConstantArray(ConstantArray *CPA) {
244
245 // As a special case, print the array as a string if it is an array of
246 // ubytes or an array of sbytes with positive values.
247 //
248 const Type *ETy = CPA->getType()->getElementType();
249 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
250
251 // Make sure the last character is a null char, as automatically added by C
252 if (CPA->getNumOperands() == 0 ||
253 !cast<Constant>(*(CPA->op_end()-1))->isNullValue())
254 isString = false;
255
256 if (isString) {
257 Out << "\"";
258 // Do not include the last character, which we know is null
259 for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) {
260 unsigned char C = (ETy == Type::SByteTy) ?
261 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
262 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
263
264 if (isprint(C)) {
265 Out << C;
266 } else {
267 switch (C) {
268 case '\n': Out << "\\n"; break;
269 case '\t': Out << "\\t"; break;
270 case '\r': Out << "\\r"; break;
271 case '\v': Out << "\\v"; break;
272 case '\a': Out << "\\a"; break;
273 default:
274 Out << "\\x";
275 Out << ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
276 Out << ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
277 break;
278 }
279 }
280 }
281 Out << "\"";
282 } else {
283 Out << "{";
284 if (CPA->getNumOperands()) {
285 Out << " ";
286 printConstant(cast<Constant>(CPA->getOperand(0)));
287 for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
288 Out << ", ";
289 printConstant(cast<Constant>(CPA->getOperand(i)));
290 }
291 }
292 Out << " }";
293 }
294}
295
296
297// printConstant - The LLVM Constant to C Constant converter.
298void CWriter::printConstant(Constant *CPV) {
299 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
300 switch (CE->getOpcode()) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000301 case Instruction::Cast:
302 Out << "((";
303 printType(CPV->getType());
304 Out << ")";
305 printConstant(cast<Constant>(CPV->getOperand(0)));
306 Out << ")";
307 return;
308
309 case Instruction::GetElementPtr:
310 Out << "&(";
311 printIndexingExpression(CPV->getOperand(0),
312 CPV->op_begin()+1, CPV->op_end());
313 Out << ")";
314 return;
315 case Instruction::Add:
316 Out << "(";
317 printConstant(cast<Constant>(CPV->getOperand(0)));
318 Out << " + ";
319 printConstant(cast<Constant>(CPV->getOperand(1)));
320 Out << ")";
321 return;
322 case Instruction::Sub:
323 Out << "(";
324 printConstant(cast<Constant>(CPV->getOperand(0)));
325 Out << " - ";
326 printConstant(cast<Constant>(CPV->getOperand(1)));
327 Out << ")";
328 return;
329
Chris Lattner6d492922002-08-19 21:32:41 +0000330 default:
331 std::cerr << "CWriter Error: Unhandled constant expression: "
332 << CE << "\n";
333 abort();
334 }
335 }
336
337 switch (CPV->getType()->getPrimitiveID()) {
338 case Type::BoolTyID:
339 Out << (CPV == ConstantBool::False ? "0" : "1"); break;
340 case Type::SByteTyID:
341 case Type::ShortTyID:
342 case Type::IntTyID:
343 Out << cast<ConstantSInt>(CPV)->getValue(); break;
344 case Type::LongTyID:
345 Out << cast<ConstantSInt>(CPV)->getValue() << "ll"; break;
346
347 case Type::UByteTyID:
348 case Type::UShortTyID:
349 Out << cast<ConstantUInt>(CPV)->getValue(); break;
350 case Type::UIntTyID:
351 Out << cast<ConstantUInt>(CPV)->getValue() << "u"; break;
352 case Type::ULongTyID:
353 Out << cast<ConstantUInt>(CPV)->getValue() << "ull"; break;
354
355 case Type::FloatTyID:
356 case Type::DoubleTyID:
357 Out << cast<ConstantFP>(CPV)->getValue(); break;
358
359 case Type::ArrayTyID:
360 printConstantArray(cast<ConstantArray>(CPV));
361 break;
362
363 case Type::StructTyID: {
364 Out << "{";
365 if (CPV->getNumOperands()) {
366 Out << " ";
367 printConstant(cast<Constant>(CPV->getOperand(0)));
368 for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) {
369 Out << ", ";
370 printConstant(cast<Constant>(CPV->getOperand(i)));
371 }
372 }
373 Out << " }";
374 break;
375 }
376
377 case Type::PointerTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000378 if (isa<ConstantPointerNull>(CPV)) {
379 Out << "((";
Chris Lattner3af3ba82002-05-09 21:31:18 +0000380 printType(CPV->getType(), "");
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000381 Out << ")NULL)";
Chris Lattner6d492922002-08-19 21:32:41 +0000382 break;
383 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
384 writeOperand(CPR->getValue());
385 break;
386 }
387 // FALL THROUGH
388 default:
389 std::cerr << "Unknown constant type: " << CPV << "\n";
390 abort();
391 }
392}
393
394void CWriter::writeOperandInternal(Value *Operand) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000395 if (Instruction *I = dyn_cast<Instruction>(Operand))
396 if (isInlinableInst(*I)) {
397 // Should we inline this instruction to build a tree?
398 Out << "(";
399 visit(*I);
400 Out << ")";
401 return;
402 }
403
Chris Lattner6d492922002-08-19 21:32:41 +0000404 if (Operand->hasName()) {
405 Out << getValueName(Operand);
406 } else if (Constant *CPV = dyn_cast<Constant>(Operand)) {
407 printConstant(CPV);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000408 } else {
409 int Slot = Table.getValSlot(Operand);
410 assert(Slot >= 0 && "Malformed LLVM!");
411 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
412 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000413}
414
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000415void CWriter::writeOperand(Value *Operand) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000416 if (isa<GlobalVariable>(Operand))
417 Out << "(&"; // Global variables are references as their addresses by llvm
418
419 writeOperandInternal(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000420
421 if (isa<GlobalVariable>(Operand))
422 Out << ")";
423}
424
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000425void CWriter::printModule(Module *M) {
Chris Lattner78771c82002-05-17 04:55:35 +0000426 // Calculate which global values have names that will collide when we throw
427 // away type information.
Chris Lattner594b9fa2002-05-21 21:10:04 +0000428 { // Scope to delete the FoundNames set when we are done with it...
Chris Lattner78771c82002-05-17 04:55:35 +0000429 std::set<string> FoundNames;
430 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000431 if (I->hasName()) // If the global has a name...
432 if (FoundNames.count(I->getName())) // And the name is already used
433 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000434 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000435 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000436
437 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000438 if (I->hasName()) // If the global has a name...
439 if (FoundNames.count(I->getName())) // And the name is already used
440 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000441 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000442 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000443 }
444
445
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000446 // printing stdlib inclusion
447 // Out << "#include <stdlib.h>\n";
448
Chris Lattner16c7bb22002-05-09 02:28:59 +0000449 // get declaration for alloca
450 Out << "/* Provide Declarations */\n"
Chris Lattner594b9fa2002-05-21 21:10:04 +0000451 << "#include <malloc.h>\n"
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000452 << "#include <alloca.h>\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000453
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000454 // Provide a definition for null if one does not already exist,
455 // and for `bool' if not compiling with a C++ compiler.
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000456 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000457 << "#ifndef __cplusplus\ntypedef unsigned char bool;\n#endif\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000458
Chris Lattnera4d4a852002-08-19 21:48:40 +0000459 << "\n\n/* Global Declarations */\n";
460
461 // First output all the declarations for the program, because C requires
462 // Functions & globals to be declared before they are used.
463 //
Chris Lattner16c7bb22002-05-09 02:28:59 +0000464
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000465 // Loop over the symbol table, emitting all named constants...
466 if (M->hasSymbolTable())
467 printSymbolTable(*M->getSymbolTable());
468
Chris Lattnera4d4a852002-08-19 21:48:40 +0000469 // Global variable declarations...
470 if (!M->gempty()) {
471 Out << "\n/* Global Variable Declarations */\n";
472 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
473 Out << (I->hasExternalLinkage() ? "extern " : "static ");
474 printType(I->getType()->getElementType(), getValueName(I));
475 Out << ";\n";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000476 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000477 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000478
Chris Lattnera4d4a852002-08-19 21:48:40 +0000479 // Function declarations
480 if (!M->empty()) {
481 Out << "\n/* Function Declarations */\n";
Chris Lattner4cda8352002-08-20 16:55:48 +0000482 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
483 printFunctionSignature(I, true);
484 Out << ";\n";
485 }
Chris Lattnera4d4a852002-08-19 21:48:40 +0000486 }
487
488 // Output the global variable contents...
489 if (!M->gempty()) {
490 Out << "\n\n/* Global Data */\n";
491 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
492 if (I->hasInternalLinkage()) Out << "static ";
493 printType(I->getType()->getElementType(), getValueName(I));
494
495 if (I->hasInitializer()) {
496 Out << " = " ;
497 writeOperand(I->getInitializer());
498 }
499 Out << ";\n";
500 }
501 }
502
Chris Lattner16c7bb22002-05-09 02:28:59 +0000503 // Output all of the functions...
Chris Lattnera4d4a852002-08-19 21:48:40 +0000504 if (!M->empty()) {
505 Out << "\n\n/* Function Bodies */\n";
506 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
507 printFunction(I);
508 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000509}
510
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000511
512// printSymbolTable - Run through symbol table looking for named constants
513// if a named constant is found, emit it's declaration...
514// Assuming that symbol table has only types and constants.
515void CWriter::printSymbolTable(const SymbolTable &ST) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000516 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
517 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
518 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
519
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000520 for (; I != End; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000521 if (const Type *Ty = dyn_cast<StructType>(I->second)) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000522 string Name = "struct l_" + makeNameProper(I->first);
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000523 Out << Name << ";\n";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000524 TypeNames.insert(std::make_pair(Ty, Name));
525 }
526 }
527
528 Out << "\n";
529
530 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
531 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
532 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
533
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000534 for (; I != End; ++I) {
535 const Value *V = I->second;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000536 if (const Type *Ty = dyn_cast<Type>(V)) {
537 string Name = "l_" + makeNameProper(I->first);
Chris Lattner1f02c892002-05-09 20:14:10 +0000538 if (isa<StructType>(Ty))
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000539 Name = "struct " + makeNameProper(Name);
Chris Lattner1f02c892002-05-09 20:14:10 +0000540 else
541 Out << "typedef ";
542
Chris Lattner83c57752002-08-19 22:17:53 +0000543 printType(Ty, Name, true);
544 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000545 }
546 }
547 }
548}
549
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000550
Chris Lattner4cda8352002-08-20 16:55:48 +0000551void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000552 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000553
554 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000555 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000556
Chris Lattner16c7bb22002-05-09 02:28:59 +0000557 // Print out the return type and name...
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000558 printType(F->getReturnType());
Chris Lattner1f02c892002-05-09 20:14:10 +0000559 Out << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000560
Chris Lattner16c7bb22002-05-09 02:28:59 +0000561 if (!F->isExternal()) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000562 if (!F->aempty()) {
Chris Lattner4cda8352002-08-20 16:55:48 +0000563 string ArgName;
564 if (F->abegin()->hasName() || !Prototype)
565 ArgName = getValueName(F->abegin());
566
567 printType(F->afront().getType(), ArgName);
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000568
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000569 for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
570 I != E; ++I) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000571 Out << ", ";
Chris Lattner4cda8352002-08-20 16:55:48 +0000572 if (I->hasName() || !Prototype)
573 ArgName = getValueName(I);
574 else
575 ArgName = "";
576 printType(I->getType(), ArgName);
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000577 }
578 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000579 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000580 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000581 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000582 FT->getParamTypes().begin(),
583 E = FT->getParamTypes().end(); I != E; ++I) {
584 if (I != FT->getParamTypes().begin()) Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000585 printType(*I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000586 }
587 }
588
589 // Finish printing arguments...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000590 if (FT->isVarArg()) {
591 if (FT->getParamTypes().size()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000592 Out << "..."; // Output varargs portion of signature!
593 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000594 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000595}
596
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000597
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000598void CWriter::printFunction(Function *F) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000599 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000600
Chris Lattnerf34ee812002-05-09 03:12:34 +0000601 Table.incorporateFunction(F);
602
Chris Lattner4cda8352002-08-20 16:55:48 +0000603 printFunctionSignature(F, false);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000604 Out << " {\n";
605
Chris Lattner497e19a2002-05-09 20:39:03 +0000606 // print local variable information for the function
Chris Lattner7683a122002-05-09 20:33:35 +0000607 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000608 if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000609 Out << " ";
Chris Lattner3af3ba82002-05-09 21:31:18 +0000610 printType((*I)->getType(), getValueName(*I));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000611 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000612 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000613
614 // print the basic blocks
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000615 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
616 BasicBlock *Prev = BB->getPrev();
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000617
618 // Don't print the label for the basic block if there are no uses, or if the
619 // only terminator use is the precessor basic block's terminator. We have
620 // to scan the use list because PHI nodes use basic blocks too but do not
621 // require a label to be generated.
622 //
623 bool NeedsLabel = false;
624 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
625 UI != UE; ++UI)
626 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
627 if (TI != Prev->getTerminator()) {
628 NeedsLabel = true;
629 break;
630 }
631
632 if (NeedsLabel) Out << getValueName(BB) << ":\n";
633
634 // Output all of the instructions in the basic block...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000635 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II){
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000636 if (!isInlinableInst(*II) && !isa<PHINode>(*II)) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000637 if (II->getType() != Type::VoidTy)
638 outputLValue(II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000639 else
640 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000641 visit(*II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000642 Out << ";\n";
643 }
644 }
645
646 // Don't emit prefix or suffix for the terminator...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000647 visit(*BB->getTerminator());
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000648 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000649
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000650 Out << "}\n\n";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000651 Table.purgeFunction();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000652}
653
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000654// Specific Instruction type classes... note that all of the casts are
655// neccesary because we use the instruction classes as opaque types...
656//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000657void CWriter::visitReturnInst(ReturnInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000658 // Don't output a void return if this is the last basic block in the function
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000659 if (I.getNumOperands() == 0 &&
660 &*--I.getParent()->getParent()->end() == I.getParent() &&
661 !I.getParent()->size() == 1) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000662 return;
Chris Lattner963b70b2002-05-21 18:05:19 +0000663 }
Chris Lattner44408262002-05-09 03:50:42 +0000664
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000665 Out << " return";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000666 if (I.getNumOperands()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000667 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000668 writeOperand(I.getOperand(0));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000669 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000670 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000671}
672
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000673static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
674 // If PHI nodes need copies, we need the copy code...
675 if (isa<PHINode>(To->front()) ||
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000676 From->getNext() != To) // Not directly successor, need goto
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000677 return true;
678
679 // Otherwise we don't need the code.
680 return false;
681}
682
683void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
684 unsigned Indent) {
685 for (BasicBlock::iterator I = Succ->begin();
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000686 PHINode *PN = dyn_cast<PHINode>(&*I); ++I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000687 // now we have to do the printing
688 Out << string(Indent, ' ');
689 outputLValue(PN);
690 writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
691 Out << "; /* for PHI node */\n";
692 }
693
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000694 if (CurBB->getNext() != Succ) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000695 Out << string(Indent, ' ') << " goto ";
696 writeOperand(Succ);
697 Out << ";\n";
698 }
699}
700
701// Brach instruction printing - Avoid printing out a brach to a basic block that
702// immediately succeeds the current one.
703//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000704void CWriter::visitBranchInst(BranchInst &I) {
705 if (I.isConditional()) {
706 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(0))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000707 Out << " if (";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000708 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000709 Out << ") {\n";
710
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000711 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000712
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000713 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(1))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000714 Out << " } else {\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000715 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000716 }
717 } else {
718 // First goto not neccesary, assume second one is...
719 Out << " if (!";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000720 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000721 Out << ") {\n";
722
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000723 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000724 }
725
726 Out << " }\n";
727 } else {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000728 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000729 }
730 Out << "\n";
731}
732
733
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000734void CWriter::visitBinaryOperator(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000735 // binary instructions, shift instructions, setCond instructions.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000736 if (isa<PointerType>(I.getType())) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000737 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000738 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000739 Out << ")";
740 }
741
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000742 if (isa<PointerType>(I.getType())) Out << "(long long)";
743 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000744
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000745 switch (I.getOpcode()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000746 case Instruction::Add: Out << " + "; break;
747 case Instruction::Sub: Out << " - "; break;
748 case Instruction::Mul: Out << "*"; break;
749 case Instruction::Div: Out << "/"; break;
750 case Instruction::Rem: Out << "%"; break;
751 case Instruction::And: Out << " & "; break;
752 case Instruction::Or: Out << " | "; break;
753 case Instruction::Xor: Out << " ^ "; break;
754 case Instruction::SetEQ: Out << " == "; break;
755 case Instruction::SetNE: Out << " != "; break;
756 case Instruction::SetLE: Out << " <= "; break;
757 case Instruction::SetGE: Out << " >= "; break;
758 case Instruction::SetLT: Out << " < "; break;
759 case Instruction::SetGT: Out << " > "; break;
760 case Instruction::Shl : Out << " << "; break;
761 case Instruction::Shr : Out << " >> "; break;
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000762 default: std::cerr << "Invalid operator type!" << I; abort();
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000763 }
764
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000765 if (isa<PointerType>(I.getType())) Out << "(long long)";
766 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000767}
768
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000769void CWriter::visitCastInst(CastInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000770 Out << "(";
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000771 printType(I.getType(), string(""),/*ignoreName*/false, /*namedContext*/false);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000772 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000773 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000774}
775
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000776void CWriter::visitCallInst(CallInst &I) {
777 const PointerType *PTy = cast<PointerType>(I.getCalledValue()->getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000778 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
779 const Type *RetTy = FTy->getReturnType();
780
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000781 Out << getValueName(I.getOperand(0)) << "(";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000782
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000783 if (I.getNumOperands() > 1) {
784 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000785
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000786 for (unsigned op = 2, Eop = I.getNumOperands(); op != Eop; ++op) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000787 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000788 writeOperand(I.getOperand(op));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000789 }
790 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000791 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000792}
793
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000794void CWriter::visitMallocInst(MallocInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000795 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000796 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000797 Out << ")malloc(sizeof(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000798 printType(I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000799 Out << ")";
800
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000801 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000802 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000803 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000804 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000805 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000806}
807
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000808void CWriter::visitAllocaInst(AllocaInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000809 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000810 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000811 Out << ") alloca(sizeof(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000812 printType(I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000813 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000814 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000815 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000816 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000817 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000818 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000819}
820
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000821void CWriter::visitFreeInst(FreeInst &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000822 Out << "free(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000823 writeOperand(I.getOperand(0));
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000824 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000825}
826
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000827void CWriter::printIndexingExpression(Value *Ptr, User::op_iterator I,
828 User::op_iterator E) {
829 bool HasImplicitAddress = false;
830 // If accessing a global value with no indexing, avoid *(&GV) syndrome
831 if (GlobalValue *V = dyn_cast<GlobalValue>(Ptr)) {
832 HasImplicitAddress = true;
833 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Ptr)) {
834 HasImplicitAddress = true;
835 Ptr = CPR->getValue(); // Get to the global...
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000836 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000837
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000838 if (I == E) {
839 if (!HasImplicitAddress)
840 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000841
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000842 writeOperandInternal(Ptr);
843 return;
844 }
845
846 const Constant *CI = dyn_cast<Constant>(I->get());
847 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
848 Out << "(&";
849
850 writeOperandInternal(Ptr);
851
852 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
853 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000854
855 // Print out the -> operator if possible...
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000856 if (CI && CI->isNullValue() && I+1 != E) {
857 if ((*(I+1))->getType() == Type::UByteTy) {
858 Out << (HasImplicitAddress ? "." : "->");
859 Out << "field" << cast<ConstantUInt>(*(I+1))->getValue();
860 I += 2;
Vikram S. Adve42eb2ba2002-08-24 14:44:23 +0000861 } else { // First array index of 0: Just skip it
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000862 ++I;
863 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000864 }
Chris Lattner5dfe7672002-08-22 22:48:55 +0000865
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000866 for (; I != E; ++I)
867 if ((*I)->getType() == Type::UIntTy) {
Vikram S. Adve42eb2ba2002-08-24 14:44:23 +0000868 Out << "[((int) ("; // sign-extend from 32 (to 64) bits
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000869 writeOperand(*I);
Vikram S. Adve42eb2ba2002-08-24 14:44:23 +0000870 Out << " * sizeof(";
871 printType(cast<PointerType>(Ptr->getType())->getElementType());
872 Out << "))) / sizeof(";
873 printType(cast<PointerType>(Ptr->getType())->getElementType());
874 Out << ")]";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000875 } else {
876 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
877 }
878}
879
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000880void CWriter::visitLoadInst(LoadInst &I) {
Chris Lattner5dfe7672002-08-22 22:48:55 +0000881 Out << "*";
882 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000883}
884
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000885void CWriter::visitStoreInst(StoreInst &I) {
Chris Lattner5dfe7672002-08-22 22:48:55 +0000886 Out << "*";
887 writeOperand(I.getPointerOperand());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000888 Out << " = ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000889 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000890}
891
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000892void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000893 Out << "&";
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000894 printIndexingExpression(I.getPointerOperand(), I.idx_begin(), I.idx_end());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000895}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000896
897//===----------------------------------------------------------------------===//
898// External Interface declaration
899//===----------------------------------------------------------------------===//
900
Chris Lattnerf34ee812002-05-09 03:12:34 +0000901void WriteToC(const Module *M, ostream &Out) {
902 assert(M && "You can't write a null module!!");
903 SlotCalculator SlotTable(M, false);
904 CWriter W(Out, SlotTable, M);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000905 W.write((Module*)M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000906 Out.flush();
907}