blob: a36f2aeb4424303cfb767e6fc9d887ec6428f153 [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"
Chris Lattner7683a122002-05-09 20:33:35 +000027#include "llvm/Support/InstIterator.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000028#include "Support/StringExtras.h"
29#include "Support/STLExtras.h"
30
31#include <algorithm>
32#include <strstream>
33using std::string;
34using std::map;
35using std::vector;
36using std::ostream;
37
Chris Lattner16c7bb22002-05-09 02:28:59 +000038//===-----------------------------------------------------------------------==//
39//
40// Implementation of the CLocalVars methods
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000041
42// Appends a variable to the LocalVars map if it does not already exist
43// Also check that the type exists on the map.
Chris Lattner7683a122002-05-09 20:33:35 +000044void CLocalVars::addLocalVar(const Type *t, const string &V) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000045 if (!LocalVars.count(t) ||
Chris Lattner7683a122002-05-09 20:33:35 +000046 find(LocalVars[t].begin(), LocalVars[t].end(), V) == LocalVars[t].end())
47 LocalVars[t].push_back(V);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000048}
49
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000050static std::string getConstStrValue(const Constant* CPV);
51
52
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000053static std::string getConstArrayStrValue(const Constant* CPV) {
54 std::string Result;
55
56 // As a special case, print the array as a string if it is an array of
57 // ubytes or an array of sbytes with positive values.
58 //
59 const Type *ETy = cast<ArrayType>(CPV->getType())->getElementType();
60 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
61
62 if (ETy == Type::SByteTy) {
63 for (unsigned i = 0; i < CPV->getNumOperands(); ++i)
64 if (ETy == Type::SByteTy &&
65 cast<ConstantSInt>(CPV->getOperand(i))->getValue() < 0) {
66 isString = false;
67 break;
68 }
69 }
Chris Lattner2f5eb4e2002-05-09 03:56:52 +000070 if (isString) {
71 // Make sure the last character is a null char, as automatically added by C
72 if (CPV->getNumOperands() == 0 ||
73 !cast<Constant>(*(CPV->op_end()-1))->isNullValue())
74 isString = false;
75 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000076
77 if (isString) {
78 Result = "\"";
Chris Lattner2f5eb4e2002-05-09 03:56:52 +000079 // Do not include the last character, which we know is null
80 for (unsigned i = 0, e = CPV->getNumOperands()-1; i != e; ++i) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000081 unsigned char C = (ETy == Type::SByteTy) ?
82 (unsigned char)cast<ConstantSInt>(CPV->getOperand(i))->getValue() :
83 (unsigned char)cast<ConstantUInt>(CPV->getOperand(i))->getValue();
84
85 if (isprint(C)) {
86 Result += C;
87 } else {
Chris Lattner2f5eb4e2002-05-09 03:56:52 +000088 switch (C) {
89 case '\n': Result += "\\n"; break;
90 case '\t': Result += "\\t"; break;
91 case '\r': Result += "\\r"; break;
92 case '\v': Result += "\\v"; break;
93 case '\a': Result += "\\a"; break;
94 default:
95 Result += "\\x";
96 Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
97 Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
98 break;
99 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000100 }
101 }
102 Result += "\"";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000103 } else {
104 Result = "{";
105 if (CPV->getNumOperands()) {
106 Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
107 for (unsigned i = 1; i < CPV->getNumOperands(); i++)
108 Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
109 }
110 Result += " }";
111 }
112
113 return Result;
114}
115
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000116static std::string getConstStrValue(const Constant* CPV) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000117 switch (CPV->getType()->getPrimitiveID()) {
118 case Type::BoolTyID:
119 return CPV == ConstantBool::False ? "0" : "1";
120 case Type::SByteTyID:
121 case Type::ShortTyID:
122 case Type::IntTyID:
123 return itostr(cast<ConstantSInt>(CPV)->getValue());
124 case Type::LongTyID:
125 return itostr(cast<ConstantSInt>(CPV)->getValue()) + "ll";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000126
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000127 case Type::UByteTyID:
128 return utostr(cast<ConstantUInt>(CPV)->getValue());
129 case Type::UShortTyID:
130 return utostr(cast<ConstantUInt>(CPV)->getValue());
131 case Type::UIntTyID:
132 return utostr(cast<ConstantUInt>(CPV)->getValue())+"u";
133 case Type::ULongTyID:
134 return utostr(cast<ConstantUInt>(CPV)->getValue())+"ull";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000135
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000136 case Type::FloatTyID:
137 case Type::DoubleTyID:
138 return ftostr(cast<ConstantFP>(CPV)->getValue());
139
140 case Type::ArrayTyID:
141 return getConstArrayStrValue(CPV);
142
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000143 case Type::StructTyID: {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000144 std::string Result = "{";
145 if (CPV->getNumOperands()) {
146 Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
147 for (unsigned i = 1; i < CPV->getNumOperands(); i++)
148 Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000149 }
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000150 return Result + " }";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000151 }
152
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000153 default:
154 cerr << "Unknown constant type: " << CPV << "\n";
155 abort();
156 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000157}
158
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000159// Pass the Type* variable and and the variable name and this prints out the
160// variable declaration.
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000161//
Chris Lattner16c7bb22002-05-09 02:28:59 +0000162static string calcTypeNameVar(const Type *Ty,
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000163 map<const Type *, string> &TypeNames,
164 const string &NameSoFar, bool ignoreName = false){
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000165 if (Ty->isPrimitiveType())
166 switch (Ty->getPrimitiveID()) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000167 case Type::VoidTyID: return "void " + NameSoFar;
168 case Type::BoolTyID: return "bool " + NameSoFar;
169 case Type::UByteTyID: return "unsigned char " + NameSoFar;
170 case Type::SByteTyID: return "signed char " + NameSoFar;
171 case Type::UShortTyID: return "unsigned short " + NameSoFar;
172 case Type::ShortTyID: return "short " + NameSoFar;
173 case Type::UIntTyID: return "unsigned " + NameSoFar;
174 case Type::IntTyID: return "int " + NameSoFar;
175 case Type::ULongTyID: return "unsigned long long " + NameSoFar;
176 case Type::LongTyID: return "signed long long " + NameSoFar;
Chris Lattner1f02c892002-05-09 20:14:10 +0000177 case Type::FloatTyID: return "float " + NameSoFar;
178 case Type::DoubleTyID: return "double " + 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: {
Chris Lattner7683a122002-05-09 20:33:35 +0000194 const FunctionType *MTy = cast<FunctionType>(Ty);
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000195 Result += calcTypeNameVar(MTy->getReturnType(), TypeNames, "");
Chris Lattner7683a122002-05-09 20:33:35 +0000196 Result += " " + NameSoFar + " (";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000197 for (FunctionType::ParamTypes::const_iterator
198 I = MTy->getParamTypes().begin(),
199 E = MTy->getParamTypes().end(); I != E; ++I) {
200 if (I != MTy->getParamTypes().begin())
201 Result += ", ";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000202 Result += calcTypeNameVar(*I, TypeNames, "");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000203 }
204 if (MTy->isVarArg()) {
205 if (!MTy->getParamTypes().empty())
206 Result += ", ";
207 Result += "...";
208 }
209 Result += ")";
210 break;
211 }
212 case Type::StructTyID: {
213 const StructType *STy = cast<const StructType>(Ty);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000214 Result = NameSoFar + " {\n";
215 unsigned indx = 0;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000216 for (StructType::ElementTypes::const_iterator
217 I = STy->getElementTypes().begin(),
218 E = STy->getElementTypes().end(); I != E; ++I) {
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000219 Result += " " +calcTypeNameVar(*I, TypeNames, "field" + utostr(indx++));
220 Result += ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000221 }
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000222 Result += "}";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000223 break;
224 }
225
226 case Type::PointerTyID: {
227 Result = calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(),
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000228 TypeNames, "*" + NameSoFar);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000229 break;
230 }
231
232 case Type::ArrayTyID: {
233 const ArrayType *ATy = cast<const ArrayType>(Ty);
234 int NumElements = ATy->getNumElements();
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000235 Result = calcTypeNameVar(ATy->getElementType(), TypeNames,
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000236 NameSoFar + "[" + itostr(NumElements) + "]");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000237 break;
238 }
239 default:
240 assert(0 && "Unhandled case in getTypeProps!");
241 Result = "<error>";
242 }
243
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000244 return Result;
245}
246
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000247namespace {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000248 class CWriter {
249 ostream& Out;
250 SlotCalculator &Table;
251 const Module *TheModule;
252 map<const Type *, string> TypeNames;
253 public:
254 inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M)
255 : Out(o), Table(Tab), TheModule(M) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000256 }
257
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000258 inline void write(Module *M) { printModule(M); }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000259
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000260 ostream& printTypeVar(const Type *Ty, const string &VariableName) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000261 return Out << calcTypeNameVar(Ty, TypeNames, VariableName);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000262 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000263
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000264 ostream& printType(const Type *Ty) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000265 return Out << calcTypeNameVar(Ty, TypeNames, "");
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000266 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000267
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000268 void writeOperand(const Value *Operand);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000269
270 string getValueName(const Value *V);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000271 private :
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000272
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000273 void printModule(Module *M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000274 void printSymbolTable(const SymbolTable &ST);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000275 void printGlobal(const GlobalVariable *GV);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000276 void printFunctionSignature(const Function *F);
277 void printFunctionDecl(const Function *F); // Print just the forward decl
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000278
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000279 void printFunction(Function *);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000280 };
281 /* END class CWriter */
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000282}
283
284namespace {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000285 /* CLASS CInstPrintVisitor */
286
287 class CInstPrintVisitor: public InstVisitor<CInstPrintVisitor> {
288 CWriter& CW;
289 SlotCalculator& Table;
290 ostream &Out;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000291
292 void outputLValue(Instruction *);
Chris Lattner1f02c892002-05-09 20:14:10 +0000293 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
294 unsigned Indent);
Chris Lattner44408262002-05-09 03:50:42 +0000295 void printIndexingExpr(MemAccessInst *MAI);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000296
297 public:
298 CInstPrintVisitor (CWriter &cw, SlotCalculator& table, ostream& o)
Chris Lattner44408262002-05-09 03:50:42 +0000299 : CW(cw), Table(table), Out(o) {}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000300
301 void visitCastInst(CastInst *I);
302 void visitCallInst(CallInst *I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000303 void visitShiftInst(ShiftInst *I) { visitBinaryOperator(I); }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000304 void visitReturnInst(ReturnInst *I);
305 void visitBranchInst(BranchInst *I);
306 void visitSwitchInst(SwitchInst *I);
307 void visitInvokeInst(InvokeInst *I) ;
308 void visitMallocInst(MallocInst *I);
309 void visitAllocaInst(AllocaInst *I);
310 void visitFreeInst(FreeInst *I);
311 void visitLoadInst(LoadInst *I);
312 void visitStoreInst(StoreInst *I);
313 void visitGetElementPtrInst(GetElementPtrInst *I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000314 void visitPHINode(PHINode *I) {}
315
316 void visitNot(GenericUnaryInst *I);
317 void visitBinaryOperator(Instruction *I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000318 };
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000319}
320
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000321void CInstPrintVisitor::outputLValue(Instruction *I) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000322 Out << " " << CW.getValueName(I) << " = ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000323}
324
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000325// Implement all "other" instructions, except for PHINode
326void CInstPrintVisitor::visitCastInst(CastInst *I) {
327 outputLValue(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000328 Out << "(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000329 CW.printType(I->getType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000330 Out << ")";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000331 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000332 Out << ";\n";
333}
334
335void CInstPrintVisitor::visitCallInst(CallInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000336 if (I->getType() != Type::VoidTy)
337 outputLValue(I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000338 else
339 Out << " ";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000340
Chris Lattner2f499022002-05-09 04:21:21 +0000341 const PointerType *PTy = cast<PointerType>(I->getCalledValue()->getType());
342 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
343 const Type *RetTy = FTy->getReturnType();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000344
Chris Lattner2f499022002-05-09 04:21:21 +0000345 Out << CW.getValueName(I->getOperand(0)) << "(";
346
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000347 if (I->getNumOperands() > 1) {
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000348 CW.writeOperand(I->getOperand(1));
Chris Lattner2f499022002-05-09 04:21:21 +0000349
350 for (unsigned op = 2, Eop = I->getNumOperands(); op != Eop; ++op) {
351 Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000352 CW.writeOperand(I->getOperand(op));
Chris Lattner2f499022002-05-09 04:21:21 +0000353 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000354 }
Chris Lattner2f499022002-05-09 04:21:21 +0000355 Out << ");\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000356}
357
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000358// Specific Instruction type classes... note that all of the casts are
359// neccesary because we use the instruction classes as opaque types...
360//
361void CInstPrintVisitor::visitReturnInst(ReturnInst *I) {
Chris Lattner1f02c892002-05-09 20:14:10 +0000362 // Don't output a void return if this is the last basic block in the function
363 if (I->getNumOperands() == 0 &&
364 *(I->getParent()->getParent()->end()-1) == I->getParent())
365 return;
366
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000367 Out << " return";
368 if (I->getNumOperands()) {
369 Out << " ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000370 CW.writeOperand(I->getOperand(0));
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000371 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000372 Out << ";\n";
373}
374
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000375// Return true if BB1 immediately preceeds BB2.
376static bool BBFollowsBB(BasicBlock *BB1, BasicBlock *BB2) {
377 Function *F = BB1->getParent();
378 Function::iterator I = find(F->begin(), F->end(), BB1);
379 assert(I != F->end() && "BB not in function!");
380 return *(I+1) == BB2;
381}
382
Chris Lattner1f02c892002-05-09 20:14:10 +0000383static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
384 // If PHI nodes need copies, we need the copy code...
385 if (isa<PHINode>(To->front()) ||
386 !BBFollowsBB(From, To)) // Not directly successor, need goto
387 return true;
388
389 // Otherwise we don't need the code.
390 return false;
391}
392
393void CInstPrintVisitor::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
394 unsigned Indent) {
395 for (BasicBlock::iterator I = Succ->begin();
396 PHINode *PN = dyn_cast<PHINode>(*I); ++I) {
397 // now we have to do the printing
398 Out << string(Indent, ' ');
399 outputLValue(PN);
400 CW.writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
401 Out << "; /* for PHI node */\n";
402 }
403
404 if (!BBFollowsBB(CurBB, Succ)) {
405 Out << string(Indent, ' ') << " goto ";
406 CW.writeOperand(Succ);
407 Out << ";\n";
408 }
409}
410
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000411// Brach instruction printing - Avoid printing out a brach to a basic block that
412// immediately succeeds the current one.
413//
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000414void CInstPrintVisitor::visitBranchInst(BranchInst *I) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000415 if (I->isConditional()) {
Chris Lattner1f02c892002-05-09 20:14:10 +0000416 if (isGotoCodeNeccessary(I->getParent(), I->getSuccessor(0))) {
417 Out << " if (";
418 CW.writeOperand(I->getCondition());
419 Out << ") {\n";
420
421 printBranchToBlock(I->getParent(), I->getSuccessor(0), 2);
422
423 if (isGotoCodeNeccessary(I->getParent(), I->getSuccessor(1))) {
424 Out << " } else {\n";
425 printBranchToBlock(I->getParent(), I->getSuccessor(1), 2);
426 }
427 } else {
428 // First goto not neccesary, assume second one is...
429 Out << " if (!";
430 CW.writeOperand(I->getCondition());
431 Out << ") {\n";
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000432
Chris Lattner1f02c892002-05-09 20:14:10 +0000433 printBranchToBlock(I->getParent(), I->getSuccessor(1), 2);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000434 }
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000435
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000436 Out << " }\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000437 } else {
Chris Lattner1f02c892002-05-09 20:14:10 +0000438 printBranchToBlock(I->getParent(), I->getSuccessor(0), 0);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000439 }
440 Out << "\n";
441}
442
443void CInstPrintVisitor::visitSwitchInst(SwitchInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000444 assert(0 && "Switch not implemented!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000445}
446
447void CInstPrintVisitor::visitInvokeInst(InvokeInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000448 assert(0 && "Invoke not implemented!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000449}
450
451void CInstPrintVisitor::visitMallocInst(MallocInst *I) {
452 outputLValue(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000453 Out << "(";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000454 CW.printType(I->getType());
455 Out << ")malloc(sizeof(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000456 CW.printType(I->getType()->getElementType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000457 Out << ")";
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000458
459 if (I->isArrayAllocation()) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000460 Out << " * " ;
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000461 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000462 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000463 Out << ");\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000464}
465
466void CInstPrintVisitor::visitAllocaInst(AllocaInst *I) {
467 outputLValue(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000468 Out << "(";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000469 CW.printType(I->getType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000470 Out << ") alloca(sizeof(";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000471 CW.printType(I->getType()->getElementType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000472 Out << ")";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000473 if (I->isArrayAllocation()) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000474 Out << " * " ;
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000475 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000476 }
477 Out << ");\n";
478}
479
Chris Lattner1f02c892002-05-09 20:14:10 +0000480void CInstPrintVisitor::visitFreeInst(FreeInst *I) {
481 Out << " free(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000482 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000483 Out << ");\n";
484}
485
Chris Lattner44408262002-05-09 03:50:42 +0000486void CInstPrintVisitor::printIndexingExpr(MemAccessInst *MAI) {
Chris Lattner1f02c892002-05-09 20:14:10 +0000487 MemAccessInst::op_iterator I = MAI->idx_begin(), E = MAI->idx_end();
488 if (I == E)
489 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
490
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000491 CW.writeOperand(MAI->getPointerOperand());
Chris Lattner44408262002-05-09 03:50:42 +0000492
Chris Lattner8c8a3702002-05-09 15:59:50 +0000493 if (I == E) return;
494
495 // Print out the -> operator if possible...
496 Constant *CI = dyn_cast<Constant>(*I);
497 if (CI && CI->isNullValue() && I+1 != E &&
498 (*(I+1))->getType() == Type::UByteTy) {
499 ++I;
500 Out << "->field" << cast<ConstantUInt>(*I)->getValue();
501 ++I;
502 }
503
504 for (; I != E; ++I)
Chris Lattner44408262002-05-09 03:50:42 +0000505 if ((*I)->getType() == Type::UIntTy) {
506 Out << "[";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000507 CW.writeOperand(*I);
Chris Lattner44408262002-05-09 03:50:42 +0000508 Out << "]";
509 } else {
510 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000511 }
Chris Lattner44408262002-05-09 03:50:42 +0000512}
513
514void CInstPrintVisitor::visitLoadInst(LoadInst *I) {
515 outputLValue(I);
516 printIndexingExpr(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000517 Out << ";\n";
518}
519
Chris Lattner44408262002-05-09 03:50:42 +0000520void CInstPrintVisitor::visitStoreInst(StoreInst *I) {
521 Out << " ";
522 printIndexingExpr(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000523 Out << " = ";
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
528void CInstPrintVisitor::visitGetElementPtrInst(GetElementPtrInst *I) {
529 outputLValue(I);
Chris Lattner44408262002-05-09 03:50:42 +0000530 Out << "&";
531 printIndexingExpr(I);
532 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000533}
534
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000535void CInstPrintVisitor::visitNot(GenericUnaryInst *I) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000536 outputLValue(I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000537 Out << "~";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000538 CW.writeOperand(I->getOperand(0));
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000539 Out << ";\n";
540}
541
542void CInstPrintVisitor::visitBinaryOperator(Instruction *I) {
543 // binary instructions, shift instructions, setCond instructions.
544 outputLValue(I);
545 if (isa<PointerType>(I->getType())) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000546 Out << "(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000547 CW.printType(I->getType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000548 Out << ")";
549 }
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000550
551 if (isa<PointerType>(I->getType())) Out << "(long long)";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000552 CW.writeOperand(I->getOperand(0));
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000553
554 switch (I->getOpcode()) {
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000555 case Instruction::Add: Out << " + "; break;
556 case Instruction::Sub: Out << " - "; break;
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000557 case Instruction::Mul: Out << "*"; break;
558 case Instruction::Div: Out << "/"; break;
559 case Instruction::Rem: Out << "%"; break;
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000560 case Instruction::And: Out << " & "; break;
561 case Instruction::Or: Out << " | "; break;
562 case Instruction::Xor: Out << " ^ "; break;
563 case Instruction::SetEQ: Out << " == "; break;
564 case Instruction::SetNE: Out << " != "; break;
565 case Instruction::SetLE: Out << " <= "; break;
566 case Instruction::SetGE: Out << " >= "; break;
567 case Instruction::SetLT: Out << " < "; break;
568 case Instruction::SetGT: Out << " > "; break;
569 case Instruction::Shl : Out << " << "; break;
570 case Instruction::Shr : Out << " >> "; break;
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000571 default: cerr << "Invalid operator type!" << I; abort();
572 }
573
574 if (isa<PointerType>(I->getType())) Out << "(long long)";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000575 CW.writeOperand(I->getOperand(1));
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000576 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000577}
578
579/* END : CInstPrintVisitor implementation */
580
Chris Lattner2f499022002-05-09 04:21:21 +0000581// We dont want identifier names with ., space, - in them.
582// So we replace them with _
583static string makeNameProper(string x) {
584 string tmp;
585 for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
586 switch (*sI) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000587 case '.': tmp += "d_"; break;
588 case ' ': tmp += "s_"; break;
589 case '-': tmp += "D_"; break;
Chris Lattner2f499022002-05-09 04:21:21 +0000590 case '_': tmp += "__"; break;
591 default: tmp += *sI;
592 }
593
594 return tmp;
595}
596
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000597string CWriter::getValueName(const Value *V) {
Chris Lattner2f499022002-05-09 04:21:21 +0000598 if (V->hasName()) { // Print out the label if it exists...
599 if (isa<GlobalValue>(V)) // Do not mangle globals...
600 return makeNameProper(V->getName());
601
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000602 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
603 makeNameProper(V->getName());
Chris Lattner2f499022002-05-09 04:21:21 +0000604 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000605
606 int Slot = Table.getValSlot(V);
607 assert(Slot >= 0 && "Invalid value!");
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000608 return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000609}
610
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000611void CWriter::printModule(Module *M) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000612 // printing stdlib inclusion
613 // Out << "#include <stdlib.h>\n";
614
Chris Lattner16c7bb22002-05-09 02:28:59 +0000615 // get declaration for alloca
616 Out << "/* Provide Declarations */\n"
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000617 << "#include <alloca.h>\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000618
619 // Provide a definition for null if one does not already exist.
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000620 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000621 << "typedef unsigned char bool;\n"
622
623 << "\n\n/* Global Symbols */\n";
624
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000625 // Loop over the symbol table, emitting all named constants...
626 if (M->hasSymbolTable())
627 printSymbolTable(*M->getSymbolTable());
628
Chris Lattner16c7bb22002-05-09 02:28:59 +0000629 Out << "\n\n/* Global Data */\n";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000630 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
631 GlobalVariable *GV = *I;
632 if (GV->hasInternalLinkage()) Out << "static ";
633 printTypeVar(GV->getType()->getElementType(), getValueName(GV));
634
635 if (GV->hasInitializer()) {
636 Out << " = " ;
637 writeOperand(GV->getInitializer());
638 }
639 Out << ";\n";
640 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000641
Chris Lattner16c7bb22002-05-09 02:28:59 +0000642 // First output all the declarations of the functions as C requires Functions
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000643 // be declared before they are used.
Chris Lattner16c7bb22002-05-09 02:28:59 +0000644 //
645 Out << "\n\n/* Function Declarations */\n";
646 for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunctionDecl));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000647
Chris Lattner16c7bb22002-05-09 02:28:59 +0000648 // Output all of the functions...
649 Out << "\n\n/* Function Bodies */\n";
650 for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunction));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000651}
652
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000653
654// printSymbolTable - Run through symbol table looking for named constants
655// if a named constant is found, emit it's declaration...
656// Assuming that symbol table has only types and constants.
657void CWriter::printSymbolTable(const SymbolTable &ST) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000658 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
659 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
660 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
661
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000662 for (; I != End; ++I)
663 if (const Type *Ty = dyn_cast<const StructType>(I->second)) {
664 string Name = "struct l_" + I->first;
665 Out << Name << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000666
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000667 TypeNames.insert(std::make_pair(Ty, Name));
668 }
669 }
670
671 Out << "\n";
672
673 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
674 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
675 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
676
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000677 for (; I != End; ++I) {
678 const Value *V = I->second;
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000679 if (const Type *Ty = dyn_cast<const Type>(V)) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000680 string Name = "l_" + I->first;
Chris Lattner1f02c892002-05-09 20:14:10 +0000681 if (isa<StructType>(Ty))
682 Name = "struct " + Name;
683 else
684 Out << "typedef ";
685
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000686 Out << calcTypeNameVar(Ty, TypeNames, Name, true) << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000687 }
688 }
689 }
690}
691
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000692
Chris Lattner16c7bb22002-05-09 02:28:59 +0000693// printFunctionDecl - Print function declaration
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000694//
Chris Lattner16c7bb22002-05-09 02:28:59 +0000695void CWriter::printFunctionDecl(const Function *F) {
696 printFunctionSignature(F);
697 Out << ";\n";
698}
699
700void CWriter::printFunctionSignature(const Function *F) {
701 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000702
703 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000704 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000705
Chris Lattner16c7bb22002-05-09 02:28:59 +0000706 // Print out the return type and name...
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000707 printType(F->getReturnType());
Chris Lattner1f02c892002-05-09 20:14:10 +0000708 Out << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000709
Chris Lattner16c7bb22002-05-09 02:28:59 +0000710 if (!F->isExternal()) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000711 if (!F->getArgumentList().empty()) {
712 printTypeVar(F->getArgumentList().front()->getType(),
713 getValueName(F->getArgumentList().front()));
714
715 for (Function::ArgumentListType::const_iterator
716 I = F->getArgumentList().begin()+1,
717 E = F->getArgumentList().end(); I != E; ++I) {
718 Out << ", ";
719 printTypeVar((*I)->getType(), getValueName(*I));
720 }
721 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000722 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000723 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000724 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000725 FT->getParamTypes().begin(),
726 E = FT->getParamTypes().end(); I != E; ++I) {
727 if (I != FT->getParamTypes().begin()) Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000728 printType(*I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000729 }
730 }
731
732 // Finish printing arguments...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000733 if (FT->isVarArg()) {
734 if (FT->getParamTypes().size()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000735 Out << "..."; // Output varargs portion of signature!
736 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000737 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000738}
739
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000740
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000741void CWriter::printFunction(Function *F) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000742 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000743
Chris Lattnerf34ee812002-05-09 03:12:34 +0000744 Table.incorporateFunction(F);
745
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000746 printFunctionSignature(F);
747 Out << " {\n";
748
Chris Lattner16c7bb22002-05-09 02:28:59 +0000749 // Process each of the basic blocks, gather information and call the
750 // output methods on the CLocalVars and Function* objects.
751
752 // gather local variable information for each basic block
Chris Lattner7683a122002-05-09 20:33:35 +0000753 CLocalVars CLV;
754 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
755 if ((*I)->getType() != Type::VoidTy)
756 CLV.addLocalVar((*I)->getType(), getValueName(*I));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000757
Chris Lattner7683a122002-05-09 20:33:35 +0000758 // print the local variables
759 for (map<const Type*, VarListType>::iterator I = CLV.LocalVars.begin(),
760 E = CLV.LocalVars.end(); I != E; ++I)
761 for (VarListType::iterator TI = I->second.begin(), E = I->second.end();
762 TI != E; ++TI) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000763 Out << " ";
Chris Lattner7683a122002-05-09 20:33:35 +0000764 printTypeVar(I->first, *TI);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000765 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000766 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000767
768 // print the basic blocks
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000769 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
770 BasicBlock *BB = *I, *Prev = I != F->begin() ? *(I-1) : 0;
771
772 // Don't print the label for the basic block if there are no uses, or if the
773 // only terminator use is the precessor basic block's terminator. We have
774 // to scan the use list because PHI nodes use basic blocks too but do not
775 // require a label to be generated.
776 //
777 bool NeedsLabel = false;
778 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
779 UI != UE; ++UI)
780 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
781 if (TI != Prev->getTerminator()) {
782 NeedsLabel = true;
783 break;
784 }
785
786 if (NeedsLabel) Out << getValueName(BB) << ":\n";
787
788 // Output all of the instructions in the basic block...
789 // print the basic blocks
790 CInstPrintVisitor CIPV(*this, Table, Out);
791 CIPV.visit(BB);
792 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000793
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000794 Out << "}\n\n";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000795 Table.purgeFunction();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000796}
797
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000798void CWriter::writeOperand(const Value *Operand) {
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000799 if (isa<GlobalVariable>(Operand))
800 Out << "(&"; // Global variables are references as their addresses by llvm
Chris Lattner44408262002-05-09 03:50:42 +0000801
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000802 if (Operand->hasName()) {
Chris Lattner2f499022002-05-09 04:21:21 +0000803 Out << getValueName(Operand);
Chris Lattner44408262002-05-09 03:50:42 +0000804 } else if (const Constant *CPV = dyn_cast<const Constant>(Operand)) {
Chris Lattner1f02c892002-05-09 20:14:10 +0000805 if (isa<ConstantPointerNull>(CPV)) {
806 Out << "((";
807 printTypeVar(CPV->getType(), "");
808 Out << ")NULL)";
809 } else
Chris Lattner16c7bb22002-05-09 02:28:59 +0000810 Out << getConstStrValue(CPV);
Chris Lattner44408262002-05-09 03:50:42 +0000811 } else {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000812 int Slot = Table.getValSlot(Operand);
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000813 assert(Slot >= 0 && "Malformed LLVM!");
814 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
Chris Lattner16c7bb22002-05-09 02:28:59 +0000815 }
Chris Lattner44408262002-05-09 03:50:42 +0000816
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000817 if (isa<GlobalVariable>(Operand))
Chris Lattner44408262002-05-09 03:50:42 +0000818 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000819}
820
821
822//===----------------------------------------------------------------------===//
823// External Interface declaration
824//===----------------------------------------------------------------------===//
825
Chris Lattnerf34ee812002-05-09 03:12:34 +0000826void WriteToC(const Module *M, ostream &Out) {
827 assert(M && "You can't write a null module!!");
828 SlotCalculator SlotTable(M, false);
829 CWriter W(Out, SlotTable, M);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000830 W.write((Module*)M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000831 Out.flush();
832}