blob: 77aef4603ac103e084f78a5ad4b0aeedb74440a7 [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;
Chris Lattner1f02c892002-05-09 20:14:10 +0000179 case Type::FloatTyID: return "float " + NameSoFar;
180 case Type::DoubleTyID: return "double " + NameSoFar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000181 default :
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000182 cerr << "Unknown primitive type: " << Ty << "\n";
183 abort();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000184 }
185
186 // Check to see if the type is named.
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000187 if (!ignoreName) {
188 map<const Type *, string>::iterator I = TypeNames.find(Ty);
189 if (I != TypeNames.end())
190 return I->second + " " + NameSoFar;
191 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000192
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000193 string Result;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000194 switch (Ty->getPrimitiveID()) {
195 case Type::FunctionTyID: {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000196 const FunctionType *MTy = cast<const FunctionType>(Ty);
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000197 Result += calcTypeNameVar(MTy->getReturnType(), TypeNames, "");
198 Result += " " + NameSoFar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000199 Result += " (";
200 for (FunctionType::ParamTypes::const_iterator
201 I = MTy->getParamTypes().begin(),
202 E = MTy->getParamTypes().end(); I != E; ++I) {
203 if (I != MTy->getParamTypes().begin())
204 Result += ", ";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000205 Result += calcTypeNameVar(*I, TypeNames, "");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000206 }
207 if (MTy->isVarArg()) {
208 if (!MTy->getParamTypes().empty())
209 Result += ", ";
210 Result += "...";
211 }
212 Result += ")";
213 break;
214 }
215 case Type::StructTyID: {
216 const StructType *STy = cast<const StructType>(Ty);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000217 Result = NameSoFar + " {\n";
218 unsigned indx = 0;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000219 for (StructType::ElementTypes::const_iterator
220 I = STy->getElementTypes().begin(),
221 E = STy->getElementTypes().end(); I != E; ++I) {
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000222 Result += " " +calcTypeNameVar(*I, TypeNames, "field" + utostr(indx++));
223 Result += ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000224 }
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000225 Result += "}";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000226 break;
227 }
228
229 case Type::PointerTyID: {
230 Result = calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(),
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000231 TypeNames, "*" + NameSoFar);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000232 break;
233 }
234
235 case Type::ArrayTyID: {
236 const ArrayType *ATy = cast<const ArrayType>(Ty);
237 int NumElements = ATy->getNumElements();
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000238 Result = calcTypeNameVar(ATy->getElementType(), TypeNames,
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000239 NameSoFar + "[" + itostr(NumElements) + "]");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000240 break;
241 }
242 default:
243 assert(0 && "Unhandled case in getTypeProps!");
244 Result = "<error>";
245 }
246
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000247 return Result;
248}
249
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000250namespace {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000251 class CWriter {
252 ostream& Out;
253 SlotCalculator &Table;
254 const Module *TheModule;
255 map<const Type *, string> TypeNames;
256 public:
257 inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M)
258 : Out(o), Table(Tab), TheModule(M) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000259 }
260
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000261 inline void write(Module *M) { printModule(M); }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000262
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000263 ostream& printTypeVar(const Type *Ty, const string &VariableName) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000264 return Out << calcTypeNameVar(Ty, TypeNames, VariableName);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000265 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000266
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000267 ostream& printType(const Type *Ty) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000268 return Out << calcTypeNameVar(Ty, TypeNames, "");
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000269 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000270
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000271 void writeOperand(const Value *Operand);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000272
273 string getValueName(const Value *V);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000274 private :
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000275
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000276 void printModule(Module *M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000277 void printSymbolTable(const SymbolTable &ST);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000278 void printGlobal(const GlobalVariable *GV);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000279 void printFunctionSignature(const Function *F);
280 void printFunctionDecl(const Function *F); // Print just the forward decl
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000281
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000282 void printFunction(Function *);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000283 };
284 /* END class CWriter */
285
286
287 /* CLASS InstLocalVarsVisitor */
288 class InstLocalVarsVisitor : public InstVisitor<InstLocalVarsVisitor> {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000289 CWriter& CW;
Chris Lattner16c7bb22002-05-09 02:28:59 +0000290 void handleTerminator(TerminatorInst *tI, int indx);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000291 public:
292 CLocalVars CLV;
293
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000294 InstLocalVarsVisitor(CWriter &cw) : CW(cw) {}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000295
296 void visitInstruction(Instruction *I) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000297 if (I->getType() != Type::VoidTy)
298 CLV.addLocalVar(I->getType(), CW.getValueName(I));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000299 }
300
301 void visitBranchInst(BranchInst *I) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000302 handleTerminator(I, 0);
303 if (I->isConditional())
304 handleTerminator(I, 1);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000305 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000306 };
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000307}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000308
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000309void InstLocalVarsVisitor::handleTerminator(TerminatorInst *tI,int indx) {
310 BasicBlock *bb = tI->getSuccessor(indx);
311
312 BasicBlock::const_iterator insIt = bb->begin();
313 while (insIt != bb->end()) {
314 if (const PHINode *pI = dyn_cast<PHINode>(*insIt)) {
315 // Its a phinode!
316 // Calculate the incoming index for this
317 assert(pI->getBasicBlockIndex(tI->getParent()) != -1);
318
319 CLV.addLocalVar(pI->getType(), CW.getValueName(pI));
320 } else
321 break;
322 insIt++;
323 }
324}
325
326namespace {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000327 /* CLASS CInstPrintVisitor */
328
329 class CInstPrintVisitor: public InstVisitor<CInstPrintVisitor> {
330 CWriter& CW;
331 SlotCalculator& Table;
332 ostream &Out;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000333
334 void outputLValue(Instruction *);
Chris Lattner1f02c892002-05-09 20:14:10 +0000335 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
336 unsigned Indent);
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
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000367// Implement all "other" instructions, except for PHINode
368void CInstPrintVisitor::visitCastInst(CastInst *I) {
369 outputLValue(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000370 Out << "(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000371 CW.printType(I->getType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000372 Out << ")";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000373 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000374 Out << ";\n";
375}
376
377void CInstPrintVisitor::visitCallInst(CallInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000378 if (I->getType() != Type::VoidTy)
379 outputLValue(I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000380 else
381 Out << " ";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000382
Chris Lattner2f499022002-05-09 04:21:21 +0000383 const PointerType *PTy = cast<PointerType>(I->getCalledValue()->getType());
384 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
385 const Type *RetTy = FTy->getReturnType();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000386
Chris Lattner2f499022002-05-09 04:21:21 +0000387 Out << CW.getValueName(I->getOperand(0)) << "(";
388
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000389 if (I->getNumOperands() > 1) {
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000390 CW.writeOperand(I->getOperand(1));
Chris Lattner2f499022002-05-09 04:21:21 +0000391
392 for (unsigned op = 2, Eop = I->getNumOperands(); op != Eop; ++op) {
393 Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000394 CW.writeOperand(I->getOperand(op));
Chris Lattner2f499022002-05-09 04:21:21 +0000395 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000396 }
Chris Lattner2f499022002-05-09 04:21:21 +0000397 Out << ");\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000398}
399
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000400// Specific Instruction type classes... note that all of the casts are
401// neccesary because we use the instruction classes as opaque types...
402//
403void CInstPrintVisitor::visitReturnInst(ReturnInst *I) {
Chris Lattner1f02c892002-05-09 20:14:10 +0000404 // Don't output a void return if this is the last basic block in the function
405 if (I->getNumOperands() == 0 &&
406 *(I->getParent()->getParent()->end()-1) == I->getParent())
407 return;
408
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000409 Out << " return";
410 if (I->getNumOperands()) {
411 Out << " ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000412 CW.writeOperand(I->getOperand(0));
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000413 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000414 Out << ";\n";
415}
416
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000417// Return true if BB1 immediately preceeds BB2.
418static bool BBFollowsBB(BasicBlock *BB1, BasicBlock *BB2) {
419 Function *F = BB1->getParent();
420 Function::iterator I = find(F->begin(), F->end(), BB1);
421 assert(I != F->end() && "BB not in function!");
422 return *(I+1) == BB2;
423}
424
Chris Lattner1f02c892002-05-09 20:14:10 +0000425static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
426 // If PHI nodes need copies, we need the copy code...
427 if (isa<PHINode>(To->front()) ||
428 !BBFollowsBB(From, To)) // Not directly successor, need goto
429 return true;
430
431 // Otherwise we don't need the code.
432 return false;
433}
434
435void CInstPrintVisitor::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
436 unsigned Indent) {
437 for (BasicBlock::iterator I = Succ->begin();
438 PHINode *PN = dyn_cast<PHINode>(*I); ++I) {
439 // now we have to do the printing
440 Out << string(Indent, ' ');
441 outputLValue(PN);
442 CW.writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
443 Out << "; /* for PHI node */\n";
444 }
445
446 if (!BBFollowsBB(CurBB, Succ)) {
447 Out << string(Indent, ' ') << " goto ";
448 CW.writeOperand(Succ);
449 Out << ";\n";
450 }
451}
452
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000453// Brach instruction printing - Avoid printing out a brach to a basic block that
454// immediately succeeds the current one.
455//
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000456void CInstPrintVisitor::visitBranchInst(BranchInst *I) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000457 if (I->isConditional()) {
Chris Lattner1f02c892002-05-09 20:14:10 +0000458 if (isGotoCodeNeccessary(I->getParent(), I->getSuccessor(0))) {
459 Out << " if (";
460 CW.writeOperand(I->getCondition());
461 Out << ") {\n";
462
463 printBranchToBlock(I->getParent(), I->getSuccessor(0), 2);
464
465 if (isGotoCodeNeccessary(I->getParent(), I->getSuccessor(1))) {
466 Out << " } else {\n";
467 printBranchToBlock(I->getParent(), I->getSuccessor(1), 2);
468 }
469 } else {
470 // First goto not neccesary, assume second one is...
471 Out << " if (!";
472 CW.writeOperand(I->getCondition());
473 Out << ") {\n";
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000474
Chris Lattner1f02c892002-05-09 20:14:10 +0000475 printBranchToBlock(I->getParent(), I->getSuccessor(1), 2);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000476 }
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000477
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000478 Out << " }\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000479 } else {
Chris Lattner1f02c892002-05-09 20:14:10 +0000480 printBranchToBlock(I->getParent(), I->getSuccessor(0), 0);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000481 }
482 Out << "\n";
483}
484
485void CInstPrintVisitor::visitSwitchInst(SwitchInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000486 assert(0 && "Switch not implemented!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000487}
488
489void CInstPrintVisitor::visitInvokeInst(InvokeInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000490 assert(0 && "Invoke not implemented!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000491}
492
493void CInstPrintVisitor::visitMallocInst(MallocInst *I) {
494 outputLValue(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000495 Out << "(";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000496 CW.printType(I->getType());
497 Out << ")malloc(sizeof(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000498 CW.printType(I->getType()->getElementType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000499 Out << ")";
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000500
501 if (I->isArrayAllocation()) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000502 Out << " * " ;
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000503 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000504 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000505 Out << ");\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000506}
507
508void CInstPrintVisitor::visitAllocaInst(AllocaInst *I) {
509 outputLValue(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000510 Out << "(";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000511 CW.printType(I->getType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000512 Out << ") alloca(sizeof(";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000513 CW.printType(I->getType()->getElementType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000514 Out << ")";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000515 if (I->isArrayAllocation()) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000516 Out << " * " ;
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000517 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000518 }
519 Out << ");\n";
520}
521
Chris Lattner1f02c892002-05-09 20:14:10 +0000522void CInstPrintVisitor::visitFreeInst(FreeInst *I) {
523 Out << " free(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000524 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000525 Out << ");\n";
526}
527
Chris Lattner44408262002-05-09 03:50:42 +0000528void CInstPrintVisitor::printIndexingExpr(MemAccessInst *MAI) {
Chris Lattner1f02c892002-05-09 20:14:10 +0000529 MemAccessInst::op_iterator I = MAI->idx_begin(), E = MAI->idx_end();
530 if (I == E)
531 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
532
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000533 CW.writeOperand(MAI->getPointerOperand());
Chris Lattner44408262002-05-09 03:50:42 +0000534
Chris Lattner8c8a3702002-05-09 15:59:50 +0000535 if (I == E) return;
536
537 // Print out the -> operator if possible...
538 Constant *CI = dyn_cast<Constant>(*I);
539 if (CI && CI->isNullValue() && I+1 != E &&
540 (*(I+1))->getType() == Type::UByteTy) {
541 ++I;
542 Out << "->field" << cast<ConstantUInt>(*I)->getValue();
543 ++I;
544 }
545
546 for (; I != E; ++I)
Chris Lattner44408262002-05-09 03:50:42 +0000547 if ((*I)->getType() == Type::UIntTy) {
548 Out << "[";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000549 CW.writeOperand(*I);
Chris Lattner44408262002-05-09 03:50:42 +0000550 Out << "]";
551 } else {
552 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000553 }
Chris Lattner44408262002-05-09 03:50:42 +0000554}
555
556void CInstPrintVisitor::visitLoadInst(LoadInst *I) {
557 outputLValue(I);
558 printIndexingExpr(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000559 Out << ";\n";
560}
561
Chris Lattner44408262002-05-09 03:50:42 +0000562void CInstPrintVisitor::visitStoreInst(StoreInst *I) {
563 Out << " ";
564 printIndexingExpr(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000565 Out << " = ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000566 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000567 Out << ";\n";
568}
569
570void CInstPrintVisitor::visitGetElementPtrInst(GetElementPtrInst *I) {
571 outputLValue(I);
Chris Lattner44408262002-05-09 03:50:42 +0000572 Out << "&";
573 printIndexingExpr(I);
574 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000575}
576
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000577void CInstPrintVisitor::visitNot(GenericUnaryInst *I) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000578 outputLValue(I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000579 Out << "~";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000580 CW.writeOperand(I->getOperand(0));
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000581 Out << ";\n";
582}
583
584void CInstPrintVisitor::visitBinaryOperator(Instruction *I) {
585 // binary instructions, shift instructions, setCond instructions.
586 outputLValue(I);
587 if (isa<PointerType>(I->getType())) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000588 Out << "(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000589 CW.printType(I->getType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000590 Out << ")";
591 }
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000592
593 if (isa<PointerType>(I->getType())) Out << "(long long)";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000594 CW.writeOperand(I->getOperand(0));
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000595
596 switch (I->getOpcode()) {
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000597 case Instruction::Add: Out << " + "; break;
598 case Instruction::Sub: Out << " - "; break;
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000599 case Instruction::Mul: Out << "*"; break;
600 case Instruction::Div: Out << "/"; break;
601 case Instruction::Rem: Out << "%"; break;
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000602 case Instruction::And: Out << " & "; break;
603 case Instruction::Or: Out << " | "; break;
604 case Instruction::Xor: Out << " ^ "; break;
605 case Instruction::SetEQ: Out << " == "; break;
606 case Instruction::SetNE: Out << " != "; break;
607 case Instruction::SetLE: Out << " <= "; break;
608 case Instruction::SetGE: Out << " >= "; break;
609 case Instruction::SetLT: Out << " < "; break;
610 case Instruction::SetGT: Out << " > "; break;
611 case Instruction::Shl : Out << " << "; break;
612 case Instruction::Shr : Out << " >> "; break;
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000613 default: cerr << "Invalid operator type!" << I; abort();
614 }
615
616 if (isa<PointerType>(I->getType())) Out << "(long long)";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000617 CW.writeOperand(I->getOperand(1));
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000618 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000619}
620
621/* END : CInstPrintVisitor implementation */
622
Chris Lattner2f499022002-05-09 04:21:21 +0000623// We dont want identifier names with ., space, - in them.
624// So we replace them with _
625static string makeNameProper(string x) {
626 string tmp;
627 for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
628 switch (*sI) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000629 case '.': tmp += "d_"; break;
630 case ' ': tmp += "s_"; break;
631 case '-': tmp += "D_"; break;
Chris Lattner2f499022002-05-09 04:21:21 +0000632 case '_': tmp += "__"; break;
633 default: tmp += *sI;
634 }
635
636 return tmp;
637}
638
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000639string CWriter::getValueName(const Value *V) {
Chris Lattner2f499022002-05-09 04:21:21 +0000640 if (V->hasName()) { // Print out the label if it exists...
641 if (isa<GlobalValue>(V)) // Do not mangle globals...
642 return makeNameProper(V->getName());
643
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000644 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
645 makeNameProper(V->getName());
Chris Lattner2f499022002-05-09 04:21:21 +0000646 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000647
648 int Slot = Table.getValSlot(V);
649 assert(Slot >= 0 && "Invalid value!");
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000650 return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000651}
652
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000653void CWriter::printModule(Module *M) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000654 // printing stdlib inclusion
655 // Out << "#include <stdlib.h>\n";
656
Chris Lattner16c7bb22002-05-09 02:28:59 +0000657 // get declaration for alloca
658 Out << "/* Provide Declarations */\n"
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000659 << "#include <alloca.h>\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000660
661 // Provide a definition for null if one does not already exist.
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000662 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000663 << "typedef unsigned char bool;\n"
664
665 << "\n\n/* Global Symbols */\n";
666
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000667 // Loop over the symbol table, emitting all named constants...
668 if (M->hasSymbolTable())
669 printSymbolTable(*M->getSymbolTable());
670
Chris Lattner16c7bb22002-05-09 02:28:59 +0000671 Out << "\n\n/* Global Data */\n";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000672 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
673 GlobalVariable *GV = *I;
674 if (GV->hasInternalLinkage()) Out << "static ";
675 printTypeVar(GV->getType()->getElementType(), getValueName(GV));
676
677 if (GV->hasInitializer()) {
678 Out << " = " ;
679 writeOperand(GV->getInitializer());
680 }
681 Out << ";\n";
682 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000683
Chris Lattner16c7bb22002-05-09 02:28:59 +0000684 // First output all the declarations of the functions as C requires Functions
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000685 // be declared before they are used.
Chris Lattner16c7bb22002-05-09 02:28:59 +0000686 //
687 Out << "\n\n/* Function Declarations */\n";
688 for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunctionDecl));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000689
Chris Lattner16c7bb22002-05-09 02:28:59 +0000690 // Output all of the functions...
691 Out << "\n\n/* Function Bodies */\n";
692 for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunction));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000693}
694
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000695
696// printSymbolTable - Run through symbol table looking for named constants
697// if a named constant is found, emit it's declaration...
698// Assuming that symbol table has only types and constants.
699void CWriter::printSymbolTable(const SymbolTable &ST) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000700 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
701 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
702 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
703
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000704 for (; I != End; ++I)
705 if (const Type *Ty = dyn_cast<const StructType>(I->second)) {
706 string Name = "struct l_" + I->first;
707 Out << Name << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000708
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000709 TypeNames.insert(std::make_pair(Ty, Name));
710 }
711 }
712
713 Out << "\n";
714
715 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
716 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
717 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
718
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000719 for (; I != End; ++I) {
720 const Value *V = I->second;
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000721 if (const Type *Ty = dyn_cast<const Type>(V)) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000722 string Name = "l_" + I->first;
Chris Lattner1f02c892002-05-09 20:14:10 +0000723 if (isa<StructType>(Ty))
724 Name = "struct " + Name;
725 else
726 Out << "typedef ";
727
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000728 Out << calcTypeNameVar(Ty, TypeNames, Name, true) << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000729 }
730 }
731 }
732}
733
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000734
Chris Lattner16c7bb22002-05-09 02:28:59 +0000735// printFunctionDecl - Print function declaration
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000736//
Chris Lattner16c7bb22002-05-09 02:28:59 +0000737void CWriter::printFunctionDecl(const Function *F) {
738 printFunctionSignature(F);
739 Out << ";\n";
740}
741
742void CWriter::printFunctionSignature(const Function *F) {
743 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000744
745 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000746 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000747
Chris Lattner16c7bb22002-05-09 02:28:59 +0000748 // Print out the return type and name...
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000749 printType(F->getReturnType());
Chris Lattner1f02c892002-05-09 20:14:10 +0000750 Out << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000751
Chris Lattner16c7bb22002-05-09 02:28:59 +0000752 if (!F->isExternal()) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000753 if (!F->getArgumentList().empty()) {
754 printTypeVar(F->getArgumentList().front()->getType(),
755 getValueName(F->getArgumentList().front()));
756
757 for (Function::ArgumentListType::const_iterator
758 I = F->getArgumentList().begin()+1,
759 E = F->getArgumentList().end(); I != E; ++I) {
760 Out << ", ";
761 printTypeVar((*I)->getType(), getValueName(*I));
762 }
763 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000764 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000765 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000766 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000767 FT->getParamTypes().begin(),
768 E = FT->getParamTypes().end(); I != E; ++I) {
769 if (I != FT->getParamTypes().begin()) Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000770 printType(*I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000771 }
772 }
773
774 // Finish printing arguments...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000775 if (FT->isVarArg()) {
776 if (FT->getParamTypes().size()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000777 Out << "..."; // Output varargs portion of signature!
778 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000779 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000780}
781
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000782
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000783void CWriter::printFunction(Function *F) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000784 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000785
Chris Lattnerf34ee812002-05-09 03:12:34 +0000786 Table.incorporateFunction(F);
787
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000788 printFunctionSignature(F);
789 Out << " {\n";
790
Chris Lattner16c7bb22002-05-09 02:28:59 +0000791 // Process each of the basic blocks, gather information and call the
792 // output methods on the CLocalVars and Function* objects.
793
794 // gather local variable information for each basic block
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000795 InstLocalVarsVisitor ILV(*this);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000796 ILV.visit(F);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000797
Chris Lattner16c7bb22002-05-09 02:28:59 +0000798 // print the local variables
799 // we assume that every local variable is alloca'ed in the C code.
800 std::map<const Type*, VarListType> &locals = ILV.CLV.LocalVars;
801
802 map<const Type*, VarListType>::iterator iter;
803 for (iter = locals.begin(); iter != locals.end(); ++iter) {
804 VarListType::iterator listiter;
805 for (listiter = iter->second.begin(); listiter != iter->second.end();
806 ++listiter) {
807 Out << " ";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000808 printTypeVar(iter->first, *listiter);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000809 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000810 }
811 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000812
813 // print the basic blocks
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000814 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
815 BasicBlock *BB = *I, *Prev = I != F->begin() ? *(I-1) : 0;
816
817 // Don't print the label for the basic block if there are no uses, or if the
818 // only terminator use is the precessor basic block's terminator. We have
819 // to scan the use list because PHI nodes use basic blocks too but do not
820 // require a label to be generated.
821 //
822 bool NeedsLabel = false;
823 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
824 UI != UE; ++UI)
825 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
826 if (TI != Prev->getTerminator()) {
827 NeedsLabel = true;
828 break;
829 }
830
831 if (NeedsLabel) Out << getValueName(BB) << ":\n";
832
833 // Output all of the instructions in the basic block...
834 // print the basic blocks
835 CInstPrintVisitor CIPV(*this, Table, Out);
836 CIPV.visit(BB);
837 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000838
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000839 Out << "}\n\n";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000840 Table.purgeFunction();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000841}
842
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000843void CWriter::writeOperand(const Value *Operand) {
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000844 if (isa<GlobalVariable>(Operand))
845 Out << "(&"; // Global variables are references as their addresses by llvm
Chris Lattner44408262002-05-09 03:50:42 +0000846
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000847 if (Operand->hasName()) {
Chris Lattner2f499022002-05-09 04:21:21 +0000848 Out << getValueName(Operand);
Chris Lattner44408262002-05-09 03:50:42 +0000849 } else if (const Constant *CPV = dyn_cast<const Constant>(Operand)) {
Chris Lattner1f02c892002-05-09 20:14:10 +0000850 if (isa<ConstantPointerNull>(CPV)) {
851 Out << "((";
852 printTypeVar(CPV->getType(), "");
853 Out << ")NULL)";
854 } else
Chris Lattner16c7bb22002-05-09 02:28:59 +0000855 Out << getConstStrValue(CPV);
Chris Lattner44408262002-05-09 03:50:42 +0000856 } else {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000857 int Slot = Table.getValSlot(Operand);
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000858 assert(Slot >= 0 && "Malformed LLVM!");
859 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
Chris Lattner16c7bb22002-05-09 02:28:59 +0000860 }
Chris Lattner44408262002-05-09 03:50:42 +0000861
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000862 if (isa<GlobalVariable>(Operand))
Chris Lattner44408262002-05-09 03:50:42 +0000863 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000864}
865
866
867//===----------------------------------------------------------------------===//
868// External Interface declaration
869//===----------------------------------------------------------------------===//
870
Chris Lattnerf34ee812002-05-09 03:12:34 +0000871void WriteToC(const Module *M, ostream &Out) {
872 assert(M && "You can't write a null module!!");
873 SlotCalculator SlotTable(M, false);
874 CWriter W(Out, SlotTable, M);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000875 W.write((Module*)M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000876 Out.flush();
877}