blob: d22a3640ff85a027582d4477c2b5be8441a95d32 [file] [log] [blame]
Chris Lattner16c7bb22002-05-09 02:28:59 +00001//===-- Writer.cpp - Library for writing C files --------------------------===//
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00002//
3// This library implements the functionality defined in llvm/Assembly/CWriter.h
4// and CLocalVars.h
5//
6// TODO : Recursive types.
Chris Lattner16c7bb22002-05-09 02:28:59 +00007//
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00008//===-----------------------------------------------------------------------==//
Chris Lattner16c7bb22002-05-09 02:28:59 +00009
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000010#include "llvm/Assembly/CWriter.h"
11#include "CLocalVars.h"
12#include "llvm/SlotCalculator.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000013#include "llvm/Constants.h"
Chris Lattner2f5f51a2002-05-09 03:28:37 +000014#include "llvm/DerivedTypes.h"
15#include "llvm/Module.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000016#include "llvm/GlobalVariable.h"
Chris Lattner2f5f51a2002-05-09 03:28:37 +000017#include "llvm/Function.h"
18#include "llvm/Argument.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000019#include "llvm/BasicBlock.h"
20#include "llvm/iMemory.h"
21#include "llvm/iTerminators.h"
22#include "llvm/iPHINode.h"
23#include "llvm/iOther.h"
Chris Lattner2f5f51a2002-05-09 03:28:37 +000024#include "llvm/iOperators.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000025#include "llvm/SymbolTable.h"
26#include "llvm/Support/InstVisitor.h"
27#include "Support/StringExtras.h"
28#include "Support/STLExtras.h"
29
30#include <algorithm>
31#include <strstream>
32using std::string;
33using std::map;
34using std::vector;
35using std::ostream;
36
Chris Lattner16c7bb22002-05-09 02:28:59 +000037//===-----------------------------------------------------------------------==//
38//
39// Implementation of the CLocalVars methods
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000040
41// Appends a variable to the LocalVars map if it does not already exist
42// Also check that the type exists on the map.
43void CLocalVars::addLocalVar(const Type *t, const string & var) {
44 if (!LocalVars.count(t) ||
45 find(LocalVars[t].begin(), LocalVars[t].end(), var)
46 == LocalVars[t].end()) {
47 LocalVars[t].push_back(var);
48 }
49}
50
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000051static std::string getConstStrValue(const Constant* CPV);
52
53
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000054static std::string getConstArrayStrValue(const Constant* CPV) {
55 std::string Result;
56
57 // As a special case, print the array as a string if it is an array of
58 // ubytes or an array of sbytes with positive values.
59 //
60 const Type *ETy = cast<ArrayType>(CPV->getType())->getElementType();
61 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
62
63 if (ETy == Type::SByteTy) {
64 for (unsigned i = 0; i < CPV->getNumOperands(); ++i)
65 if (ETy == Type::SByteTy &&
66 cast<ConstantSInt>(CPV->getOperand(i))->getValue() < 0) {
67 isString = false;
68 break;
69 }
70 }
Chris Lattner2f5eb4e2002-05-09 03:56:52 +000071 if (isString) {
72 // Make sure the last character is a null char, as automatically added by C
73 if (CPV->getNumOperands() == 0 ||
74 !cast<Constant>(*(CPV->op_end()-1))->isNullValue())
75 isString = false;
76 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000077
78 if (isString) {
79 Result = "\"";
Chris Lattner2f5eb4e2002-05-09 03:56:52 +000080 // Do not include the last character, which we know is null
81 for (unsigned i = 0, e = CPV->getNumOperands()-1; i != e; ++i) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000082 unsigned char C = (ETy == Type::SByteTy) ?
83 (unsigned char)cast<ConstantSInt>(CPV->getOperand(i))->getValue() :
84 (unsigned char)cast<ConstantUInt>(CPV->getOperand(i))->getValue();
85
86 if (isprint(C)) {
87 Result += C;
88 } else {
Chris Lattner2f5eb4e2002-05-09 03:56:52 +000089 switch (C) {
90 case '\n': Result += "\\n"; break;
91 case '\t': Result += "\\t"; break;
92 case '\r': Result += "\\r"; break;
93 case '\v': Result += "\\v"; break;
94 case '\a': Result += "\\a"; break;
95 default:
96 Result += "\\x";
97 Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
98 Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
99 break;
100 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000101 }
102 }
103 Result += "\"";
104
105 } else {
106 Result = "{";
107 if (CPV->getNumOperands()) {
108 Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
109 for (unsigned i = 1; i < CPV->getNumOperands(); i++)
110 Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
111 }
112 Result += " }";
113 }
114
115 return Result;
116}
117
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000118static std::string getConstStrValue(const Constant* CPV) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000119 switch (CPV->getType()->getPrimitiveID()) {
120 case Type::BoolTyID:
121 return CPV == ConstantBool::False ? "0" : "1";
122 case Type::SByteTyID:
123 case Type::ShortTyID:
124 case Type::IntTyID:
125 return itostr(cast<ConstantSInt>(CPV)->getValue());
126 case Type::LongTyID:
127 return itostr(cast<ConstantSInt>(CPV)->getValue()) + "ll";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000128
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000129 case Type::UByteTyID:
130 return utostr(cast<ConstantUInt>(CPV)->getValue());
131 case Type::UShortTyID:
132 return utostr(cast<ConstantUInt>(CPV)->getValue());
133 case Type::UIntTyID:
134 return utostr(cast<ConstantUInt>(CPV)->getValue())+"u";
135 case Type::ULongTyID:
136 return utostr(cast<ConstantUInt>(CPV)->getValue())+"ull";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000137
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000138 case Type::FloatTyID:
139 case Type::DoubleTyID:
140 return ftostr(cast<ConstantFP>(CPV)->getValue());
141
142 case Type::ArrayTyID:
143 return getConstArrayStrValue(CPV);
144
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000145 case Type::StructTyID: {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000146 std::string Result = "{";
147 if (CPV->getNumOperands()) {
148 Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
149 for (unsigned i = 1; i < CPV->getNumOperands(); i++)
150 Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000151 }
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000152 return Result + " }";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000153 }
154
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000155 default:
156 cerr << "Unknown constant type: " << CPV << "\n";
157 abort();
158 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000159}
160
161// Internal function
162// Pass the Type* variable and and the variable name and this prints out the
163// variable declaration.
164// This is different from calcTypeName because if you need to declare an array
165// the size of the array would appear after the variable name itself
166// For eg. int a[10];
Chris Lattner16c7bb22002-05-09 02:28:59 +0000167static string calcTypeNameVar(const Type *Ty,
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000168 map<const Type *, string> &TypeNames,
169 const string &NameSoFar, bool ignoreName = false){
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000170 if (Ty->isPrimitiveType())
171 switch (Ty->getPrimitiveID()) {
172 case Type::BoolTyID:
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000173 return "bool " + NameSoFar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000174 case Type::UByteTyID:
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000175 return "unsigned char " + NameSoFar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000176 case Type::SByteTyID:
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000177 return "signed char " + NameSoFar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000178 case Type::UShortTyID:
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000179 return "unsigned long long " + NameSoFar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000180 case Type::ULongTyID:
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000181 return "unsigned long long " + NameSoFar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000182 case Type::LongTyID:
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000183 return "signed long long " + NameSoFar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000184 case Type::UIntTyID:
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000185 return "unsigned " + NameSoFar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000186 default :
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000187 return Ty->getDescription() + " " + NameSoFar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000188 }
189
190 // Check to see if the type is named.
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000191 if (!ignoreName) {
192 map<const Type *, string>::iterator I = TypeNames.find(Ty);
193 if (I != TypeNames.end())
194 return I->second + " " + NameSoFar;
195 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000196
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000197 string Result;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000198 switch (Ty->getPrimitiveID()) {
199 case Type::FunctionTyID: {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000200 const FunctionType *MTy = cast<const FunctionType>(Ty);
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000201 Result += calcTypeNameVar(MTy->getReturnType(), TypeNames, "");
202 Result += " " + NameSoFar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000203 Result += " (";
204 for (FunctionType::ParamTypes::const_iterator
205 I = MTy->getParamTypes().begin(),
206 E = MTy->getParamTypes().end(); I != E; ++I) {
207 if (I != MTy->getParamTypes().begin())
208 Result += ", ";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000209 Result += calcTypeNameVar(*I, TypeNames, "");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000210 }
211 if (MTy->isVarArg()) {
212 if (!MTy->getParamTypes().empty())
213 Result += ", ";
214 Result += "...";
215 }
216 Result += ")";
217 break;
218 }
219 case Type::StructTyID: {
220 const StructType *STy = cast<const StructType>(Ty);
221 Result = " struct {\n ";
222 int indx = 0;
223 for (StructType::ElementTypes::const_iterator
224 I = STy->getElementTypes().begin(),
225 E = STy->getElementTypes().end(); I != E; ++I) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000226 Result += calcTypeNameVar(*I, TypeNames, "field" + itostr(indx++));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000227 Result += ";\n ";
228 }
229 Result += " }";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000230 Result += " " + NameSoFar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000231 break;
232 }
233
234 case Type::PointerTyID: {
235 Result = calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(),
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000236 TypeNames, "(*" + NameSoFar + ")");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000237 break;
238 }
239
240 case Type::ArrayTyID: {
241 const ArrayType *ATy = cast<const ArrayType>(Ty);
242 int NumElements = ATy->getNumElements();
Chris Lattner16c7bb22002-05-09 02:28:59 +0000243 Result = calcTypeNameVar(ATy->getElementType(), TypeNames,
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000244 NameSoFar + "[" + itostr(NumElements) + "]");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000245 break;
246 }
247 default:
248 assert(0 && "Unhandled case in getTypeProps!");
249 Result = "<error>";
250 }
251
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000252 return Result;
253}
254
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000255namespace {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000256 class CWriter {
257 ostream& Out;
258 SlotCalculator &Table;
259 const Module *TheModule;
260 map<const Type *, string> TypeNames;
261 public:
262 inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M)
263 : Out(o), Table(Tab), TheModule(M) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000264 }
265
Chris Lattner16c7bb22002-05-09 02:28:59 +0000266 inline void write(const Module *M) { printModule(M); }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000267
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000268 ostream& printTypeVar(const Type *Ty, const string &VariableName) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000269 return Out << calcTypeNameVar(Ty, TypeNames, VariableName);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000270 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000271
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000272 ostream& printType(const Type *Ty) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000273 return Out << calcTypeNameVar(Ty, TypeNames, "");
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000274 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000275
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000276 void writeOperand(const Value *Operand);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000277
278 string getValueName(const Value *V);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000279 private :
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000280
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000281 void printModule(const Module *M);
282 void printSymbolTable(const SymbolTable &ST);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000283 void printGlobal(const GlobalVariable *GV);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000284 void printFunctionSignature(const Function *F);
285 void printFunctionDecl(const Function *F); // Print just the forward decl
286 void printFunctionArgument(const Argument *FA);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000287
288 void printFunction(const Function *);
289
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000290 void outputBasicBlock(const BasicBlock *);
291 };
292 /* END class CWriter */
293
294
295 /* CLASS InstLocalVarsVisitor */
296 class InstLocalVarsVisitor : public InstVisitor<InstLocalVarsVisitor> {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000297 CWriter& CW;
Chris Lattner16c7bb22002-05-09 02:28:59 +0000298 void handleTerminator(TerminatorInst *tI, int indx);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000299 public:
300 CLocalVars CLV;
301
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000302 InstLocalVarsVisitor(CWriter &cw) : CW(cw) {}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000303
304 void visitInstruction(Instruction *I) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000305 if (I->getType() != Type::VoidTy)
306 CLV.addLocalVar(I->getType(), CW.getValueName(I));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000307 }
308
309 void visitBranchInst(BranchInst *I) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000310 handleTerminator(I, 0);
311 if (I->isConditional())
312 handleTerminator(I, 1);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000313 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000314 };
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000315}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000316
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000317void InstLocalVarsVisitor::handleTerminator(TerminatorInst *tI,int indx) {
318 BasicBlock *bb = tI->getSuccessor(indx);
319
320 BasicBlock::const_iterator insIt = bb->begin();
321 while (insIt != bb->end()) {
322 if (const PHINode *pI = dyn_cast<PHINode>(*insIt)) {
323 // Its a phinode!
324 // Calculate the incoming index for this
325 assert(pI->getBasicBlockIndex(tI->getParent()) != -1);
326
327 CLV.addLocalVar(pI->getType(), CW.getValueName(pI));
328 } else
329 break;
330 insIt++;
331 }
332}
333
334namespace {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000335 /* CLASS CInstPrintVisitor */
336
337 class CInstPrintVisitor: public InstVisitor<CInstPrintVisitor> {
338 CWriter& CW;
339 SlotCalculator& Table;
340 ostream &Out;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000341
342 void outputLValue(Instruction *);
343 void printPhiFromNextBlock(TerminatorInst *tI, int indx);
Chris Lattner44408262002-05-09 03:50:42 +0000344 void printIndexingExpr(MemAccessInst *MAI);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000345
346 public:
347 CInstPrintVisitor (CWriter &cw, SlotCalculator& table, ostream& o)
Chris Lattner44408262002-05-09 03:50:42 +0000348 : CW(cw), Table(table), Out(o) {}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000349
350 void visitCastInst(CastInst *I);
351 void visitCallInst(CallInst *I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000352 void visitShiftInst(ShiftInst *I) { visitBinaryOperator(I); }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000353 void visitReturnInst(ReturnInst *I);
354 void visitBranchInst(BranchInst *I);
355 void visitSwitchInst(SwitchInst *I);
356 void visitInvokeInst(InvokeInst *I) ;
357 void visitMallocInst(MallocInst *I);
358 void visitAllocaInst(AllocaInst *I);
359 void visitFreeInst(FreeInst *I);
360 void visitLoadInst(LoadInst *I);
361 void visitStoreInst(StoreInst *I);
362 void visitGetElementPtrInst(GetElementPtrInst *I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000363 void visitPHINode(PHINode *I) {}
364
365 void visitNot(GenericUnaryInst *I);
366 void visitBinaryOperator(Instruction *I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000367 };
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000368}
369
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000370void CInstPrintVisitor::outputLValue(Instruction *I) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000371 Out << " " << CW.getValueName(I) << " = ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000372}
373
374void CInstPrintVisitor::printPhiFromNextBlock(TerminatorInst *tI, int indx) {
375 BasicBlock *bb = tI->getSuccessor(indx);
376 BasicBlock::const_iterator insIt = bb->begin();
377 while (insIt != bb->end()) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000378 if (PHINode *pI = dyn_cast<PHINode>(*insIt)) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000379 //Its a phinode!
380 //Calculate the incoming index for this
381 int incindex = pI->getBasicBlockIndex(tI->getParent());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000382 if (incindex != -1) {
383 //now we have to do the printing
384 outputLValue(pI);
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000385 CW.writeOperand(pI->getIncomingValue(incindex));
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000386 Out << ";\n";
387 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000388 }
389 else break;
390 insIt++;
391 }
392}
393
394// Implement all "other" instructions, except for PHINode
395void CInstPrintVisitor::visitCastInst(CastInst *I) {
396 outputLValue(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000397 Out << "(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000398 CW.printType(I->getType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000399 Out << ")";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000400 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000401 Out << ";\n";
402}
403
404void CInstPrintVisitor::visitCallInst(CallInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000405 if (I->getType() != Type::VoidTy)
406 outputLValue(I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000407 else
408 Out << " ";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000409
Chris Lattner2f499022002-05-09 04:21:21 +0000410 const PointerType *PTy = cast<PointerType>(I->getCalledValue()->getType());
411 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
412 const Type *RetTy = FTy->getReturnType();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000413
Chris Lattner2f499022002-05-09 04:21:21 +0000414 Out << CW.getValueName(I->getOperand(0)) << "(";
415
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000416 if (I->getNumOperands() > 1) {
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000417 CW.writeOperand(I->getOperand(1));
Chris Lattner2f499022002-05-09 04:21:21 +0000418
419 for (unsigned op = 2, Eop = I->getNumOperands(); op != Eop; ++op) {
420 Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000421 CW.writeOperand(I->getOperand(op));
Chris Lattner2f499022002-05-09 04:21:21 +0000422 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000423 }
Chris Lattner2f499022002-05-09 04:21:21 +0000424 Out << ");\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000425}
426
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000427// Specific Instruction type classes... note that all of the casts are
428// neccesary because we use the instruction classes as opaque types...
429//
430void CInstPrintVisitor::visitReturnInst(ReturnInst *I) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000431 Out << " return";
432 if (I->getNumOperands()) {
433 Out << " ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000434 CW.writeOperand(I->getOperand(0));
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000435 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000436 Out << ";\n";
437}
438
439void CInstPrintVisitor::visitBranchInst(BranchInst *I) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000440 TerminatorInst *tI = cast<TerminatorInst>(I);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000441 if (I->isConditional()) {
442 Out << " if (";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000443 CW.writeOperand(I->getCondition());
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000444 Out << ") {\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000445 printPhiFromNextBlock(tI,0);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000446 Out << " goto ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000447 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000448 Out << ";\n";
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000449 Out << " } else {\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000450 printPhiFromNextBlock(tI,1);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000451 Out << " goto ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000452 CW.writeOperand(I->getOperand(1));
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000453 Out << ";\n }\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000454 } else {
455 printPhiFromNextBlock(tI,0);
456 Out << " goto ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000457 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000458 Out << ";\n";
459 }
460 Out << "\n";
461}
462
463void CInstPrintVisitor::visitSwitchInst(SwitchInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000464 assert(0 && "Switch not implemented!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000465}
466
467void CInstPrintVisitor::visitInvokeInst(InvokeInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000468 assert(0 && "Invoke not implemented!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000469}
470
471void CInstPrintVisitor::visitMallocInst(MallocInst *I) {
472 outputLValue(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000473 Out << "(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000474 CW.printType(I->getType()->getElementType());
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000475 Out << "*)malloc(sizeof(";
476 CW.printTypeVar(I->getType()->getElementType(), "");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000477 Out << ")";
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000478
479 if (I->isArrayAllocation()) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000480 Out << " * " ;
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000481 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000482 }
483 Out << ");";
484}
485
486void CInstPrintVisitor::visitAllocaInst(AllocaInst *I) {
487 outputLValue(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000488 Out << "(";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000489 CW.printTypeVar(I->getType(), "");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000490 Out << ") alloca(sizeof(";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000491 CW.printTypeVar(I->getType()->getElementType(), "");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000492 Out << ")";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000493 if (I->isArrayAllocation()) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000494 Out << " * " ;
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000495 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000496 }
497 Out << ");\n";
498}
499
500void CInstPrintVisitor::visitFreeInst(FreeInst *I) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000501 Out << "free(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000502 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000503 Out << ");\n";
504}
505
Chris Lattner44408262002-05-09 03:50:42 +0000506void CInstPrintVisitor::printIndexingExpr(MemAccessInst *MAI) {
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000507 CW.writeOperand(MAI->getPointerOperand());
Chris Lattner44408262002-05-09 03:50:42 +0000508
509 for (MemAccessInst::op_iterator I = MAI->idx_begin(), E = MAI->idx_end();
510 I != E; ++I)
511 if ((*I)->getType() == Type::UIntTy) {
512 Out << "[";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000513 CW.writeOperand(*I);
Chris Lattner44408262002-05-09 03:50:42 +0000514 Out << "]";
515 } else {
516 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000517 }
Chris Lattner44408262002-05-09 03:50:42 +0000518}
519
520void CInstPrintVisitor::visitLoadInst(LoadInst *I) {
521 outputLValue(I);
522 printIndexingExpr(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000523 Out << ";\n";
524}
525
Chris Lattner44408262002-05-09 03:50:42 +0000526void CInstPrintVisitor::visitStoreInst(StoreInst *I) {
527 Out << " ";
528 printIndexingExpr(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000529 Out << " = ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000530 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000531 Out << ";\n";
532}
533
534void CInstPrintVisitor::visitGetElementPtrInst(GetElementPtrInst *I) {
535 outputLValue(I);
Chris Lattner44408262002-05-09 03:50:42 +0000536 Out << "&";
537 printIndexingExpr(I);
538 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000539}
540
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000541void CInstPrintVisitor::visitNot(GenericUnaryInst *I) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000542 outputLValue(I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000543 Out << "~";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000544 CW.writeOperand(I->getOperand(0));
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000545 Out << ";\n";
546}
547
548void CInstPrintVisitor::visitBinaryOperator(Instruction *I) {
549 // binary instructions, shift instructions, setCond instructions.
550 outputLValue(I);
551 if (isa<PointerType>(I->getType())) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000552 Out << "(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000553 CW.printType(I->getType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000554 Out << ")";
555 }
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000556
557 if (isa<PointerType>(I->getType())) Out << "(long long)";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000558 CW.writeOperand(I->getOperand(0));
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000559
560 switch (I->getOpcode()) {
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000561 case Instruction::Add: Out << " + "; break;
562 case Instruction::Sub: Out << " - "; break;
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000563 case Instruction::Mul: Out << "*"; break;
564 case Instruction::Div: Out << "/"; break;
565 case Instruction::Rem: Out << "%"; break;
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000566 case Instruction::And: Out << " & "; break;
567 case Instruction::Or: Out << " | "; break;
568 case Instruction::Xor: Out << " ^ "; break;
569 case Instruction::SetEQ: Out << " == "; break;
570 case Instruction::SetNE: Out << " != "; break;
571 case Instruction::SetLE: Out << " <= "; break;
572 case Instruction::SetGE: Out << " >= "; break;
573 case Instruction::SetLT: Out << " < "; break;
574 case Instruction::SetGT: Out << " > "; break;
575 case Instruction::Shl : Out << " << "; break;
576 case Instruction::Shr : Out << " >> "; break;
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000577 default: cerr << "Invalid operator type!" << I; abort();
578 }
579
580 if (isa<PointerType>(I->getType())) Out << "(long long)";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000581 CW.writeOperand(I->getOperand(1));
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000582 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000583}
584
585/* END : CInstPrintVisitor implementation */
586
Chris Lattner2f499022002-05-09 04:21:21 +0000587// We dont want identifier names with ., space, - in them.
588// So we replace them with _
589static string makeNameProper(string x) {
590 string tmp;
591 for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
592 switch (*sI) {
593 case '.': tmp += "_d"; break;
594 case ' ': tmp += "_s"; break;
595 case '-': tmp += "_D"; break;
596 case '_': tmp += "__"; break;
597 default: tmp += *sI;
598 }
599
600 return tmp;
601}
602
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000603string CWriter::getValueName(const Value *V) {
Chris Lattner2f499022002-05-09 04:21:21 +0000604 if (V->hasName()) { // Print out the label if it exists...
605 if (isa<GlobalValue>(V)) // Do not mangle globals...
606 return makeNameProper(V->getName());
607
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000608 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
609 makeNameProper(V->getName());
Chris Lattner2f499022002-05-09 04:21:21 +0000610 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000611
612 int Slot = Table.getValSlot(V);
613 assert(Slot >= 0 && "Invalid value!");
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000614 return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000615}
616
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000617void CWriter::printModule(const Module *M) {
618 // printing stdlib inclusion
619 // Out << "#include <stdlib.h>\n";
620
Chris Lattner16c7bb22002-05-09 02:28:59 +0000621 // get declaration for alloca
622 Out << "/* Provide Declarations */\n"
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000623 << "#include <alloca.h>\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000624
625 // Provide a definition for null if one does not already exist.
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000626 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000627 << "typedef unsigned char bool;\n"
628
629 << "\n\n/* Global Symbols */\n";
630
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000631 // Loop over the symbol table, emitting all named constants...
632 if (M->hasSymbolTable())
633 printSymbolTable(*M->getSymbolTable());
634
Chris Lattner16c7bb22002-05-09 02:28:59 +0000635 Out << "\n\n/* Global Data */\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000636 for_each(M->gbegin(), M->gend(),
637 bind_obj(this, &CWriter::printGlobal));
638
Chris Lattner16c7bb22002-05-09 02:28:59 +0000639 // First output all the declarations of the functions as C requires Functions
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000640 // be declared before they are used.
Chris Lattner16c7bb22002-05-09 02:28:59 +0000641 //
642 Out << "\n\n/* Function Declarations */\n";
643 for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunctionDecl));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000644
Chris Lattner16c7bb22002-05-09 02:28:59 +0000645 // Output all of the functions...
646 Out << "\n\n/* Function Bodies */\n";
647 for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunction));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000648}
649
650// prints the global constants
651void CWriter::printGlobal(const GlobalVariable *GV) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000652 if (GV->hasInternalLinkage()) Out << "static ";
653
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000654 printTypeVar(GV->getType()->getElementType(), getValueName(GV));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000655
656 if (GV->hasInitializer()) {
657 Out << " = " ;
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000658 writeOperand(GV->getInitializer());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000659 }
660
661 Out << ";\n";
662}
663
664// printSymbolTable - Run through symbol table looking for named constants
665// if a named constant is found, emit it's declaration...
666// Assuming that symbol table has only types and constants.
667void CWriter::printSymbolTable(const SymbolTable &ST) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000668 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
669 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
670 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
671
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000672 for (; I != End; ++I)
673 if (const Type *Ty = dyn_cast<const StructType>(I->second)) {
674 string Name = "struct l_" + I->first;
675 Out << Name << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000676
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000677 TypeNames.insert(std::make_pair(Ty, Name));
678 }
679 }
680
681 Out << "\n";
682
683 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
684 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
685 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
686
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000687 for (; I != End; ++I) {
688 const Value *V = I->second;
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000689 if (const Type *Ty = dyn_cast<const Type>(V)) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000690 Out << "typedef ";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000691 string Name = "l_" + I->first;
692 if (isa<StructType>(Ty)) Name = "struct " + Name;
693 Out << calcTypeNameVar(Ty, TypeNames, Name, true) << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000694 }
695 }
696 }
697}
698
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000699
Chris Lattner16c7bb22002-05-09 02:28:59 +0000700// printFunctionDecl - Print function declaration
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000701//
Chris Lattner16c7bb22002-05-09 02:28:59 +0000702void CWriter::printFunctionDecl(const Function *F) {
703 printFunctionSignature(F);
704 Out << ";\n";
705}
706
707void CWriter::printFunctionSignature(const Function *F) {
708 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000709
710 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000711 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000712
Chris Lattner16c7bb22002-05-09 02:28:59 +0000713 // Print out the return type and name...
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000714 printType(F->getReturnType());
Chris Lattner2f499022002-05-09 04:21:21 +0000715 Out << " " << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000716
Chris Lattner16c7bb22002-05-09 02:28:59 +0000717 if (!F->isExternal()) {
718 for_each(F->getArgumentList().begin(), F->getArgumentList().end(),
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000719 bind_obj(this, &CWriter::printFunctionArgument));
720 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000721 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000722 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000723 FT->getParamTypes().begin(),
724 E = FT->getParamTypes().end(); I != E; ++I) {
725 if (I != FT->getParamTypes().begin()) Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000726 printType(*I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000727 }
728 }
729
730 // Finish printing arguments...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000731 if (FT->isVarArg()) {
732 if (FT->getParamTypes().size()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000733 Out << "..."; // Output varargs portion of signature!
734 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000735 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000736}
737
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000738
739// printFunctionArgument - This member is called for every argument that
740// is passed into the method. Simply print it out
741//
742void CWriter::printFunctionArgument(const Argument *Arg) {
743 // Insert commas as we go... the first arg doesn't get a comma
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000744 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
745
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000746 // Output type...
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000747 printTypeVar(Arg->getType(), getValueName(Arg));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000748}
749
Chris Lattner16c7bb22002-05-09 02:28:59 +0000750void CWriter::printFunction(const Function *F) {
751 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000752
Chris Lattnerf34ee812002-05-09 03:12:34 +0000753 Table.incorporateFunction(F);
754
Chris Lattner16c7bb22002-05-09 02:28:59 +0000755 // Process each of the basic blocks, gather information and call the
756 // output methods on the CLocalVars and Function* objects.
757
758 // gather local variable information for each basic block
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000759 InstLocalVarsVisitor ILV(*this);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000760 ILV.visit((Function *)F);
761
762 printFunctionSignature(F);
763 Out << " {\n";
764
765 // Loop over the symbol table, emitting all named constants...
766 if (F->hasSymbolTable())
767 printSymbolTable(*F->getSymbolTable());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000768
Chris Lattner16c7bb22002-05-09 02:28:59 +0000769 // print the local variables
770 // we assume that every local variable is alloca'ed in the C code.
771 std::map<const Type*, VarListType> &locals = ILV.CLV.LocalVars;
772
773 map<const Type*, VarListType>::iterator iter;
774 for (iter = locals.begin(); iter != locals.end(); ++iter) {
775 VarListType::iterator listiter;
776 for (listiter = iter->second.begin(); listiter != iter->second.end();
777 ++listiter) {
778 Out << " ";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000779 printTypeVar(iter->first, *listiter);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000780 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000781 }
782 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000783
784 // print the basic blocks
785 for_each(F->begin(), F->end(), bind_obj(this, &CWriter::outputBasicBlock));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000786
Chris Lattner16c7bb22002-05-09 02:28:59 +0000787 Out << "}\n";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000788 Table.purgeFunction();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000789}
790
791void CWriter::outputBasicBlock(const BasicBlock* BB) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000792 Out << getValueName(BB) << ":\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000793
794 // Output all of the instructions in the basic block...
795 // print the basic blocks
796 CInstPrintVisitor CIPV(*this, Table, Out);
797 CIPV.visit((BasicBlock *) BB);
798}
799
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000800void CWriter::writeOperand(const Value *Operand) {
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000801 if (isa<GlobalVariable>(Operand))
802 Out << "(&"; // Global variables are references as their addresses by llvm
Chris Lattner44408262002-05-09 03:50:42 +0000803
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000804 if (Operand->hasName()) {
Chris Lattner2f499022002-05-09 04:21:21 +0000805 Out << getValueName(Operand);
Chris Lattner44408262002-05-09 03:50:42 +0000806 } else if (const Constant *CPV = dyn_cast<const Constant>(Operand)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000807 if (isa<ConstantPointerNull>(CPV))
808 Out << "NULL";
809 else
810 Out << getConstStrValue(CPV);
Chris Lattner44408262002-05-09 03:50:42 +0000811 } else {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000812 int Slot = Table.getValSlot(Operand);
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000813 assert(Slot >= 0 && "Malformed LLVM!");
814 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
Chris Lattner16c7bb22002-05-09 02:28:59 +0000815 }
Chris Lattner44408262002-05-09 03:50:42 +0000816
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000817 if (isa<GlobalVariable>(Operand))
Chris Lattner44408262002-05-09 03:50:42 +0000818 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000819}
820
821
822//===----------------------------------------------------------------------===//
823// External Interface declaration
824//===----------------------------------------------------------------------===//
825
Chris Lattnerf34ee812002-05-09 03:12:34 +0000826void WriteToC(const Module *M, ostream &Out) {
827 assert(M && "You can't write a null module!!");
828 SlotCalculator SlotTable(M, false);
829 CWriter W(Out, SlotTable, M);
830 W.write(M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000831 Out.flush();
832}