blob: 00b00ab9a8254a0d3b40591173746c955b028a9b [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
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000161// Pass the Type* variable and and the variable name and this prints out the
162// variable declaration.
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000163//
Chris Lattner16c7bb22002-05-09 02:28:59 +0000164static string calcTypeNameVar(const Type *Ty,
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000165 map<const Type *, string> &TypeNames,
166 const string &NameSoFar, bool ignoreName = false){
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000167 if (Ty->isPrimitiveType())
168 switch (Ty->getPrimitiveID()) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000169 case Type::VoidTyID: return "void " + NameSoFar;
170 case Type::BoolTyID: return "bool " + NameSoFar;
171 case Type::UByteTyID: return "unsigned char " + NameSoFar;
172 case Type::SByteTyID: return "signed char " + NameSoFar;
173 case Type::UShortTyID: return "unsigned short " + NameSoFar;
174 case Type::ShortTyID: return "short " + NameSoFar;
175 case Type::UIntTyID: return "unsigned " + NameSoFar;
176 case Type::IntTyID: return "int " + NameSoFar;
177 case Type::ULongTyID: return "unsigned long long " + NameSoFar;
178 case Type::LongTyID: return "signed long long " + NameSoFar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000179 default :
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000180 cerr << "Unknown primitive type: " << Ty << "\n";
181 abort();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000182 }
183
184 // Check to see if the type is named.
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000185 if (!ignoreName) {
186 map<const Type *, string>::iterator I = TypeNames.find(Ty);
187 if (I != TypeNames.end())
188 return I->second + " " + NameSoFar;
189 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000190
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000191 string Result;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000192 switch (Ty->getPrimitiveID()) {
193 case Type::FunctionTyID: {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000194 const FunctionType *MTy = cast<const FunctionType>(Ty);
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000195 Result += calcTypeNameVar(MTy->getReturnType(), TypeNames, "");
196 Result += " " + NameSoFar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000197 Result += " (";
198 for (FunctionType::ParamTypes::const_iterator
199 I = MTy->getParamTypes().begin(),
200 E = MTy->getParamTypes().end(); I != E; ++I) {
201 if (I != MTy->getParamTypes().begin())
202 Result += ", ";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000203 Result += calcTypeNameVar(*I, TypeNames, "");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000204 }
205 if (MTy->isVarArg()) {
206 if (!MTy->getParamTypes().empty())
207 Result += ", ";
208 Result += "...";
209 }
210 Result += ")";
211 break;
212 }
213 case Type::StructTyID: {
214 const StructType *STy = cast<const StructType>(Ty);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000215 Result = NameSoFar + " {\n";
216 unsigned indx = 0;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000217 for (StructType::ElementTypes::const_iterator
218 I = STy->getElementTypes().begin(),
219 E = STy->getElementTypes().end(); I != E; ++I) {
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000220 Result += " " +calcTypeNameVar(*I, TypeNames, "field" + utostr(indx++));
221 Result += ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000222 }
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000223 Result += "}";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000224 break;
225 }
226
227 case Type::PointerTyID: {
228 Result = calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(),
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000229 TypeNames, "*" + NameSoFar);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000230 break;
231 }
232
233 case Type::ArrayTyID: {
234 const ArrayType *ATy = cast<const ArrayType>(Ty);
235 int NumElements = ATy->getNumElements();
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000236 Result = calcTypeNameVar(ATy->getElementType(), TypeNames,
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000237 NameSoFar + "[" + itostr(NumElements) + "]");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000238 break;
239 }
240 default:
241 assert(0 && "Unhandled case in getTypeProps!");
242 Result = "<error>";
243 }
244
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000245 return Result;
246}
247
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000248namespace {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000249 class CWriter {
250 ostream& Out;
251 SlotCalculator &Table;
252 const Module *TheModule;
253 map<const Type *, string> TypeNames;
254 public:
255 inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M)
256 : Out(o), Table(Tab), TheModule(M) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000257 }
258
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000259 inline void write(Module *M) { printModule(M); }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000260
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000261 ostream& printTypeVar(const Type *Ty, const string &VariableName) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000262 return Out << calcTypeNameVar(Ty, TypeNames, VariableName);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000263 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000264
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000265 ostream& printType(const Type *Ty) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000266 return Out << calcTypeNameVar(Ty, TypeNames, "");
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000267 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000268
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000269 void writeOperand(const Value *Operand);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000270
271 string getValueName(const Value *V);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000272 private :
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000273
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000274 void printModule(Module *M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000275 void printSymbolTable(const SymbolTable &ST);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000276 void printGlobal(const GlobalVariable *GV);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000277 void printFunctionSignature(const Function *F);
278 void printFunctionDecl(const Function *F); // Print just the forward decl
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000279
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000280 void printFunction(Function *);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000281 };
282 /* END class CWriter */
283
284
285 /* CLASS InstLocalVarsVisitor */
286 class InstLocalVarsVisitor : public InstVisitor<InstLocalVarsVisitor> {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000287 CWriter& CW;
Chris Lattner16c7bb22002-05-09 02:28:59 +0000288 void handleTerminator(TerminatorInst *tI, int indx);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000289 public:
290 CLocalVars CLV;
291
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000292 InstLocalVarsVisitor(CWriter &cw) : CW(cw) {}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000293
294 void visitInstruction(Instruction *I) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000295 if (I->getType() != Type::VoidTy)
296 CLV.addLocalVar(I->getType(), CW.getValueName(I));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000297 }
298
299 void visitBranchInst(BranchInst *I) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000300 handleTerminator(I, 0);
301 if (I->isConditional())
302 handleTerminator(I, 1);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000303 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000304 };
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000305}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000306
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000307void InstLocalVarsVisitor::handleTerminator(TerminatorInst *tI,int indx) {
308 BasicBlock *bb = tI->getSuccessor(indx);
309
310 BasicBlock::const_iterator insIt = bb->begin();
311 while (insIt != bb->end()) {
312 if (const PHINode *pI = dyn_cast<PHINode>(*insIt)) {
313 // Its a phinode!
314 // Calculate the incoming index for this
315 assert(pI->getBasicBlockIndex(tI->getParent()) != -1);
316
317 CLV.addLocalVar(pI->getType(), CW.getValueName(pI));
318 } else
319 break;
320 insIt++;
321 }
322}
323
324namespace {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000325 /* CLASS CInstPrintVisitor */
326
327 class CInstPrintVisitor: public InstVisitor<CInstPrintVisitor> {
328 CWriter& CW;
329 SlotCalculator& Table;
330 ostream &Out;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000331
332 void outputLValue(Instruction *);
333 void printPhiFromNextBlock(TerminatorInst *tI, int indx);
Chris Lattner44408262002-05-09 03:50:42 +0000334 void printIndexingExpr(MemAccessInst *MAI);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000335
336 public:
337 CInstPrintVisitor (CWriter &cw, SlotCalculator& table, ostream& o)
Chris Lattner44408262002-05-09 03:50:42 +0000338 : CW(cw), Table(table), Out(o) {}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000339
340 void visitCastInst(CastInst *I);
341 void visitCallInst(CallInst *I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000342 void visitShiftInst(ShiftInst *I) { visitBinaryOperator(I); }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000343 void visitReturnInst(ReturnInst *I);
344 void visitBranchInst(BranchInst *I);
345 void visitSwitchInst(SwitchInst *I);
346 void visitInvokeInst(InvokeInst *I) ;
347 void visitMallocInst(MallocInst *I);
348 void visitAllocaInst(AllocaInst *I);
349 void visitFreeInst(FreeInst *I);
350 void visitLoadInst(LoadInst *I);
351 void visitStoreInst(StoreInst *I);
352 void visitGetElementPtrInst(GetElementPtrInst *I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000353 void visitPHINode(PHINode *I) {}
354
355 void visitNot(GenericUnaryInst *I);
356 void visitBinaryOperator(Instruction *I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000357 };
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000358}
359
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000360void CInstPrintVisitor::outputLValue(Instruction *I) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000361 Out << " " << CW.getValueName(I) << " = ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000362}
363
364void CInstPrintVisitor::printPhiFromNextBlock(TerminatorInst *tI, int indx) {
365 BasicBlock *bb = tI->getSuccessor(indx);
366 BasicBlock::const_iterator insIt = bb->begin();
367 while (insIt != bb->end()) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000368 if (PHINode *pI = dyn_cast<PHINode>(*insIt)) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000369 //Its a phinode!
370 //Calculate the incoming index for this
371 int incindex = pI->getBasicBlockIndex(tI->getParent());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000372 if (incindex != -1) {
373 //now we have to do the printing
374 outputLValue(pI);
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000375 CW.writeOperand(pI->getIncomingValue(incindex));
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000376 Out << ";\n";
377 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000378 }
379 else break;
380 insIt++;
381 }
382}
383
384// Implement all "other" instructions, except for PHINode
385void CInstPrintVisitor::visitCastInst(CastInst *I) {
386 outputLValue(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000387 Out << "(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000388 CW.printType(I->getType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000389 Out << ")";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000390 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000391 Out << ";\n";
392}
393
394void CInstPrintVisitor::visitCallInst(CallInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000395 if (I->getType() != Type::VoidTy)
396 outputLValue(I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000397 else
398 Out << " ";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000399
Chris Lattner2f499022002-05-09 04:21:21 +0000400 const PointerType *PTy = cast<PointerType>(I->getCalledValue()->getType());
401 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
402 const Type *RetTy = FTy->getReturnType();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000403
Chris Lattner2f499022002-05-09 04:21:21 +0000404 Out << CW.getValueName(I->getOperand(0)) << "(";
405
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000406 if (I->getNumOperands() > 1) {
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000407 CW.writeOperand(I->getOperand(1));
Chris Lattner2f499022002-05-09 04:21:21 +0000408
409 for (unsigned op = 2, Eop = I->getNumOperands(); op != Eop; ++op) {
410 Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000411 CW.writeOperand(I->getOperand(op));
Chris Lattner2f499022002-05-09 04:21:21 +0000412 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000413 }
Chris Lattner2f499022002-05-09 04:21:21 +0000414 Out << ");\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000415}
416
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000417// Specific Instruction type classes... note that all of the casts are
418// neccesary because we use the instruction classes as opaque types...
419//
420void CInstPrintVisitor::visitReturnInst(ReturnInst *I) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000421 Out << " return";
422 if (I->getNumOperands()) {
423 Out << " ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000424 CW.writeOperand(I->getOperand(0));
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000425 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000426 Out << ";\n";
427}
428
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000429// Return true if BB1 immediately preceeds BB2.
430static bool BBFollowsBB(BasicBlock *BB1, BasicBlock *BB2) {
431 Function *F = BB1->getParent();
432 Function::iterator I = find(F->begin(), F->end(), BB1);
433 assert(I != F->end() && "BB not in function!");
434 return *(I+1) == BB2;
435}
436
437// Brach instruction printing - Avoid printing out a brach to a basic block that
438// immediately succeeds the current one.
439//
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000440void CInstPrintVisitor::visitBranchInst(BranchInst *I) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000441 TerminatorInst *tI = cast<TerminatorInst>(I);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000442 if (I->isConditional()) {
443 Out << " if (";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000444 CW.writeOperand(I->getCondition());
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000445 Out << ") {\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000446 printPhiFromNextBlock(tI,0);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000447
448 if (!BBFollowsBB(I->getParent(), cast<BasicBlock>(I->getOperand(0)))) {
449 Out << " goto ";
450 CW.writeOperand(I->getOperand(0));
451 Out << ";\n";
452 }
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000453 Out << " } else {\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000454 printPhiFromNextBlock(tI,1);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000455
456 if (!BBFollowsBB(I->getParent(), cast<BasicBlock>(I->getOperand(1)))) {
457 Out << " goto ";
458 CW.writeOperand(I->getOperand(1));
459 Out << ";\n";
460 }
461 Out << " }\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000462 } else {
463 printPhiFromNextBlock(tI,0);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000464
465 if (!BBFollowsBB(I->getParent(), cast<BasicBlock>(I->getOperand(0)))) {
466 Out << " goto ";
467 CW.writeOperand(I->getOperand(0));
468 Out << ";\n";
469 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000470 }
471 Out << "\n";
472}
473
474void CInstPrintVisitor::visitSwitchInst(SwitchInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000475 assert(0 && "Switch not implemented!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000476}
477
478void CInstPrintVisitor::visitInvokeInst(InvokeInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000479 assert(0 && "Invoke not implemented!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000480}
481
482void CInstPrintVisitor::visitMallocInst(MallocInst *I) {
483 outputLValue(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000484 Out << "(";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000485 CW.printType(I->getType());
486 Out << ")malloc(sizeof(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000487 CW.printType(I->getType()->getElementType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000488 Out << ")";
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000489
490 if (I->isArrayAllocation()) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000491 Out << " * " ;
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000492 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000493 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000494 Out << ");\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000495}
496
497void CInstPrintVisitor::visitAllocaInst(AllocaInst *I) {
498 outputLValue(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000499 Out << "(";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000500 CW.printType(I->getType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000501 Out << ") alloca(sizeof(";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000502 CW.printType(I->getType()->getElementType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000503 Out << ")";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000504 if (I->isArrayAllocation()) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000505 Out << " * " ;
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000506 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000507 }
508 Out << ");\n";
509}
510
511void CInstPrintVisitor::visitFreeInst(FreeInst *I) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000512 Out << "free(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000513 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000514 Out << ");\n";
515}
516
Chris Lattner44408262002-05-09 03:50:42 +0000517void CInstPrintVisitor::printIndexingExpr(MemAccessInst *MAI) {
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000518 CW.writeOperand(MAI->getPointerOperand());
Chris Lattner44408262002-05-09 03:50:42 +0000519
520 for (MemAccessInst::op_iterator I = MAI->idx_begin(), E = MAI->idx_end();
521 I != E; ++I)
522 if ((*I)->getType() == Type::UIntTy) {
523 Out << "[";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000524 CW.writeOperand(*I);
Chris Lattner44408262002-05-09 03:50:42 +0000525 Out << "]";
526 } else {
527 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000528 }
Chris Lattner44408262002-05-09 03:50:42 +0000529}
530
531void CInstPrintVisitor::visitLoadInst(LoadInst *I) {
532 outputLValue(I);
533 printIndexingExpr(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000534 Out << ";\n";
535}
536
Chris Lattner44408262002-05-09 03:50:42 +0000537void CInstPrintVisitor::visitStoreInst(StoreInst *I) {
538 Out << " ";
539 printIndexingExpr(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000540 Out << " = ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000541 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000542 Out << ";\n";
543}
544
545void CInstPrintVisitor::visitGetElementPtrInst(GetElementPtrInst *I) {
546 outputLValue(I);
Chris Lattner44408262002-05-09 03:50:42 +0000547 Out << "&";
548 printIndexingExpr(I);
549 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000550}
551
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000552void CInstPrintVisitor::visitNot(GenericUnaryInst *I) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000553 outputLValue(I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000554 Out << "~";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000555 CW.writeOperand(I->getOperand(0));
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000556 Out << ";\n";
557}
558
559void CInstPrintVisitor::visitBinaryOperator(Instruction *I) {
560 // binary instructions, shift instructions, setCond instructions.
561 outputLValue(I);
562 if (isa<PointerType>(I->getType())) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000563 Out << "(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000564 CW.printType(I->getType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000565 Out << ")";
566 }
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000567
568 if (isa<PointerType>(I->getType())) Out << "(long long)";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000569 CW.writeOperand(I->getOperand(0));
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000570
571 switch (I->getOpcode()) {
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000572 case Instruction::Add: Out << " + "; break;
573 case Instruction::Sub: Out << " - "; break;
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000574 case Instruction::Mul: Out << "*"; break;
575 case Instruction::Div: Out << "/"; break;
576 case Instruction::Rem: Out << "%"; break;
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000577 case Instruction::And: Out << " & "; break;
578 case Instruction::Or: Out << " | "; break;
579 case Instruction::Xor: Out << " ^ "; break;
580 case Instruction::SetEQ: Out << " == "; break;
581 case Instruction::SetNE: Out << " != "; break;
582 case Instruction::SetLE: Out << " <= "; break;
583 case Instruction::SetGE: Out << " >= "; break;
584 case Instruction::SetLT: Out << " < "; break;
585 case Instruction::SetGT: Out << " > "; break;
586 case Instruction::Shl : Out << " << "; break;
587 case Instruction::Shr : Out << " >> "; break;
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000588 default: cerr << "Invalid operator type!" << I; abort();
589 }
590
591 if (isa<PointerType>(I->getType())) Out << "(long long)";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000592 CW.writeOperand(I->getOperand(1));
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000593 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000594}
595
596/* END : CInstPrintVisitor implementation */
597
Chris Lattner2f499022002-05-09 04:21:21 +0000598// We dont want identifier names with ., space, - in them.
599// So we replace them with _
600static string makeNameProper(string x) {
601 string tmp;
602 for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
603 switch (*sI) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000604 case '.': tmp += "d_"; break;
605 case ' ': tmp += "s_"; break;
606 case '-': tmp += "D_"; break;
Chris Lattner2f499022002-05-09 04:21:21 +0000607 case '_': tmp += "__"; break;
608 default: tmp += *sI;
609 }
610
611 return tmp;
612}
613
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000614string CWriter::getValueName(const Value *V) {
Chris Lattner2f499022002-05-09 04:21:21 +0000615 if (V->hasName()) { // Print out the label if it exists...
616 if (isa<GlobalValue>(V)) // Do not mangle globals...
617 return makeNameProper(V->getName());
618
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000619 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
620 makeNameProper(V->getName());
Chris Lattner2f499022002-05-09 04:21:21 +0000621 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000622
623 int Slot = Table.getValSlot(V);
624 assert(Slot >= 0 && "Invalid value!");
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000625 return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000626}
627
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000628void CWriter::printModule(Module *M) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000629 // printing stdlib inclusion
630 // Out << "#include <stdlib.h>\n";
631
Chris Lattner16c7bb22002-05-09 02:28:59 +0000632 // get declaration for alloca
633 Out << "/* Provide Declarations */\n"
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000634 << "#include <alloca.h>\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000635
636 // Provide a definition for null if one does not already exist.
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000637 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000638 << "typedef unsigned char bool;\n"
639
640 << "\n\n/* Global Symbols */\n";
641
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000642 // Loop over the symbol table, emitting all named constants...
643 if (M->hasSymbolTable())
644 printSymbolTable(*M->getSymbolTable());
645
Chris Lattner16c7bb22002-05-09 02:28:59 +0000646 Out << "\n\n/* Global Data */\n";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000647 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
648 GlobalVariable *GV = *I;
649 if (GV->hasInternalLinkage()) Out << "static ";
650 printTypeVar(GV->getType()->getElementType(), getValueName(GV));
651
652 if (GV->hasInitializer()) {
653 Out << " = " ;
654 writeOperand(GV->getInitializer());
655 }
656 Out << ";\n";
657 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000658
Chris Lattner16c7bb22002-05-09 02:28:59 +0000659 // First output all the declarations of the functions as C requires Functions
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000660 // be declared before they are used.
Chris Lattner16c7bb22002-05-09 02:28:59 +0000661 //
662 Out << "\n\n/* Function Declarations */\n";
663 for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunctionDecl));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000664
Chris Lattner16c7bb22002-05-09 02:28:59 +0000665 // Output all of the functions...
666 Out << "\n\n/* Function Bodies */\n";
667 for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunction));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000668}
669
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000670
671// printSymbolTable - Run through symbol table looking for named constants
672// if a named constant is found, emit it's declaration...
673// Assuming that symbol table has only types and constants.
674void CWriter::printSymbolTable(const SymbolTable &ST) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000675 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
676 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
677 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
678
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000679 for (; I != End; ++I)
680 if (const Type *Ty = dyn_cast<const StructType>(I->second)) {
681 string Name = "struct l_" + I->first;
682 Out << Name << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000683
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000684 TypeNames.insert(std::make_pair(Ty, Name));
685 }
686 }
687
688 Out << "\n";
689
690 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
691 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
692 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
693
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000694 for (; I != End; ++I) {
695 const Value *V = I->second;
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000696 if (const Type *Ty = dyn_cast<const Type>(V)) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000697 Out << "typedef ";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000698 string Name = "l_" + I->first;
699 if (isa<StructType>(Ty)) Name = "struct " + Name;
700 Out << calcTypeNameVar(Ty, TypeNames, Name, true) << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000701 }
702 }
703 }
704}
705
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000706
Chris Lattner16c7bb22002-05-09 02:28:59 +0000707// printFunctionDecl - Print function declaration
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000708//
Chris Lattner16c7bb22002-05-09 02:28:59 +0000709void CWriter::printFunctionDecl(const Function *F) {
710 printFunctionSignature(F);
711 Out << ";\n";
712}
713
714void CWriter::printFunctionSignature(const Function *F) {
715 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000716
717 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000718 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000719
Chris Lattner16c7bb22002-05-09 02:28:59 +0000720 // Print out the return type and name...
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000721 printType(F->getReturnType());
Chris Lattner2f499022002-05-09 04:21:21 +0000722 Out << " " << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000723
Chris Lattner16c7bb22002-05-09 02:28:59 +0000724 if (!F->isExternal()) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000725 if (!F->getArgumentList().empty()) {
726 printTypeVar(F->getArgumentList().front()->getType(),
727 getValueName(F->getArgumentList().front()));
728
729 for (Function::ArgumentListType::const_iterator
730 I = F->getArgumentList().begin()+1,
731 E = F->getArgumentList().end(); I != E; ++I) {
732 Out << ", ";
733 printTypeVar((*I)->getType(), getValueName(*I));
734 }
735 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000736 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000737 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000738 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000739 FT->getParamTypes().begin(),
740 E = FT->getParamTypes().end(); I != E; ++I) {
741 if (I != FT->getParamTypes().begin()) Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000742 printType(*I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000743 }
744 }
745
746 // Finish printing arguments...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000747 if (FT->isVarArg()) {
748 if (FT->getParamTypes().size()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000749 Out << "..."; // Output varargs portion of signature!
750 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000751 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000752}
753
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000754
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000755void CWriter::printFunction(Function *F) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000756 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000757
Chris Lattnerf34ee812002-05-09 03:12:34 +0000758 Table.incorporateFunction(F);
759
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000760 printFunctionSignature(F);
761 Out << " {\n";
762
Chris Lattner16c7bb22002-05-09 02:28:59 +0000763 // Process each of the basic blocks, gather information and call the
764 // output methods on the CLocalVars and Function* objects.
765
766 // gather local variable information for each basic block
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000767 InstLocalVarsVisitor ILV(*this);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000768 ILV.visit(F);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000769
Chris Lattner16c7bb22002-05-09 02:28:59 +0000770 // print the local variables
771 // we assume that every local variable is alloca'ed in the C code.
772 std::map<const Type*, VarListType> &locals = ILV.CLV.LocalVars;
773
774 map<const Type*, VarListType>::iterator iter;
775 for (iter = locals.begin(); iter != locals.end(); ++iter) {
776 VarListType::iterator listiter;
777 for (listiter = iter->second.begin(); listiter != iter->second.end();
778 ++listiter) {
779 Out << " ";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000780 printTypeVar(iter->first, *listiter);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000781 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000782 }
783 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000784
785 // print the basic blocks
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000786 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
787 BasicBlock *BB = *I, *Prev = I != F->begin() ? *(I-1) : 0;
788
789 // Don't print the label for the basic block if there are no uses, or if the
790 // only terminator use is the precessor basic block's terminator. We have
791 // to scan the use list because PHI nodes use basic blocks too but do not
792 // require a label to be generated.
793 //
794 bool NeedsLabel = false;
795 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
796 UI != UE; ++UI)
797 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
798 if (TI != Prev->getTerminator()) {
799 NeedsLabel = true;
800 break;
801 }
802
803 if (NeedsLabel) Out << getValueName(BB) << ":\n";
804
805 // Output all of the instructions in the basic block...
806 // print the basic blocks
807 CInstPrintVisitor CIPV(*this, Table, Out);
808 CIPV.visit(BB);
809 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000810
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000811 Out << "}\n\n";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000812 Table.purgeFunction();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000813}
814
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000815void CWriter::writeOperand(const Value *Operand) {
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000816 if (isa<GlobalVariable>(Operand))
817 Out << "(&"; // Global variables are references as their addresses by llvm
Chris Lattner44408262002-05-09 03:50:42 +0000818
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000819 if (Operand->hasName()) {
Chris Lattner2f499022002-05-09 04:21:21 +0000820 Out << getValueName(Operand);
Chris Lattner44408262002-05-09 03:50:42 +0000821 } else if (const Constant *CPV = dyn_cast<const Constant>(Operand)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000822 if (isa<ConstantPointerNull>(CPV))
823 Out << "NULL";
824 else
825 Out << getConstStrValue(CPV);
Chris Lattner44408262002-05-09 03:50:42 +0000826 } else {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000827 int Slot = Table.getValSlot(Operand);
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000828 assert(Slot >= 0 && "Malformed LLVM!");
829 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
Chris Lattner16c7bb22002-05-09 02:28:59 +0000830 }
Chris Lattner44408262002-05-09 03:50:42 +0000831
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000832 if (isa<GlobalVariable>(Operand))
Chris Lattner44408262002-05-09 03:50:42 +0000833 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000834}
835
836
837//===----------------------------------------------------------------------===//
838// External Interface declaration
839//===----------------------------------------------------------------------===//
840
Chris Lattnerf34ee812002-05-09 03:12:34 +0000841void WriteToC(const Module *M, ostream &Out) {
842 assert(M && "You can't write a null module!!");
843 SlotCalculator SlotTable(M, false);
844 CWriter W(Out, SlotTable, M);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000845 W.write((Module*)M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000846 Out.flush();
847}