blob: 955b89b451b21cde405dd76d10d92e9c3df28773 [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 Lattnerbb03efd2002-06-25 15:57:03 +0000110 void printIndexingExpr(MemAccessInst &MAI);
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
Chris Lattner83c57752002-08-19 22:17:53 +0000145// Pass the Type* and the variable name and this prints out the variable
146// declaration.
147//
148ostream &CWriter::printType(const Type *Ty, const string &NameSoFar,
149 bool IgnoreName = false) {
150 if (Ty->isPrimitiveType())
151 switch (Ty->getPrimitiveID()) {
152 case Type::VoidTyID: return Out << "void " << NameSoFar;
153 case Type::BoolTyID: return Out << "bool " << NameSoFar;
154 case Type::UByteTyID: return Out << "unsigned char " << NameSoFar;
155 case Type::SByteTyID: return Out << "signed char " << NameSoFar;
156 case Type::UShortTyID: return Out << "unsigned short " << NameSoFar;
157 case Type::ShortTyID: return Out << "short " << NameSoFar;
158 case Type::UIntTyID: return Out << "unsigned " << NameSoFar;
159 case Type::IntTyID: return Out << "int " << NameSoFar;
160 case Type::ULongTyID: return Out << "unsigned long long " << NameSoFar;
161 case Type::LongTyID: return Out << "signed long long " << NameSoFar;
162 case Type::FloatTyID: return Out << "float " << NameSoFar;
163 case Type::DoubleTyID: return Out << "double " << NameSoFar;
164 default :
165 std::cerr << "Unknown primitive type: " << Ty << "\n";
166 abort();
167 }
168
169 // Check to see if the type is named.
170 if (!IgnoreName) {
171 map<const Type *, string>::iterator I = TypeNames.find(Ty);
172 if (I != TypeNames.end()) {
173 return Out << I->second << " " << NameSoFar;
174 }
175 }
176
177 string Result;
178 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);
214 // If this is a pointer to a function, we need parens. In all other cases,
215 // we don't though, and adding them all the time clutters stuff up a ton, so
216 // special case this.
217 //
218 if (isa<FunctionType>(PTy->getElementType()))
219 return printType(PTy->getElementType(), "(*" + NameSoFar + ")");
220 else
221 return printType(PTy->getElementType(), "*" + NameSoFar);
222 }
223
224 case Type::ArrayTyID: {
225 const ArrayType *ATy = cast<ArrayType>(Ty);
226 unsigned NumElements = ATy->getNumElements();
227 return printType(ATy->getElementType(),
228 NameSoFar + "[" + utostr(NumElements) + "]");
229 }
230 default:
231 assert(0 && "Unhandled case in getTypeProps!");
232 abort();
233 }
234
235 return Out;
236}
237
Chris Lattner6d492922002-08-19 21:32:41 +0000238void CWriter::printConstantArray(ConstantArray *CPA) {
239
240 // As a special case, print the array as a string if it is an array of
241 // ubytes or an array of sbytes with positive values.
242 //
243 const Type *ETy = CPA->getType()->getElementType();
244 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
245
246 // Make sure the last character is a null char, as automatically added by C
247 if (CPA->getNumOperands() == 0 ||
248 !cast<Constant>(*(CPA->op_end()-1))->isNullValue())
249 isString = false;
250
251 if (isString) {
252 Out << "\"";
253 // Do not include the last character, which we know is null
254 for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) {
255 unsigned char C = (ETy == Type::SByteTy) ?
256 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
257 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
258
259 if (isprint(C)) {
260 Out << C;
261 } else {
262 switch (C) {
263 case '\n': Out << "\\n"; break;
264 case '\t': Out << "\\t"; break;
265 case '\r': Out << "\\r"; break;
266 case '\v': Out << "\\v"; break;
267 case '\a': Out << "\\a"; break;
268 default:
269 Out << "\\x";
270 Out << ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
271 Out << ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
272 break;
273 }
274 }
275 }
276 Out << "\"";
277 } else {
278 Out << "{";
279 if (CPA->getNumOperands()) {
280 Out << " ";
281 printConstant(cast<Constant>(CPA->getOperand(0)));
282 for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
283 Out << ", ";
284 printConstant(cast<Constant>(CPA->getOperand(i)));
285 }
286 }
287 Out << " }";
288 }
289}
290
291
292// printConstant - The LLVM Constant to C Constant converter.
293void CWriter::printConstant(Constant *CPV) {
294 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
295 switch (CE->getOpcode()) {
296 default:
297 std::cerr << "CWriter Error: Unhandled constant expression: "
298 << CE << "\n";
299 abort();
300 }
301 }
302
303 switch (CPV->getType()->getPrimitiveID()) {
304 case Type::BoolTyID:
305 Out << (CPV == ConstantBool::False ? "0" : "1"); break;
306 case Type::SByteTyID:
307 case Type::ShortTyID:
308 case Type::IntTyID:
309 Out << cast<ConstantSInt>(CPV)->getValue(); break;
310 case Type::LongTyID:
311 Out << cast<ConstantSInt>(CPV)->getValue() << "ll"; break;
312
313 case Type::UByteTyID:
314 case Type::UShortTyID:
315 Out << cast<ConstantUInt>(CPV)->getValue(); break;
316 case Type::UIntTyID:
317 Out << cast<ConstantUInt>(CPV)->getValue() << "u"; break;
318 case Type::ULongTyID:
319 Out << cast<ConstantUInt>(CPV)->getValue() << "ull"; break;
320
321 case Type::FloatTyID:
322 case Type::DoubleTyID:
323 Out << cast<ConstantFP>(CPV)->getValue(); break;
324
325 case Type::ArrayTyID:
326 printConstantArray(cast<ConstantArray>(CPV));
327 break;
328
329 case Type::StructTyID: {
330 Out << "{";
331 if (CPV->getNumOperands()) {
332 Out << " ";
333 printConstant(cast<Constant>(CPV->getOperand(0)));
334 for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) {
335 Out << ", ";
336 printConstant(cast<Constant>(CPV->getOperand(i)));
337 }
338 }
339 Out << " }";
340 break;
341 }
342
343 case Type::PointerTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000344 if (isa<ConstantPointerNull>(CPV)) {
345 Out << "((";
Chris Lattner3af3ba82002-05-09 21:31:18 +0000346 printType(CPV->getType(), "");
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000347 Out << ")NULL)";
Chris Lattner6d492922002-08-19 21:32:41 +0000348 break;
349 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
350 writeOperand(CPR->getValue());
351 break;
352 }
353 // FALL THROUGH
354 default:
355 std::cerr << "Unknown constant type: " << CPV << "\n";
356 abort();
357 }
358}
359
360void CWriter::writeOperandInternal(Value *Operand) {
361 if (Operand->hasName()) {
362 Out << getValueName(Operand);
363 } else if (Constant *CPV = dyn_cast<Constant>(Operand)) {
364 printConstant(CPV);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000365 } else {
366 int Slot = Table.getValSlot(Operand);
367 assert(Slot >= 0 && "Malformed LLVM!");
368 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
369 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000370}
371
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000372void CWriter::writeOperand(Value *Operand) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000373 if (Instruction *I = dyn_cast<Instruction>(Operand))
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000374 if (isInlinableInst(*I)) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000375 // Should we inline this instruction to build a tree?
376 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000377 visit(*I);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000378 Out << ")";
379 return;
380 }
381
382 if (isa<GlobalVariable>(Operand))
383 Out << "(&"; // Global variables are references as their addresses by llvm
384
385 writeOperandInternal(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000386
387 if (isa<GlobalVariable>(Operand))
388 Out << ")";
389}
390
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000391void CWriter::printModule(Module *M) {
Chris Lattner78771c82002-05-17 04:55:35 +0000392 // Calculate which global values have names that will collide when we throw
393 // away type information.
Chris Lattner594b9fa2002-05-21 21:10:04 +0000394 { // Scope to delete the FoundNames set when we are done with it...
Chris Lattner78771c82002-05-17 04:55:35 +0000395 std::set<string> FoundNames;
396 for (Module::iterator I = M->begin(), E = M->end(); 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 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000404 if (I->hasName()) // If the global has a name...
405 if (FoundNames.count(I->getName())) // And the name is already used
406 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000407 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000408 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000409 }
410
411
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000412 // printing stdlib inclusion
413 // Out << "#include <stdlib.h>\n";
414
Chris Lattner16c7bb22002-05-09 02:28:59 +0000415 // get declaration for alloca
416 Out << "/* Provide Declarations */\n"
Chris Lattner594b9fa2002-05-21 21:10:04 +0000417 << "#include <malloc.h>\n"
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000418 << "#include <alloca.h>\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000419
420 // Provide a definition for null if one does not already exist.
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000421 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000422 << "typedef unsigned char bool;\n"
423
Chris Lattnera4d4a852002-08-19 21:48:40 +0000424 << "\n\n/* Global Declarations */\n";
425
426 // First output all the declarations for the program, because C requires
427 // Functions & globals to be declared before they are used.
428 //
Chris Lattner16c7bb22002-05-09 02:28:59 +0000429
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000430 // Loop over the symbol table, emitting all named constants...
431 if (M->hasSymbolTable())
432 printSymbolTable(*M->getSymbolTable());
433
Chris Lattnera4d4a852002-08-19 21:48:40 +0000434 // Global variable declarations...
435 if (!M->gempty()) {
436 Out << "\n/* Global Variable Declarations */\n";
437 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
438 Out << (I->hasExternalLinkage() ? "extern " : "static ");
439 printType(I->getType()->getElementType(), getValueName(I));
440 Out << ";\n";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000441 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000442 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000443
Chris Lattnera4d4a852002-08-19 21:48:40 +0000444 // Function declarations
445 if (!M->empty()) {
446 Out << "\n/* Function Declarations */\n";
447 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
448 printFunctionDecl(I);
449 }
450
451 // Output the global variable contents...
452 if (!M->gempty()) {
453 Out << "\n\n/* Global Data */\n";
454 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
455 if (I->hasInternalLinkage()) Out << "static ";
456 printType(I->getType()->getElementType(), getValueName(I));
457
458 if (I->hasInitializer()) {
459 Out << " = " ;
460 writeOperand(I->getInitializer());
461 }
462 Out << ";\n";
463 }
464 }
465
Chris Lattner16c7bb22002-05-09 02:28:59 +0000466 // Output all of the functions...
Chris Lattnera4d4a852002-08-19 21:48:40 +0000467 if (!M->empty()) {
468 Out << "\n\n/* Function Bodies */\n";
469 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
470 printFunction(I);
471 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000472}
473
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000474
475// printSymbolTable - Run through symbol table looking for named constants
476// if a named constant is found, emit it's declaration...
477// Assuming that symbol table has only types and constants.
478void CWriter::printSymbolTable(const SymbolTable &ST) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000479 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
480 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
481 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
482
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000483 for (; I != End; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000484 if (const Type *Ty = dyn_cast<StructType>(I->second)) {
485 string Name = "struct l_" + makeNameProper(I->first);
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000486 Out << Name << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000487
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000488 TypeNames.insert(std::make_pair(Ty, Name));
489 }
490 }
491
492 Out << "\n";
493
494 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
495 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
496 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
497
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000498 for (; I != End; ++I) {
499 const Value *V = I->second;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000500 if (const Type *Ty = dyn_cast<Type>(V)) {
501 string Name = "l_" + makeNameProper(I->first);
Chris Lattner1f02c892002-05-09 20:14:10 +0000502 if (isa<StructType>(Ty))
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000503 Name = "struct " + makeNameProper(Name);
Chris Lattner1f02c892002-05-09 20:14:10 +0000504 else
505 Out << "typedef ";
506
Chris Lattner83c57752002-08-19 22:17:53 +0000507 printType(Ty, Name, true);
508 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000509 }
510 }
511 }
512}
513
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000514
Chris Lattner16c7bb22002-05-09 02:28:59 +0000515// printFunctionDecl - Print function declaration
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000516//
Chris Lattner16c7bb22002-05-09 02:28:59 +0000517void CWriter::printFunctionDecl(const Function *F) {
518 printFunctionSignature(F);
519 Out << ";\n";
520}
521
522void CWriter::printFunctionSignature(const Function *F) {
523 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000524
525 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000526 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000527
Chris Lattner16c7bb22002-05-09 02:28:59 +0000528 // Print out the return type and name...
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000529 printType(F->getReturnType());
Chris Lattner1f02c892002-05-09 20:14:10 +0000530 Out << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000531
Chris Lattner16c7bb22002-05-09 02:28:59 +0000532 if (!F->isExternal()) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000533 if (!F->aempty()) {
534 printType(F->afront().getType(), getValueName(F->abegin()));
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000535
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000536 for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
537 I != E; ++I) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000538 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000539 printType(I->getType(), getValueName(I));
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000540 }
541 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000542 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000543 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000544 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000545 FT->getParamTypes().begin(),
546 E = FT->getParamTypes().end(); I != E; ++I) {
547 if (I != FT->getParamTypes().begin()) Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000548 printType(*I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000549 }
550 }
551
552 // Finish printing arguments...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000553 if (FT->isVarArg()) {
554 if (FT->getParamTypes().size()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000555 Out << "..."; // Output varargs portion of signature!
556 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000557 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000558}
559
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000560
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000561void CWriter::printFunction(Function *F) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000562 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000563
Chris Lattnerf34ee812002-05-09 03:12:34 +0000564 Table.incorporateFunction(F);
565
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000566 printFunctionSignature(F);
567 Out << " {\n";
568
Chris Lattner497e19a2002-05-09 20:39:03 +0000569 // print local variable information for the function
Chris Lattner7683a122002-05-09 20:33:35 +0000570 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000571 if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000572 Out << " ";
Chris Lattner3af3ba82002-05-09 21:31:18 +0000573 printType((*I)->getType(), getValueName(*I));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000574 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000575 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000576
577 // print the basic blocks
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000578 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
579 BasicBlock *Prev = BB->getPrev();
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000580
581 // Don't print the label for the basic block if there are no uses, or if the
582 // only terminator use is the precessor basic block's terminator. We have
583 // to scan the use list because PHI nodes use basic blocks too but do not
584 // require a label to be generated.
585 //
586 bool NeedsLabel = false;
587 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
588 UI != UE; ++UI)
589 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
590 if (TI != Prev->getTerminator()) {
591 NeedsLabel = true;
592 break;
593 }
594
595 if (NeedsLabel) Out << getValueName(BB) << ":\n";
596
597 // Output all of the instructions in the basic block...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000598 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II){
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000599 if (!isInlinableInst(*II) && !isa<PHINode>(*II)) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000600 if (II->getType() != Type::VoidTy)
601 outputLValue(II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000602 else
603 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000604 visit(*II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000605 Out << ";\n";
606 }
607 }
608
609 // Don't emit prefix or suffix for the terminator...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000610 visit(*BB->getTerminator());
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000611 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000612
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000613 Out << "}\n\n";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000614 Table.purgeFunction();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000615}
616
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000617// Specific Instruction type classes... note that all of the casts are
618// neccesary because we use the instruction classes as opaque types...
619//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000620void CWriter::visitReturnInst(ReturnInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000621 // Don't output a void return if this is the last basic block in the function
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000622 if (I.getNumOperands() == 0 &&
623 &*--I.getParent()->getParent()->end() == I.getParent() &&
624 !I.getParent()->size() == 1) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000625 return;
Chris Lattner963b70b2002-05-21 18:05:19 +0000626 }
Chris Lattner44408262002-05-09 03:50:42 +0000627
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000628 Out << " return";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000629 if (I.getNumOperands()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000630 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000631 writeOperand(I.getOperand(0));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000632 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000633 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000634}
635
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000636static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
637 // If PHI nodes need copies, we need the copy code...
638 if (isa<PHINode>(To->front()) ||
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000639 From->getNext() != To) // Not directly successor, need goto
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000640 return true;
641
642 // Otherwise we don't need the code.
643 return false;
644}
645
646void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
647 unsigned Indent) {
648 for (BasicBlock::iterator I = Succ->begin();
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000649 PHINode *PN = dyn_cast<PHINode>(&*I); ++I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000650 // now we have to do the printing
651 Out << string(Indent, ' ');
652 outputLValue(PN);
653 writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
654 Out << "; /* for PHI node */\n";
655 }
656
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000657 if (CurBB->getNext() != Succ) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000658 Out << string(Indent, ' ') << " goto ";
659 writeOperand(Succ);
660 Out << ";\n";
661 }
662}
663
664// Brach instruction printing - Avoid printing out a brach to a basic block that
665// immediately succeeds the current one.
666//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000667void CWriter::visitBranchInst(BranchInst &I) {
668 if (I.isConditional()) {
669 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(0))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000670 Out << " if (";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000671 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000672 Out << ") {\n";
673
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000674 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000675
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000676 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(1))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000677 Out << " } else {\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000678 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000679 }
680 } else {
681 // First goto not neccesary, assume second one is...
682 Out << " if (!";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000683 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000684 Out << ") {\n";
685
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000686 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000687 }
688
689 Out << " }\n";
690 } else {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000691 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000692 }
693 Out << "\n";
694}
695
696
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000697void CWriter::visitBinaryOperator(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000698 // binary instructions, shift instructions, setCond instructions.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000699 if (isa<PointerType>(I.getType())) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000700 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000701 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000702 Out << ")";
703 }
704
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000705 if (isa<PointerType>(I.getType())) Out << "(long long)";
706 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000707
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000708 switch (I.getOpcode()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000709 case Instruction::Add: Out << " + "; break;
710 case Instruction::Sub: Out << " - "; break;
711 case Instruction::Mul: Out << "*"; break;
712 case Instruction::Div: Out << "/"; break;
713 case Instruction::Rem: Out << "%"; break;
714 case Instruction::And: Out << " & "; break;
715 case Instruction::Or: Out << " | "; break;
716 case Instruction::Xor: Out << " ^ "; break;
717 case Instruction::SetEQ: Out << " == "; break;
718 case Instruction::SetNE: Out << " != "; break;
719 case Instruction::SetLE: Out << " <= "; break;
720 case Instruction::SetGE: Out << " >= "; break;
721 case Instruction::SetLT: Out << " < "; break;
722 case Instruction::SetGT: Out << " > "; break;
723 case Instruction::Shl : Out << " << "; break;
724 case Instruction::Shr : Out << " >> "; break;
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000725 default: std::cerr << "Invalid operator type!" << I; abort();
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000726 }
727
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000728 if (isa<PointerType>(I.getType())) Out << "(long long)";
729 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000730}
731
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000732void CWriter::visitCastInst(CastInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000733 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000734 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000735 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000736 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000737}
738
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000739void CWriter::visitCallInst(CallInst &I) {
740 const PointerType *PTy = cast<PointerType>(I.getCalledValue()->getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000741 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
742 const Type *RetTy = FTy->getReturnType();
743
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000744 Out << getValueName(I.getOperand(0)) << "(";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000745
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000746 if (I.getNumOperands() > 1) {
747 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000748
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000749 for (unsigned op = 2, Eop = I.getNumOperands(); op != Eop; ++op) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000750 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000751 writeOperand(I.getOperand(op));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000752 }
753 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000754 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000755}
756
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000757void CWriter::visitMallocInst(MallocInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000758 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000759 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000760 Out << ")malloc(sizeof(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000761 printType(I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000762 Out << ")";
763
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000764 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000765 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000766 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000767 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000768 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000769}
770
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000771void CWriter::visitAllocaInst(AllocaInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000772 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000773 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000774 Out << ") alloca(sizeof(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000775 printType(I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000776 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000777 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000778 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000779 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000780 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000781 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000782}
783
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000784void CWriter::visitFreeInst(FreeInst &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000785 Out << "free(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000786 writeOperand(I.getOperand(0));
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000787 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000788}
789
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000790void CWriter::printIndexingExpr(MemAccessInst &MAI) {
791 MemAccessInst::op_iterator I = MAI.idx_begin(), E = MAI.idx_end();
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000792 if (I == E) {
793 // If accessing a global value with no indexing, avoid *(&GV) syndrome
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000794 if (GlobalValue *V = dyn_cast<GlobalValue>(MAI.getPointerOperand())) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000795 writeOperandInternal(V);
796 return;
797 }
798
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000799 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000800 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000801
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000802 writeOperand(MAI.getPointerOperand());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000803
804 if (I == E) return;
805
806 // Print out the -> operator if possible...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000807 const Constant *CI = dyn_cast<Constant>(I->get());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000808 if (CI && CI->isNullValue() && I+1 != E &&
809 (*(I+1))->getType() == Type::UByteTy) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000810 Out << "->field" << cast<ConstantUInt>(*(I+1))->getValue();
811 I += 2;
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000812 }
813
814 for (; I != E; ++I)
815 if ((*I)->getType() == Type::UIntTy) {
816 Out << "[";
817 writeOperand(*I);
818 Out << "]";
819 } else {
820 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
821 }
822}
823
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000824void CWriter::visitLoadInst(LoadInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000825 printIndexingExpr(I);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000826}
827
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000828void CWriter::visitStoreInst(StoreInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000829 printIndexingExpr(I);
830 Out << " = ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000831 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000832}
833
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000834void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000835 Out << "&";
836 printIndexingExpr(I);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000837}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000838
839//===----------------------------------------------------------------------===//
840// External Interface declaration
841//===----------------------------------------------------------------------===//
842
Chris Lattnerf34ee812002-05-09 03:12:34 +0000843void WriteToC(const Module *M, ostream &Out) {
844 assert(M && "You can't write a null module!!");
845 SlotCalculator SlotTable(M, false);
846 CWriter W(Out, SlotTable, M);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000847 W.write((Module*)M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000848 Out.flush();
849}