blob: c8a06a3c10c1c11945f0301e79ea3fde8de59fe5 [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);
215 Result = " struct {\n ";
216 int indx = 0;
217 for (StructType::ElementTypes::const_iterator
218 I = STy->getElementTypes().begin(),
219 E = STy->getElementTypes().end(); I != E; ++I) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000220 Result += calcTypeNameVar(*I, TypeNames, "field" + itostr(indx++));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000221 Result += ";\n ";
222 }
223 Result += " }";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000224 Result += " " + NameSoFar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000225 break;
226 }
227
228 case Type::PointerTyID: {
229 Result = calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(),
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000230 TypeNames, "*" + NameSoFar);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000231 break;
232 }
233
234 case Type::ArrayTyID: {
235 const ArrayType *ATy = cast<const ArrayType>(Ty);
236 int NumElements = ATy->getNumElements();
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000237 Result = calcTypeNameVar(ATy->getElementType(), TypeNames,
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000238 NameSoFar + "[" + itostr(NumElements) + "]");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000239 break;
240 }
241 default:
242 assert(0 && "Unhandled case in getTypeProps!");
243 Result = "<error>";
244 }
245
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000246 return Result;
247}
248
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000249namespace {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000250 class CWriter {
251 ostream& Out;
252 SlotCalculator &Table;
253 const Module *TheModule;
254 map<const Type *, string> TypeNames;
255 public:
256 inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M)
257 : Out(o), Table(Tab), TheModule(M) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000258 }
259
Chris Lattner16c7bb22002-05-09 02:28:59 +0000260 inline void write(const Module *M) { printModule(M); }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000261
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000262 ostream& printTypeVar(const Type *Ty, const string &VariableName) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000263 return Out << calcTypeNameVar(Ty, TypeNames, VariableName);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000264 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000265
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000266 ostream& printType(const Type *Ty) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000267 return Out << calcTypeNameVar(Ty, TypeNames, "");
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000268 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000269
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000270 void writeOperand(const Value *Operand);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000271
272 string getValueName(const Value *V);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000273 private :
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000274
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000275 void printModule(const Module *M);
276 void printSymbolTable(const SymbolTable &ST);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000277 void printGlobal(const GlobalVariable *GV);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000278 void printFunctionSignature(const Function *F);
279 void printFunctionDecl(const Function *F); // Print just the forward decl
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000280
281 void printFunction(const Function *);
282
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000283 void outputBasicBlock(const BasicBlock *);
284 };
285 /* END class CWriter */
286
287
288 /* CLASS InstLocalVarsVisitor */
289 class InstLocalVarsVisitor : public InstVisitor<InstLocalVarsVisitor> {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000290 CWriter& CW;
Chris Lattner16c7bb22002-05-09 02:28:59 +0000291 void handleTerminator(TerminatorInst *tI, int indx);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000292 public:
293 CLocalVars CLV;
294
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000295 InstLocalVarsVisitor(CWriter &cw) : CW(cw) {}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000296
297 void visitInstruction(Instruction *I) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000298 if (I->getType() != Type::VoidTy)
299 CLV.addLocalVar(I->getType(), CW.getValueName(I));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000300 }
301
302 void visitBranchInst(BranchInst *I) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000303 handleTerminator(I, 0);
304 if (I->isConditional())
305 handleTerminator(I, 1);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000306 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000307 };
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000308}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000309
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000310void InstLocalVarsVisitor::handleTerminator(TerminatorInst *tI,int indx) {
311 BasicBlock *bb = tI->getSuccessor(indx);
312
313 BasicBlock::const_iterator insIt = bb->begin();
314 while (insIt != bb->end()) {
315 if (const PHINode *pI = dyn_cast<PHINode>(*insIt)) {
316 // Its a phinode!
317 // Calculate the incoming index for this
318 assert(pI->getBasicBlockIndex(tI->getParent()) != -1);
319
320 CLV.addLocalVar(pI->getType(), CW.getValueName(pI));
321 } else
322 break;
323 insIt++;
324 }
325}
326
327namespace {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000328 /* CLASS CInstPrintVisitor */
329
330 class CInstPrintVisitor: public InstVisitor<CInstPrintVisitor> {
331 CWriter& CW;
332 SlotCalculator& Table;
333 ostream &Out;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000334
335 void outputLValue(Instruction *);
336 void printPhiFromNextBlock(TerminatorInst *tI, int indx);
Chris Lattner44408262002-05-09 03:50:42 +0000337 void printIndexingExpr(MemAccessInst *MAI);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000338
339 public:
340 CInstPrintVisitor (CWriter &cw, SlotCalculator& table, ostream& o)
Chris Lattner44408262002-05-09 03:50:42 +0000341 : CW(cw), Table(table), Out(o) {}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000342
343 void visitCastInst(CastInst *I);
344 void visitCallInst(CallInst *I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000345 void visitShiftInst(ShiftInst *I) { visitBinaryOperator(I); }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000346 void visitReturnInst(ReturnInst *I);
347 void visitBranchInst(BranchInst *I);
348 void visitSwitchInst(SwitchInst *I);
349 void visitInvokeInst(InvokeInst *I) ;
350 void visitMallocInst(MallocInst *I);
351 void visitAllocaInst(AllocaInst *I);
352 void visitFreeInst(FreeInst *I);
353 void visitLoadInst(LoadInst *I);
354 void visitStoreInst(StoreInst *I);
355 void visitGetElementPtrInst(GetElementPtrInst *I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000356 void visitPHINode(PHINode *I) {}
357
358 void visitNot(GenericUnaryInst *I);
359 void visitBinaryOperator(Instruction *I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000360 };
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000361}
362
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000363void CInstPrintVisitor::outputLValue(Instruction *I) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000364 Out << " " << CW.getValueName(I) << " = ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000365}
366
367void CInstPrintVisitor::printPhiFromNextBlock(TerminatorInst *tI, int indx) {
368 BasicBlock *bb = tI->getSuccessor(indx);
369 BasicBlock::const_iterator insIt = bb->begin();
370 while (insIt != bb->end()) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000371 if (PHINode *pI = dyn_cast<PHINode>(*insIt)) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000372 //Its a phinode!
373 //Calculate the incoming index for this
374 int incindex = pI->getBasicBlockIndex(tI->getParent());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000375 if (incindex != -1) {
376 //now we have to do the printing
377 outputLValue(pI);
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000378 CW.writeOperand(pI->getIncomingValue(incindex));
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000379 Out << ";\n";
380 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000381 }
382 else break;
383 insIt++;
384 }
385}
386
387// Implement all "other" instructions, except for PHINode
388void CInstPrintVisitor::visitCastInst(CastInst *I) {
389 outputLValue(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000390 Out << "(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000391 CW.printType(I->getType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000392 Out << ")";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000393 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000394 Out << ";\n";
395}
396
397void CInstPrintVisitor::visitCallInst(CallInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000398 if (I->getType() != Type::VoidTy)
399 outputLValue(I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000400 else
401 Out << " ";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000402
Chris Lattner2f499022002-05-09 04:21:21 +0000403 const PointerType *PTy = cast<PointerType>(I->getCalledValue()->getType());
404 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
405 const Type *RetTy = FTy->getReturnType();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000406
Chris Lattner2f499022002-05-09 04:21:21 +0000407 Out << CW.getValueName(I->getOperand(0)) << "(";
408
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000409 if (I->getNumOperands() > 1) {
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000410 CW.writeOperand(I->getOperand(1));
Chris Lattner2f499022002-05-09 04:21:21 +0000411
412 for (unsigned op = 2, Eop = I->getNumOperands(); op != Eop; ++op) {
413 Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000414 CW.writeOperand(I->getOperand(op));
Chris Lattner2f499022002-05-09 04:21:21 +0000415 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000416 }
Chris Lattner2f499022002-05-09 04:21:21 +0000417 Out << ");\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000418}
419
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000420// Specific Instruction type classes... note that all of the casts are
421// neccesary because we use the instruction classes as opaque types...
422//
423void CInstPrintVisitor::visitReturnInst(ReturnInst *I) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000424 Out << " return";
425 if (I->getNumOperands()) {
426 Out << " ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000427 CW.writeOperand(I->getOperand(0));
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000428 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000429 Out << ";\n";
430}
431
432void CInstPrintVisitor::visitBranchInst(BranchInst *I) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000433 TerminatorInst *tI = cast<TerminatorInst>(I);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000434 if (I->isConditional()) {
435 Out << " if (";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000436 CW.writeOperand(I->getCondition());
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000437 Out << ") {\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000438 printPhiFromNextBlock(tI,0);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000439 Out << " goto ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000440 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000441 Out << ";\n";
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000442 Out << " } else {\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000443 printPhiFromNextBlock(tI,1);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000444 Out << " goto ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000445 CW.writeOperand(I->getOperand(1));
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000446 Out << ";\n }\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000447 } else {
448 printPhiFromNextBlock(tI,0);
449 Out << " goto ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000450 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000451 Out << ";\n";
452 }
453 Out << "\n";
454}
455
456void CInstPrintVisitor::visitSwitchInst(SwitchInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000457 assert(0 && "Switch not implemented!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000458}
459
460void CInstPrintVisitor::visitInvokeInst(InvokeInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000461 assert(0 && "Invoke not implemented!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000462}
463
464void CInstPrintVisitor::visitMallocInst(MallocInst *I) {
465 outputLValue(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000466 Out << "(";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000467 CW.printType(I->getType());
468 Out << ")malloc(sizeof(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000469 CW.printType(I->getType()->getElementType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000470 Out << ")";
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000471
472 if (I->isArrayAllocation()) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000473 Out << " * " ;
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000474 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000475 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000476 Out << ");\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000477}
478
479void CInstPrintVisitor::visitAllocaInst(AllocaInst *I) {
480 outputLValue(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000481 Out << "(";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000482 CW.printType(I->getType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000483 Out << ") alloca(sizeof(";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000484 CW.printType(I->getType()->getElementType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000485 Out << ")";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000486 if (I->isArrayAllocation()) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000487 Out << " * " ;
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000488 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000489 }
490 Out << ");\n";
491}
492
493void CInstPrintVisitor::visitFreeInst(FreeInst *I) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000494 Out << "free(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000495 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000496 Out << ");\n";
497}
498
Chris Lattner44408262002-05-09 03:50:42 +0000499void CInstPrintVisitor::printIndexingExpr(MemAccessInst *MAI) {
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000500 CW.writeOperand(MAI->getPointerOperand());
Chris Lattner44408262002-05-09 03:50:42 +0000501
502 for (MemAccessInst::op_iterator I = MAI->idx_begin(), E = MAI->idx_end();
503 I != E; ++I)
504 if ((*I)->getType() == Type::UIntTy) {
505 Out << "[";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000506 CW.writeOperand(*I);
Chris Lattner44408262002-05-09 03:50:42 +0000507 Out << "]";
508 } else {
509 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000510 }
Chris Lattner44408262002-05-09 03:50:42 +0000511}
512
513void CInstPrintVisitor::visitLoadInst(LoadInst *I) {
514 outputLValue(I);
515 printIndexingExpr(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000516 Out << ";\n";
517}
518
Chris Lattner44408262002-05-09 03:50:42 +0000519void CInstPrintVisitor::visitStoreInst(StoreInst *I) {
520 Out << " ";
521 printIndexingExpr(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000522 Out << " = ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000523 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000524 Out << ";\n";
525}
526
527void CInstPrintVisitor::visitGetElementPtrInst(GetElementPtrInst *I) {
528 outputLValue(I);
Chris Lattner44408262002-05-09 03:50:42 +0000529 Out << "&";
530 printIndexingExpr(I);
531 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000532}
533
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000534void CInstPrintVisitor::visitNot(GenericUnaryInst *I) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000535 outputLValue(I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000536 Out << "~";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000537 CW.writeOperand(I->getOperand(0));
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000538 Out << ";\n";
539}
540
541void CInstPrintVisitor::visitBinaryOperator(Instruction *I) {
542 // binary instructions, shift instructions, setCond instructions.
543 outputLValue(I);
544 if (isa<PointerType>(I->getType())) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000545 Out << "(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000546 CW.printType(I->getType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000547 Out << ")";
548 }
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000549
550 if (isa<PointerType>(I->getType())) Out << "(long long)";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000551 CW.writeOperand(I->getOperand(0));
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000552
553 switch (I->getOpcode()) {
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000554 case Instruction::Add: Out << " + "; break;
555 case Instruction::Sub: Out << " - "; break;
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000556 case Instruction::Mul: Out << "*"; break;
557 case Instruction::Div: Out << "/"; break;
558 case Instruction::Rem: Out << "%"; break;
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000559 case Instruction::And: Out << " & "; break;
560 case Instruction::Or: Out << " | "; break;
561 case Instruction::Xor: Out << " ^ "; break;
562 case Instruction::SetEQ: Out << " == "; break;
563 case Instruction::SetNE: Out << " != "; break;
564 case Instruction::SetLE: Out << " <= "; break;
565 case Instruction::SetGE: Out << " >= "; break;
566 case Instruction::SetLT: Out << " < "; break;
567 case Instruction::SetGT: Out << " > "; break;
568 case Instruction::Shl : Out << " << "; break;
569 case Instruction::Shr : Out << " >> "; break;
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000570 default: cerr << "Invalid operator type!" << I; abort();
571 }
572
573 if (isa<PointerType>(I->getType())) Out << "(long long)";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000574 CW.writeOperand(I->getOperand(1));
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000575 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000576}
577
578/* END : CInstPrintVisitor implementation */
579
Chris Lattner2f499022002-05-09 04:21:21 +0000580// We dont want identifier names with ., space, - in them.
581// So we replace them with _
582static string makeNameProper(string x) {
583 string tmp;
584 for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
585 switch (*sI) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000586 case '.': tmp += "d_"; break;
587 case ' ': tmp += "s_"; break;
588 case '-': tmp += "D_"; break;
Chris Lattner2f499022002-05-09 04:21:21 +0000589 case '_': tmp += "__"; break;
590 default: tmp += *sI;
591 }
592
593 return tmp;
594}
595
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000596string CWriter::getValueName(const Value *V) {
Chris Lattner2f499022002-05-09 04:21:21 +0000597 if (V->hasName()) { // Print out the label if it exists...
598 if (isa<GlobalValue>(V)) // Do not mangle globals...
599 return makeNameProper(V->getName());
600
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000601 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
602 makeNameProper(V->getName());
Chris Lattner2f499022002-05-09 04:21:21 +0000603 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000604
605 int Slot = Table.getValSlot(V);
606 assert(Slot >= 0 && "Invalid value!");
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000607 return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000608}
609
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000610void CWriter::printModule(const Module *M) {
611 // printing stdlib inclusion
612 // Out << "#include <stdlib.h>\n";
613
Chris Lattner16c7bb22002-05-09 02:28:59 +0000614 // get declaration for alloca
615 Out << "/* Provide Declarations */\n"
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000616 << "#include <alloca.h>\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000617
618 // Provide a definition for null if one does not already exist.
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000619 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000620 << "typedef unsigned char bool;\n"
621
622 << "\n\n/* Global Symbols */\n";
623
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000624 // Loop over the symbol table, emitting all named constants...
625 if (M->hasSymbolTable())
626 printSymbolTable(*M->getSymbolTable());
627
Chris Lattner16c7bb22002-05-09 02:28:59 +0000628 Out << "\n\n/* Global Data */\n";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000629 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
630 GlobalVariable *GV = *I;
631 if (GV->hasInternalLinkage()) Out << "static ";
632 printTypeVar(GV->getType()->getElementType(), getValueName(GV));
633
634 if (GV->hasInitializer()) {
635 Out << " = " ;
636 writeOperand(GV->getInitializer());
637 }
638 Out << ";\n";
639 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000640
Chris Lattner16c7bb22002-05-09 02:28:59 +0000641 // First output all the declarations of the functions as C requires Functions
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000642 // be declared before they are used.
Chris Lattner16c7bb22002-05-09 02:28:59 +0000643 //
644 Out << "\n\n/* Function Declarations */\n";
645 for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunctionDecl));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000646
Chris Lattner16c7bb22002-05-09 02:28:59 +0000647 // Output all of the functions...
648 Out << "\n\n/* Function Bodies */\n";
649 for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunction));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000650}
651
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000652
653// printSymbolTable - Run through symbol table looking for named constants
654// if a named constant is found, emit it's declaration...
655// Assuming that symbol table has only types and constants.
656void CWriter::printSymbolTable(const SymbolTable &ST) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000657 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
658 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
659 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
660
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000661 for (; I != End; ++I)
662 if (const Type *Ty = dyn_cast<const StructType>(I->second)) {
663 string Name = "struct l_" + I->first;
664 Out << Name << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000665
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000666 TypeNames.insert(std::make_pair(Ty, Name));
667 }
668 }
669
670 Out << "\n";
671
672 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
673 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
674 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
675
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000676 for (; I != End; ++I) {
677 const Value *V = I->second;
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000678 if (const Type *Ty = dyn_cast<const Type>(V)) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000679 Out << "typedef ";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000680 string Name = "l_" + I->first;
681 if (isa<StructType>(Ty)) Name = "struct " + Name;
682 Out << calcTypeNameVar(Ty, TypeNames, Name, true) << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000683 }
684 }
685 }
686}
687
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000688
Chris Lattner16c7bb22002-05-09 02:28:59 +0000689// printFunctionDecl - Print function declaration
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000690//
Chris Lattner16c7bb22002-05-09 02:28:59 +0000691void CWriter::printFunctionDecl(const Function *F) {
692 printFunctionSignature(F);
693 Out << ";\n";
694}
695
696void CWriter::printFunctionSignature(const Function *F) {
697 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000698
699 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000700 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000701
Chris Lattner16c7bb22002-05-09 02:28:59 +0000702 // Print out the return type and name...
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000703 printType(F->getReturnType());
Chris Lattner2f499022002-05-09 04:21:21 +0000704 Out << " " << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000705
Chris Lattner16c7bb22002-05-09 02:28:59 +0000706 if (!F->isExternal()) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000707 if (!F->getArgumentList().empty()) {
708 printTypeVar(F->getArgumentList().front()->getType(),
709 getValueName(F->getArgumentList().front()));
710
711 for (Function::ArgumentListType::const_iterator
712 I = F->getArgumentList().begin()+1,
713 E = F->getArgumentList().end(); I != E; ++I) {
714 Out << ", ";
715 printTypeVar((*I)->getType(), getValueName(*I));
716 }
717 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000718 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000719 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000720 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000721 FT->getParamTypes().begin(),
722 E = FT->getParamTypes().end(); I != E; ++I) {
723 if (I != FT->getParamTypes().begin()) Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000724 printType(*I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000725 }
726 }
727
728 // Finish printing arguments...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000729 if (FT->isVarArg()) {
730 if (FT->getParamTypes().size()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000731 Out << "..."; // Output varargs portion of signature!
732 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000733 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000734}
735
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000736
Chris Lattner16c7bb22002-05-09 02:28:59 +0000737void CWriter::printFunction(const Function *F) {
738 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000739
Chris Lattnerf34ee812002-05-09 03:12:34 +0000740 Table.incorporateFunction(F);
741
Chris Lattner16c7bb22002-05-09 02:28:59 +0000742 // Process each of the basic blocks, gather information and call the
743 // output methods on the CLocalVars and Function* objects.
744
745 // gather local variable information for each basic block
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000746 InstLocalVarsVisitor ILV(*this);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000747 ILV.visit((Function *)F);
748
749 printFunctionSignature(F);
750 Out << " {\n";
751
752 // Loop over the symbol table, emitting all named constants...
753 if (F->hasSymbolTable())
754 printSymbolTable(*F->getSymbolTable());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000755
Chris Lattner16c7bb22002-05-09 02:28:59 +0000756 // print the local variables
757 // we assume that every local variable is alloca'ed in the C code.
758 std::map<const Type*, VarListType> &locals = ILV.CLV.LocalVars;
759
760 map<const Type*, VarListType>::iterator iter;
761 for (iter = locals.begin(); iter != locals.end(); ++iter) {
762 VarListType::iterator listiter;
763 for (listiter = iter->second.begin(); listiter != iter->second.end();
764 ++listiter) {
765 Out << " ";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000766 printTypeVar(iter->first, *listiter);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000767 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000768 }
769 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000770
771 // print the basic blocks
772 for_each(F->begin(), F->end(), bind_obj(this, &CWriter::outputBasicBlock));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000773
Chris Lattner16c7bb22002-05-09 02:28:59 +0000774 Out << "}\n";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000775 Table.purgeFunction();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000776}
777
778void CWriter::outputBasicBlock(const BasicBlock* BB) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000779 Out << getValueName(BB) << ":\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000780
781 // Output all of the instructions in the basic block...
782 // print the basic blocks
783 CInstPrintVisitor CIPV(*this, Table, Out);
784 CIPV.visit((BasicBlock *) BB);
785}
786
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000787void CWriter::writeOperand(const Value *Operand) {
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000788 if (isa<GlobalVariable>(Operand))
789 Out << "(&"; // Global variables are references as their addresses by llvm
Chris Lattner44408262002-05-09 03:50:42 +0000790
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000791 if (Operand->hasName()) {
Chris Lattner2f499022002-05-09 04:21:21 +0000792 Out << getValueName(Operand);
Chris Lattner44408262002-05-09 03:50:42 +0000793 } else if (const Constant *CPV = dyn_cast<const Constant>(Operand)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000794 if (isa<ConstantPointerNull>(CPV))
795 Out << "NULL";
796 else
797 Out << getConstStrValue(CPV);
Chris Lattner44408262002-05-09 03:50:42 +0000798 } else {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000799 int Slot = Table.getValSlot(Operand);
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000800 assert(Slot >= 0 && "Malformed LLVM!");
801 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
Chris Lattner16c7bb22002-05-09 02:28:59 +0000802 }
Chris Lattner44408262002-05-09 03:50:42 +0000803
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000804 if (isa<GlobalVariable>(Operand))
Chris Lattner44408262002-05-09 03:50:42 +0000805 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000806}
807
808
809//===----------------------------------------------------------------------===//
810// External Interface declaration
811//===----------------------------------------------------------------------===//
812
Chris Lattnerf34ee812002-05-09 03:12:34 +0000813void WriteToC(const Module *M, ostream &Out) {
814 assert(M && "You can't write a null module!!");
815 SlotCalculator SlotTable(M, false);
816 CWriter W(Out, SlotTable, M);
817 W.write(M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000818 Out.flush();
819}