blob: c706d089ba244188520241defb3d6081d495c6f8 [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 = "",
45 bool IgnoreName = false);
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 Lattner16c7bb22002-05-09 02:28:59 +000056 void printFunctionSignature(const Function *F);
57 void printFunctionDecl(const Function *F); // Print just the forward decl
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000058
Chris Lattner8c3c4bf2002-05-09 15:49:41 +000059 void printFunction(Function *);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000060
Chris Lattner6d492922002-08-19 21:32:41 +000061 void printConstant(Constant *CPV);
62 void printConstantArray(ConstantArray *CPA);
63
Chris Lattnerd0c668c2002-05-09 21:18:38 +000064 // isInlinableInst - Attempt to inline instructions into their uses to build
65 // trees as much as possible. To do this, we have to consistently decide
66 // what is acceptable to inline, so that variable declarations don't get
67 // printed and an extra copy of the expr is not emitted.
68 //
Chris Lattnerbb03efd2002-06-25 15:57:03 +000069 static bool isInlinableInst(const Instruction &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +000070 // Must be an expression, must be used exactly once. If it is dead, we
71 // emit it inline where it would go.
Chris Lattnerbb03efd2002-06-25 15:57:03 +000072 if (I.getType() == Type::VoidTy || I.use_size() != 1 ||
Chris Lattnerd0c668c2002-05-09 21:18:38 +000073 isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I))
74 return false;
75
76 // Only inline instruction it it's use is in the same BB as the inst.
Chris Lattnerbb03efd2002-06-25 15:57:03 +000077 return I.getParent() == cast<Instruction>(I.use_back())->getParent();
Chris Lattnerd0c668c2002-05-09 21:18:38 +000078 }
79
Chris Lattner4fbf26d2002-05-09 20:53:56 +000080 // Instruction visitation functions
81 friend class InstVisitor<CWriter>;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000082
Chris Lattnerbb03efd2002-06-25 15:57:03 +000083 void visitReturnInst(ReturnInst &I);
84 void visitBranchInst(BranchInst &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000085
Chris Lattnerbb03efd2002-06-25 15:57:03 +000086 void visitPHINode(PHINode &I) {}
Chris Lattnerbb03efd2002-06-25 15:57:03 +000087 void visitBinaryOperator(Instruction &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000088
Chris Lattnerbb03efd2002-06-25 15:57:03 +000089 void visitCastInst (CastInst &I);
90 void visitCallInst (CallInst &I);
91 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Chris Lattner4fbf26d2002-05-09 20:53:56 +000092
Chris Lattnerbb03efd2002-06-25 15:57:03 +000093 void visitMallocInst(MallocInst &I);
94 void visitAllocaInst(AllocaInst &I);
95 void visitFreeInst (FreeInst &I);
96 void visitLoadInst (LoadInst &I);
97 void visitStoreInst (StoreInst &I);
98 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +000099
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000100 void visitInstruction(Instruction &I) {
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000101 std::cerr << "C Writer does not know about " << I;
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000102 abort();
103 }
104
105 void outputLValue(Instruction *I) {
106 Out << " " << getValueName(I) << " = ";
107 }
108 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
109 unsigned Indent);
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000110 void printIndexingExpression(Value *Ptr, User::op_iterator I,
111 User::op_iterator E);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000112 };
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000113}
114
Chris Lattner2f499022002-05-09 04:21:21 +0000115// We dont want identifier names with ., space, - in them.
116// So we replace them with _
117static string makeNameProper(string x) {
118 string tmp;
119 for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
120 switch (*sI) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000121 case '.': tmp += "d_"; break;
122 case ' ': tmp += "s_"; break;
123 case '-': tmp += "D_"; break;
Chris Lattner2f499022002-05-09 04:21:21 +0000124 default: tmp += *sI;
125 }
126
127 return tmp;
128}
129
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000130string CWriter::getValueName(const Value *V) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000131 if (V->hasName()) { // Print out the label if it exists...
132 if (isa<GlobalValue>(V) && // Do not mangle globals...
133 cast<GlobalValue>(V)->hasExternalLinkage() && // Unless it's internal or
134 !MangledGlobals.count(V)) // Unless the name would collide if we don't
Chris Lattner2f499022002-05-09 04:21:21 +0000135 return makeNameProper(V->getName());
136
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000137 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
138 makeNameProper(V->getName());
Chris Lattner2f499022002-05-09 04:21:21 +0000139 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000140
141 int Slot = Table.getValSlot(V);
142 assert(Slot >= 0 && "Invalid value!");
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000143 return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000144}
145
Chris Lattner83c57752002-08-19 22:17:53 +0000146// Pass the Type* and the variable name and this prints out the variable
147// declaration.
148//
149ostream &CWriter::printType(const Type *Ty, const string &NameSoFar,
150 bool IgnoreName = false) {
151 if (Ty->isPrimitiveType())
152 switch (Ty->getPrimitiveID()) {
153 case Type::VoidTyID: return Out << "void " << NameSoFar;
154 case Type::BoolTyID: return Out << "bool " << NameSoFar;
155 case Type::UByteTyID: return Out << "unsigned char " << NameSoFar;
156 case Type::SByteTyID: return Out << "signed char " << NameSoFar;
157 case Type::UShortTyID: return Out << "unsigned short " << NameSoFar;
158 case Type::ShortTyID: return Out << "short " << NameSoFar;
159 case Type::UIntTyID: return Out << "unsigned " << NameSoFar;
160 case Type::IntTyID: return Out << "int " << NameSoFar;
161 case Type::ULongTyID: return Out << "unsigned long long " << NameSoFar;
162 case Type::LongTyID: return Out << "signed long long " << NameSoFar;
163 case Type::FloatTyID: return Out << "float " << NameSoFar;
164 case Type::DoubleTyID: return Out << "double " << NameSoFar;
165 default :
166 std::cerr << "Unknown primitive type: " << Ty << "\n";
167 abort();
168 }
169
170 // Check to see if the type is named.
171 if (!IgnoreName) {
172 map<const Type *, string>::iterator I = TypeNames.find(Ty);
173 if (I != TypeNames.end()) {
174 return Out << I->second << " " << NameSoFar;
175 }
176 }
177
Chris Lattner83c57752002-08-19 22:17:53 +0000178 switch (Ty->getPrimitiveID()) {
179 case Type::FunctionTyID: {
180 const FunctionType *MTy = cast<FunctionType>(Ty);
181 printType(MTy->getReturnType(), "");
182 Out << " " << NameSoFar << " (";
183
184 for (FunctionType::ParamTypes::const_iterator
185 I = MTy->getParamTypes().begin(),
186 E = MTy->getParamTypes().end(); I != E; ++I) {
187 if (I != MTy->getParamTypes().begin())
188 Out << ", ";
189 printType(*I, "");
190 }
191 if (MTy->isVarArg()) {
192 if (!MTy->getParamTypes().empty())
193 Out << ", ";
194 Out << "...";
195 }
196 return Out << ")";
197 }
198 case Type::StructTyID: {
199 const StructType *STy = cast<StructType>(Ty);
200 Out << NameSoFar + " {\n";
201 unsigned Idx = 0;
202 for (StructType::ElementTypes::const_iterator
203 I = STy->getElementTypes().begin(),
204 E = STy->getElementTypes().end(); I != E; ++I) {
205 Out << " ";
206 printType(*I, "field" + utostr(Idx++));
207 Out << ";\n";
208 }
209 return Out << "}";
210 }
211
212 case Type::PointerTyID: {
213 const PointerType *PTy = cast<PointerType>(Ty);
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000214 return printType(PTy->getElementType(), "(*" + NameSoFar + ")");
Chris Lattner83c57752002-08-19 22:17:53 +0000215 }
216
217 case Type::ArrayTyID: {
218 const ArrayType *ATy = cast<ArrayType>(Ty);
219 unsigned NumElements = ATy->getNumElements();
220 return printType(ATy->getElementType(),
221 NameSoFar + "[" + utostr(NumElements) + "]");
222 }
223 default:
224 assert(0 && "Unhandled case in getTypeProps!");
225 abort();
226 }
227
228 return Out;
229}
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()) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000289 case Instruction::Cast:
290 Out << "((";
291 printType(CPV->getType());
292 Out << ")";
293 printConstant(cast<Constant>(CPV->getOperand(0)));
294 Out << ")";
295 return;
296
297 case Instruction::GetElementPtr:
298 Out << "&(";
299 printIndexingExpression(CPV->getOperand(0),
300 CPV->op_begin()+1, CPV->op_end());
301 Out << ")";
302 return;
303 case Instruction::Add:
304 Out << "(";
305 printConstant(cast<Constant>(CPV->getOperand(0)));
306 Out << " + ";
307 printConstant(cast<Constant>(CPV->getOperand(1)));
308 Out << ")";
309 return;
310 case Instruction::Sub:
311 Out << "(";
312 printConstant(cast<Constant>(CPV->getOperand(0)));
313 Out << " - ";
314 printConstant(cast<Constant>(CPV->getOperand(1)));
315 Out << ")";
316 return;
317
Chris Lattner6d492922002-08-19 21:32:41 +0000318 default:
319 std::cerr << "CWriter Error: Unhandled constant expression: "
320 << CE << "\n";
321 abort();
322 }
323 }
324
325 switch (CPV->getType()->getPrimitiveID()) {
326 case Type::BoolTyID:
327 Out << (CPV == ConstantBool::False ? "0" : "1"); break;
328 case Type::SByteTyID:
329 case Type::ShortTyID:
330 case Type::IntTyID:
331 Out << cast<ConstantSInt>(CPV)->getValue(); break;
332 case Type::LongTyID:
333 Out << cast<ConstantSInt>(CPV)->getValue() << "ll"; break;
334
335 case Type::UByteTyID:
336 case Type::UShortTyID:
337 Out << cast<ConstantUInt>(CPV)->getValue(); break;
338 case Type::UIntTyID:
339 Out << cast<ConstantUInt>(CPV)->getValue() << "u"; break;
340 case Type::ULongTyID:
341 Out << cast<ConstantUInt>(CPV)->getValue() << "ull"; break;
342
343 case Type::FloatTyID:
344 case Type::DoubleTyID:
345 Out << cast<ConstantFP>(CPV)->getValue(); break;
346
347 case Type::ArrayTyID:
348 printConstantArray(cast<ConstantArray>(CPV));
349 break;
350
351 case Type::StructTyID: {
352 Out << "{";
353 if (CPV->getNumOperands()) {
354 Out << " ";
355 printConstant(cast<Constant>(CPV->getOperand(0)));
356 for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) {
357 Out << ", ";
358 printConstant(cast<Constant>(CPV->getOperand(i)));
359 }
360 }
361 Out << " }";
362 break;
363 }
364
365 case Type::PointerTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000366 if (isa<ConstantPointerNull>(CPV)) {
367 Out << "((";
Chris Lattner3af3ba82002-05-09 21:31:18 +0000368 printType(CPV->getType(), "");
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000369 Out << ")NULL)";
Chris Lattner6d492922002-08-19 21:32:41 +0000370 break;
371 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
372 writeOperand(CPR->getValue());
373 break;
374 }
375 // FALL THROUGH
376 default:
377 std::cerr << "Unknown constant type: " << CPV << "\n";
378 abort();
379 }
380}
381
382void CWriter::writeOperandInternal(Value *Operand) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000383 if (Instruction *I = dyn_cast<Instruction>(Operand))
384 if (isInlinableInst(*I)) {
385 // Should we inline this instruction to build a tree?
386 Out << "(";
387 visit(*I);
388 Out << ")";
389 return;
390 }
391
Chris Lattner6d492922002-08-19 21:32:41 +0000392 if (Operand->hasName()) {
393 Out << getValueName(Operand);
394 } else if (Constant *CPV = dyn_cast<Constant>(Operand)) {
395 printConstant(CPV);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000396 } else {
397 int Slot = Table.getValSlot(Operand);
398 assert(Slot >= 0 && "Malformed LLVM!");
399 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
400 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000401}
402
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000403void CWriter::writeOperand(Value *Operand) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000404 if (isa<GlobalVariable>(Operand))
405 Out << "(&"; // Global variables are references as their addresses by llvm
406
407 writeOperandInternal(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000408
409 if (isa<GlobalVariable>(Operand))
410 Out << ")";
411}
412
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000413void CWriter::printModule(Module *M) {
Chris Lattner78771c82002-05-17 04:55:35 +0000414 // Calculate which global values have names that will collide when we throw
415 // away type information.
Chris Lattner594b9fa2002-05-21 21:10:04 +0000416 { // Scope to delete the FoundNames set when we are done with it...
Chris Lattner78771c82002-05-17 04:55:35 +0000417 std::set<string> FoundNames;
418 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000419 if (I->hasName()) // If the global has a name...
420 if (FoundNames.count(I->getName())) // And the name is already used
421 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000422 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000423 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000424
425 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000426 if (I->hasName()) // If the global has a name...
427 if (FoundNames.count(I->getName())) // And the name is already used
428 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000429 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000430 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000431 }
432
433
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000434 // printing stdlib inclusion
435 // Out << "#include <stdlib.h>\n";
436
Chris Lattner16c7bb22002-05-09 02:28:59 +0000437 // get declaration for alloca
438 Out << "/* Provide Declarations */\n"
Chris Lattner594b9fa2002-05-21 21:10:04 +0000439 << "#include <malloc.h>\n"
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000440 << "#include <alloca.h>\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000441
442 // Provide a definition for null if one does not already exist.
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000443 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000444 << "typedef unsigned char bool;\n"
445
Chris Lattnera4d4a852002-08-19 21:48:40 +0000446 << "\n\n/* Global Declarations */\n";
447
448 // First output all the declarations for the program, because C requires
449 // Functions & globals to be declared before they are used.
450 //
Chris Lattner16c7bb22002-05-09 02:28:59 +0000451
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000452 // Loop over the symbol table, emitting all named constants...
453 if (M->hasSymbolTable())
454 printSymbolTable(*M->getSymbolTable());
455
Chris Lattnera4d4a852002-08-19 21:48:40 +0000456 // Global variable declarations...
457 if (!M->gempty()) {
458 Out << "\n/* Global Variable Declarations */\n";
459 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
460 Out << (I->hasExternalLinkage() ? "extern " : "static ");
461 printType(I->getType()->getElementType(), getValueName(I));
462 Out << ";\n";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000463 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000464 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000465
Chris Lattnera4d4a852002-08-19 21:48:40 +0000466 // Function declarations
467 if (!M->empty()) {
468 Out << "\n/* Function Declarations */\n";
469 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
470 printFunctionDecl(I);
471 }
472
473 // Output the global variable contents...
474 if (!M->gempty()) {
475 Out << "\n\n/* Global Data */\n";
476 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
477 if (I->hasInternalLinkage()) Out << "static ";
478 printType(I->getType()->getElementType(), getValueName(I));
479
480 if (I->hasInitializer()) {
481 Out << " = " ;
482 writeOperand(I->getInitializer());
483 }
484 Out << ";\n";
485 }
486 }
487
Chris Lattner16c7bb22002-05-09 02:28:59 +0000488 // Output all of the functions...
Chris Lattnera4d4a852002-08-19 21:48:40 +0000489 if (!M->empty()) {
490 Out << "\n\n/* Function Bodies */\n";
491 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
492 printFunction(I);
493 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000494}
495
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000496
497// printSymbolTable - Run through symbol table looking for named constants
498// if a named constant is found, emit it's declaration...
499// Assuming that symbol table has only types and constants.
500void CWriter::printSymbolTable(const SymbolTable &ST) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000501 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
502 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
503 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
504
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000505 for (; I != End; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000506 if (const Type *Ty = dyn_cast<StructType>(I->second)) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000507 string Name = "struct l_" + makeNameProper(I->first);
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000508 Out << Name << ";\n";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000509 TypeNames.insert(std::make_pair(Ty, Name));
510 }
511 }
512
513 Out << "\n";
514
515 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
516 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
517 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
518
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000519 for (; I != End; ++I) {
520 const Value *V = I->second;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000521 if (const Type *Ty = dyn_cast<Type>(V)) {
522 string Name = "l_" + makeNameProper(I->first);
Chris Lattner1f02c892002-05-09 20:14:10 +0000523 if (isa<StructType>(Ty))
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000524 Name = "struct " + makeNameProper(Name);
Chris Lattner1f02c892002-05-09 20:14:10 +0000525 else
526 Out << "typedef ";
527
Chris Lattner83c57752002-08-19 22:17:53 +0000528 printType(Ty, Name, true);
529 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000530 }
531 }
532 }
533}
534
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000535
Chris Lattner16c7bb22002-05-09 02:28:59 +0000536// printFunctionDecl - Print function declaration
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000537//
Chris Lattner16c7bb22002-05-09 02:28:59 +0000538void CWriter::printFunctionDecl(const Function *F) {
539 printFunctionSignature(F);
540 Out << ";\n";
541}
542
543void CWriter::printFunctionSignature(const Function *F) {
544 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000545
546 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000547 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000548
Chris Lattner16c7bb22002-05-09 02:28:59 +0000549 // Print out the return type and name...
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000550 printType(F->getReturnType());
Chris Lattner1f02c892002-05-09 20:14:10 +0000551 Out << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000552
Chris Lattner16c7bb22002-05-09 02:28:59 +0000553 if (!F->isExternal()) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000554 if (!F->aempty()) {
555 printType(F->afront().getType(), getValueName(F->abegin()));
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000556
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000557 for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
558 I != E; ++I) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000559 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000560 printType(I->getType(), getValueName(I));
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000561 }
562 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000563 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000564 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000565 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000566 FT->getParamTypes().begin(),
567 E = FT->getParamTypes().end(); I != E; ++I) {
568 if (I != FT->getParamTypes().begin()) Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000569 printType(*I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000570 }
571 }
572
573 // Finish printing arguments...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000574 if (FT->isVarArg()) {
575 if (FT->getParamTypes().size()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000576 Out << "..."; // Output varargs portion of signature!
577 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000578 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000579}
580
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000581
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000582void CWriter::printFunction(Function *F) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000583 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000584
Chris Lattnerf34ee812002-05-09 03:12:34 +0000585 Table.incorporateFunction(F);
586
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000587 printFunctionSignature(F);
588 Out << " {\n";
589
Chris Lattner497e19a2002-05-09 20:39:03 +0000590 // print local variable information for the function
Chris Lattner7683a122002-05-09 20:33:35 +0000591 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000592 if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000593 Out << " ";
Chris Lattner3af3ba82002-05-09 21:31:18 +0000594 printType((*I)->getType(), getValueName(*I));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000595 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000596 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000597
598 // print the basic blocks
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000599 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
600 BasicBlock *Prev = BB->getPrev();
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000601
602 // Don't print the label for the basic block if there are no uses, or if the
603 // only terminator use is the precessor basic block's terminator. We have
604 // to scan the use list because PHI nodes use basic blocks too but do not
605 // require a label to be generated.
606 //
607 bool NeedsLabel = false;
608 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
609 UI != UE; ++UI)
610 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
611 if (TI != Prev->getTerminator()) {
612 NeedsLabel = true;
613 break;
614 }
615
616 if (NeedsLabel) Out << getValueName(BB) << ":\n";
617
618 // Output all of the instructions in the basic block...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000619 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II){
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000620 if (!isInlinableInst(*II) && !isa<PHINode>(*II)) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000621 if (II->getType() != Type::VoidTy)
622 outputLValue(II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000623 else
624 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000625 visit(*II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000626 Out << ";\n";
627 }
628 }
629
630 // Don't emit prefix or suffix for the terminator...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000631 visit(*BB->getTerminator());
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000632 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000633
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000634 Out << "}\n\n";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000635 Table.purgeFunction();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000636}
637
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000638// Specific Instruction type classes... note that all of the casts are
639// neccesary because we use the instruction classes as opaque types...
640//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000641void CWriter::visitReturnInst(ReturnInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000642 // Don't output a void return if this is the last basic block in the function
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000643 if (I.getNumOperands() == 0 &&
644 &*--I.getParent()->getParent()->end() == I.getParent() &&
645 !I.getParent()->size() == 1) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000646 return;
Chris Lattner963b70b2002-05-21 18:05:19 +0000647 }
Chris Lattner44408262002-05-09 03:50:42 +0000648
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000649 Out << " return";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000650 if (I.getNumOperands()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000651 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000652 writeOperand(I.getOperand(0));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000653 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000654 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000655}
656
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000657static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
658 // If PHI nodes need copies, we need the copy code...
659 if (isa<PHINode>(To->front()) ||
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000660 From->getNext() != To) // Not directly successor, need goto
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000661 return true;
662
663 // Otherwise we don't need the code.
664 return false;
665}
666
667void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
668 unsigned Indent) {
669 for (BasicBlock::iterator I = Succ->begin();
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000670 PHINode *PN = dyn_cast<PHINode>(&*I); ++I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000671 // now we have to do the printing
672 Out << string(Indent, ' ');
673 outputLValue(PN);
674 writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
675 Out << "; /* for PHI node */\n";
676 }
677
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000678 if (CurBB->getNext() != Succ) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000679 Out << string(Indent, ' ') << " goto ";
680 writeOperand(Succ);
681 Out << ";\n";
682 }
683}
684
685// Brach instruction printing - Avoid printing out a brach to a basic block that
686// immediately succeeds the current one.
687//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000688void CWriter::visitBranchInst(BranchInst &I) {
689 if (I.isConditional()) {
690 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(0))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000691 Out << " if (";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000692 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000693 Out << ") {\n";
694
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000695 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000696
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000697 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(1))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000698 Out << " } else {\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000699 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000700 }
701 } else {
702 // First goto not neccesary, assume second one is...
703 Out << " if (!";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000704 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000705 Out << ") {\n";
706
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000707 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000708 }
709
710 Out << " }\n";
711 } else {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000712 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000713 }
714 Out << "\n";
715}
716
717
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000718void CWriter::visitBinaryOperator(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000719 // binary instructions, shift instructions, setCond instructions.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000720 if (isa<PointerType>(I.getType())) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000721 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000722 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000723 Out << ")";
724 }
725
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000726 if (isa<PointerType>(I.getType())) Out << "(long long)";
727 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000728
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000729 switch (I.getOpcode()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000730 case Instruction::Add: Out << " + "; break;
731 case Instruction::Sub: Out << " - "; break;
732 case Instruction::Mul: Out << "*"; break;
733 case Instruction::Div: Out << "/"; break;
734 case Instruction::Rem: Out << "%"; break;
735 case Instruction::And: Out << " & "; break;
736 case Instruction::Or: Out << " | "; break;
737 case Instruction::Xor: Out << " ^ "; break;
738 case Instruction::SetEQ: Out << " == "; break;
739 case Instruction::SetNE: Out << " != "; break;
740 case Instruction::SetLE: Out << " <= "; break;
741 case Instruction::SetGE: Out << " >= "; break;
742 case Instruction::SetLT: Out << " < "; break;
743 case Instruction::SetGT: Out << " > "; break;
744 case Instruction::Shl : Out << " << "; break;
745 case Instruction::Shr : Out << " >> "; break;
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000746 default: std::cerr << "Invalid operator type!" << I; abort();
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000747 }
748
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000749 if (isa<PointerType>(I.getType())) Out << "(long long)";
750 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000751}
752
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000753void CWriter::visitCastInst(CastInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000754 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000755 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000756 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000757 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000758}
759
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000760void CWriter::visitCallInst(CallInst &I) {
761 const PointerType *PTy = cast<PointerType>(I.getCalledValue()->getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000762 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
763 const Type *RetTy = FTy->getReturnType();
764
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000765 Out << getValueName(I.getOperand(0)) << "(";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000766
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000767 if (I.getNumOperands() > 1) {
768 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000769
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000770 for (unsigned op = 2, Eop = I.getNumOperands(); op != Eop; ++op) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000771 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000772 writeOperand(I.getOperand(op));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000773 }
774 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000775 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000776}
777
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000778void CWriter::visitMallocInst(MallocInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000779 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000780 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000781 Out << ")malloc(sizeof(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000782 printType(I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000783 Out << ")";
784
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000785 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000786 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000787 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000788 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000789 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000790}
791
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000792void CWriter::visitAllocaInst(AllocaInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000793 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000794 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000795 Out << ") alloca(sizeof(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000796 printType(I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000797 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000798 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000799 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000800 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000801 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000802 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000803}
804
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000805void CWriter::visitFreeInst(FreeInst &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000806 Out << "free(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000807 writeOperand(I.getOperand(0));
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000808 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000809}
810
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000811void CWriter::printIndexingExpression(Value *Ptr, User::op_iterator I,
812 User::op_iterator E) {
813 bool HasImplicitAddress = false;
814 // If accessing a global value with no indexing, avoid *(&GV) syndrome
815 if (GlobalValue *V = dyn_cast<GlobalValue>(Ptr)) {
816 HasImplicitAddress = true;
817 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Ptr)) {
818 HasImplicitAddress = true;
819 Ptr = CPR->getValue(); // Get to the global...
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000820 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000821
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000822 if (I == E) {
823 if (!HasImplicitAddress)
824 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000825
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000826 writeOperandInternal(Ptr);
827 return;
828 }
829
830 const Constant *CI = dyn_cast<Constant>(I->get());
831 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
832 Out << "(&";
833
834 writeOperandInternal(Ptr);
835
836 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
837 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000838
839 // Print out the -> operator if possible...
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000840 if (CI && CI->isNullValue() && I+1 != E) {
841 if ((*(I+1))->getType() == Type::UByteTy) {
842 Out << (HasImplicitAddress ? "." : "->");
843 Out << "field" << cast<ConstantUInt>(*(I+1))->getValue();
844 I += 2;
845 } else { // Performing array indexing. Just skip the 0
846 ++I;
847 }
848 } else if (HasImplicitAddress) {
849
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000850 }
851
852 for (; I != E; ++I)
853 if ((*I)->getType() == Type::UIntTy) {
854 Out << "[";
855 writeOperand(*I);
856 Out << "]";
857 } else {
858 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
859 }
860}
861
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000862void CWriter::visitLoadInst(LoadInst &I) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000863 printIndexingExpression(I.getPointerOperand(), I.idx_begin(), I.idx_end());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000864}
865
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000866void CWriter::visitStoreInst(StoreInst &I) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000867 printIndexingExpression(I.getPointerOperand(), I.idx_begin(), I.idx_end());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000868 Out << " = ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000869 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000870}
871
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000872void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000873 Out << "&";
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000874 printIndexingExpression(I.getPointerOperand(), I.idx_begin(), I.idx_end());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000875}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000876
877//===----------------------------------------------------------------------===//
878// External Interface declaration
879//===----------------------------------------------------------------------===//
880
Chris Lattnerf34ee812002-05-09 03:12:34 +0000881void WriteToC(const Module *M, ostream &Out) {
882 assert(M && "You can't write a null module!!");
883 SlotCalculator SlotTable(M, false);
884 CWriter W(Out, SlotTable, M);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000885 W.write((Module*)M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000886 Out.flush();
887}