blob: 98503f3bbe1562e29a462f8dc98b4a1bc28096a3 [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"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000011#include "llvm/SlotCalculator.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000012#include "llvm/Constants.h"
Chris Lattner2f5f51a2002-05-09 03:28:37 +000013#include "llvm/DerivedTypes.h"
14#include "llvm/Module.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000015#include "llvm/GlobalVariable.h"
Chris Lattner2f5f51a2002-05-09 03:28:37 +000016#include "llvm/Function.h"
17#include "llvm/Argument.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000018#include "llvm/BasicBlock.h"
19#include "llvm/iMemory.h"
20#include "llvm/iTerminators.h"
21#include "llvm/iPHINode.h"
22#include "llvm/iOther.h"
Chris Lattner2f5f51a2002-05-09 03:28:37 +000023#include "llvm/iOperators.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000024#include "llvm/SymbolTable.h"
25#include "llvm/Support/InstVisitor.h"
Chris Lattner7683a122002-05-09 20:33:35 +000026#include "llvm/Support/InstIterator.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000027#include "Support/StringExtras.h"
28#include "Support/STLExtras.h"
29
30#include <algorithm>
31#include <strstream>
32using std::string;
33using std::map;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000034using std::ostream;
35
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000036static std::string getConstStrValue(const Constant* CPV);
37
38
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000039static std::string getConstArrayStrValue(const Constant* CPV) {
40 std::string Result;
41
42 // As a special case, print the array as a string if it is an array of
43 // ubytes or an array of sbytes with positive values.
44 //
45 const Type *ETy = cast<ArrayType>(CPV->getType())->getElementType();
46 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
47
48 if (ETy == Type::SByteTy) {
49 for (unsigned i = 0; i < CPV->getNumOperands(); ++i)
50 if (ETy == Type::SByteTy &&
51 cast<ConstantSInt>(CPV->getOperand(i))->getValue() < 0) {
52 isString = false;
53 break;
54 }
55 }
Chris Lattner2f5eb4e2002-05-09 03:56:52 +000056 if (isString) {
57 // Make sure the last character is a null char, as automatically added by C
58 if (CPV->getNumOperands() == 0 ||
59 !cast<Constant>(*(CPV->op_end()-1))->isNullValue())
60 isString = false;
61 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000062
63 if (isString) {
64 Result = "\"";
Chris Lattner2f5eb4e2002-05-09 03:56:52 +000065 // Do not include the last character, which we know is null
66 for (unsigned i = 0, e = CPV->getNumOperands()-1; i != e; ++i) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000067 unsigned char C = (ETy == Type::SByteTy) ?
68 (unsigned char)cast<ConstantSInt>(CPV->getOperand(i))->getValue() :
69 (unsigned char)cast<ConstantUInt>(CPV->getOperand(i))->getValue();
70
71 if (isprint(C)) {
72 Result += C;
73 } else {
Chris Lattner2f5eb4e2002-05-09 03:56:52 +000074 switch (C) {
75 case '\n': Result += "\\n"; break;
76 case '\t': Result += "\\t"; break;
77 case '\r': Result += "\\r"; break;
78 case '\v': Result += "\\v"; break;
79 case '\a': Result += "\\a"; break;
80 default:
81 Result += "\\x";
82 Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
83 Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
84 break;
85 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000086 }
87 }
88 Result += "\"";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000089 } else {
90 Result = "{";
91 if (CPV->getNumOperands()) {
92 Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
93 for (unsigned i = 1; i < CPV->getNumOperands(); i++)
94 Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
95 }
96 Result += " }";
97 }
98
99 return Result;
100}
101
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000102static std::string getConstStrValue(const Constant* CPV) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000103 switch (CPV->getType()->getPrimitiveID()) {
104 case Type::BoolTyID:
105 return CPV == ConstantBool::False ? "0" : "1";
106 case Type::SByteTyID:
107 case Type::ShortTyID:
108 case Type::IntTyID:
109 return itostr(cast<ConstantSInt>(CPV)->getValue());
110 case Type::LongTyID:
111 return itostr(cast<ConstantSInt>(CPV)->getValue()) + "ll";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000112
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000113 case Type::UByteTyID:
114 return utostr(cast<ConstantUInt>(CPV)->getValue());
115 case Type::UShortTyID:
116 return utostr(cast<ConstantUInt>(CPV)->getValue());
117 case Type::UIntTyID:
118 return utostr(cast<ConstantUInt>(CPV)->getValue())+"u";
119 case Type::ULongTyID:
120 return utostr(cast<ConstantUInt>(CPV)->getValue())+"ull";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000121
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000122 case Type::FloatTyID:
123 case Type::DoubleTyID:
124 return ftostr(cast<ConstantFP>(CPV)->getValue());
125
126 case Type::ArrayTyID:
127 return getConstArrayStrValue(CPV);
128
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000129 case Type::StructTyID: {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000130 std::string Result = "{";
131 if (CPV->getNumOperands()) {
132 Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
133 for (unsigned i = 1; i < CPV->getNumOperands(); i++)
134 Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000135 }
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000136 return Result + " }";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000137 }
138
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000139 default:
140 cerr << "Unknown constant type: " << CPV << "\n";
141 abort();
142 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000143}
144
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000145// Pass the Type* variable and and the variable name and this prints out the
146// variable declaration.
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000147//
Chris Lattner16c7bb22002-05-09 02:28:59 +0000148static string calcTypeNameVar(const Type *Ty,
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000149 map<const Type *, string> &TypeNames,
150 const string &NameSoFar, bool ignoreName = false){
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000151 if (Ty->isPrimitiveType())
152 switch (Ty->getPrimitiveID()) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000153 case Type::VoidTyID: return "void " + NameSoFar;
154 case Type::BoolTyID: return "bool " + NameSoFar;
155 case Type::UByteTyID: return "unsigned char " + NameSoFar;
156 case Type::SByteTyID: return "signed char " + NameSoFar;
157 case Type::UShortTyID: return "unsigned short " + NameSoFar;
158 case Type::ShortTyID: return "short " + NameSoFar;
159 case Type::UIntTyID: return "unsigned " + NameSoFar;
160 case Type::IntTyID: return "int " + NameSoFar;
161 case Type::ULongTyID: return "unsigned long long " + NameSoFar;
162 case Type::LongTyID: return "signed long long " + NameSoFar;
Chris Lattner1f02c892002-05-09 20:14:10 +0000163 case Type::FloatTyID: return "float " + NameSoFar;
164 case Type::DoubleTyID: return "double " + NameSoFar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000165 default :
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000166 cerr << "Unknown primitive type: " << Ty << "\n";
167 abort();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000168 }
169
170 // Check to see if the type is named.
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000171 if (!ignoreName) {
172 map<const Type *, string>::iterator I = TypeNames.find(Ty);
173 if (I != TypeNames.end())
174 return I->second + " " + NameSoFar;
175 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000176
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000177 string Result;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000178 switch (Ty->getPrimitiveID()) {
179 case Type::FunctionTyID: {
Chris Lattner7683a122002-05-09 20:33:35 +0000180 const FunctionType *MTy = cast<FunctionType>(Ty);
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000181 Result += calcTypeNameVar(MTy->getReturnType(), TypeNames, "");
Chris Lattner7683a122002-05-09 20:33:35 +0000182 Result += " " + NameSoFar + " (";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000183 for (FunctionType::ParamTypes::const_iterator
184 I = MTy->getParamTypes().begin(),
185 E = MTy->getParamTypes().end(); I != E; ++I) {
186 if (I != MTy->getParamTypes().begin())
187 Result += ", ";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000188 Result += calcTypeNameVar(*I, TypeNames, "");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000189 }
190 if (MTy->isVarArg()) {
191 if (!MTy->getParamTypes().empty())
192 Result += ", ";
193 Result += "...";
194 }
195 Result += ")";
196 break;
197 }
198 case Type::StructTyID: {
199 const StructType *STy = cast<const StructType>(Ty);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000200 Result = NameSoFar + " {\n";
201 unsigned indx = 0;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000202 for (StructType::ElementTypes::const_iterator
203 I = STy->getElementTypes().begin(),
204 E = STy->getElementTypes().end(); I != E; ++I) {
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000205 Result += " " +calcTypeNameVar(*I, TypeNames, "field" + utostr(indx++));
206 Result += ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000207 }
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000208 Result += "}";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000209 break;
210 }
211
212 case Type::PointerTyID: {
213 Result = calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(),
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000214 TypeNames, "*" + NameSoFar);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000215 break;
216 }
217
218 case Type::ArrayTyID: {
219 const ArrayType *ATy = cast<const ArrayType>(Ty);
220 int NumElements = ATy->getNumElements();
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000221 Result = calcTypeNameVar(ATy->getElementType(), TypeNames,
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000222 NameSoFar + "[" + itostr(NumElements) + "]");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000223 break;
224 }
225 default:
226 assert(0 && "Unhandled case in getTypeProps!");
227 Result = "<error>";
228 }
229
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000230 return Result;
231}
232
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000233namespace {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000234 class CWriter {
235 ostream& Out;
236 SlotCalculator &Table;
237 const Module *TheModule;
238 map<const Type *, string> TypeNames;
239 public:
240 inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M)
241 : Out(o), Table(Tab), TheModule(M) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000242 }
243
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000244 inline void write(Module *M) { printModule(M); }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000245
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000246 ostream& printTypeVar(const Type *Ty, const string &VariableName) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000247 return Out << calcTypeNameVar(Ty, TypeNames, VariableName);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000248 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000249
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000250 ostream& printType(const Type *Ty) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000251 return Out << calcTypeNameVar(Ty, TypeNames, "");
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000252 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000253
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000254 void writeOperand(const Value *Operand);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000255
256 string getValueName(const Value *V);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000257 private :
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000258
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000259 void printModule(Module *M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000260 void printSymbolTable(const SymbolTable &ST);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000261 void printGlobal(const GlobalVariable *GV);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000262 void printFunctionSignature(const Function *F);
263 void printFunctionDecl(const Function *F); // Print just the forward decl
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000264
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000265 void printFunction(Function *);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000266 };
267 /* END class CWriter */
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000268}
269
270namespace {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000271 /* CLASS CInstPrintVisitor */
272
273 class CInstPrintVisitor: public InstVisitor<CInstPrintVisitor> {
274 CWriter& CW;
275 SlotCalculator& Table;
276 ostream &Out;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000277
278 void outputLValue(Instruction *);
Chris Lattner1f02c892002-05-09 20:14:10 +0000279 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
280 unsigned Indent);
Chris Lattner44408262002-05-09 03:50:42 +0000281 void printIndexingExpr(MemAccessInst *MAI);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000282
283 public:
284 CInstPrintVisitor (CWriter &cw, SlotCalculator& table, ostream& o)
Chris Lattner44408262002-05-09 03:50:42 +0000285 : CW(cw), Table(table), Out(o) {}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000286
287 void visitCastInst(CastInst *I);
288 void visitCallInst(CallInst *I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000289 void visitShiftInst(ShiftInst *I) { visitBinaryOperator(I); }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000290 void visitReturnInst(ReturnInst *I);
291 void visitBranchInst(BranchInst *I);
292 void visitSwitchInst(SwitchInst *I);
293 void visitInvokeInst(InvokeInst *I) ;
294 void visitMallocInst(MallocInst *I);
295 void visitAllocaInst(AllocaInst *I);
296 void visitFreeInst(FreeInst *I);
297 void visitLoadInst(LoadInst *I);
298 void visitStoreInst(StoreInst *I);
299 void visitGetElementPtrInst(GetElementPtrInst *I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000300 void visitPHINode(PHINode *I) {}
301
302 void visitNot(GenericUnaryInst *I);
303 void visitBinaryOperator(Instruction *I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000304 };
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000305}
306
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000307void CInstPrintVisitor::outputLValue(Instruction *I) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000308 Out << " " << CW.getValueName(I) << " = ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000309}
310
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000311// Implement all "other" instructions, except for PHINode
312void CInstPrintVisitor::visitCastInst(CastInst *I) {
313 outputLValue(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000314 Out << "(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000315 CW.printType(I->getType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000316 Out << ")";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000317 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000318 Out << ";\n";
319}
320
321void CInstPrintVisitor::visitCallInst(CallInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000322 if (I->getType() != Type::VoidTy)
323 outputLValue(I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000324 else
325 Out << " ";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000326
Chris Lattner2f499022002-05-09 04:21:21 +0000327 const PointerType *PTy = cast<PointerType>(I->getCalledValue()->getType());
328 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
329 const Type *RetTy = FTy->getReturnType();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000330
Chris Lattner2f499022002-05-09 04:21:21 +0000331 Out << CW.getValueName(I->getOperand(0)) << "(";
332
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000333 if (I->getNumOperands() > 1) {
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000334 CW.writeOperand(I->getOperand(1));
Chris Lattner2f499022002-05-09 04:21:21 +0000335
336 for (unsigned op = 2, Eop = I->getNumOperands(); op != Eop; ++op) {
337 Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000338 CW.writeOperand(I->getOperand(op));
Chris Lattner2f499022002-05-09 04:21:21 +0000339 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000340 }
Chris Lattner2f499022002-05-09 04:21:21 +0000341 Out << ");\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000342}
343
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000344// Specific Instruction type classes... note that all of the casts are
345// neccesary because we use the instruction classes as opaque types...
346//
347void CInstPrintVisitor::visitReturnInst(ReturnInst *I) {
Chris Lattner1f02c892002-05-09 20:14:10 +0000348 // Don't output a void return if this is the last basic block in the function
349 if (I->getNumOperands() == 0 &&
350 *(I->getParent()->getParent()->end()-1) == I->getParent())
351 return;
352
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000353 Out << " return";
354 if (I->getNumOperands()) {
355 Out << " ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000356 CW.writeOperand(I->getOperand(0));
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000357 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000358 Out << ";\n";
359}
360
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000361// Return true if BB1 immediately preceeds BB2.
362static bool BBFollowsBB(BasicBlock *BB1, BasicBlock *BB2) {
363 Function *F = BB1->getParent();
364 Function::iterator I = find(F->begin(), F->end(), BB1);
365 assert(I != F->end() && "BB not in function!");
366 return *(I+1) == BB2;
367}
368
Chris Lattner1f02c892002-05-09 20:14:10 +0000369static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
370 // If PHI nodes need copies, we need the copy code...
371 if (isa<PHINode>(To->front()) ||
372 !BBFollowsBB(From, To)) // Not directly successor, need goto
373 return true;
374
375 // Otherwise we don't need the code.
376 return false;
377}
378
379void CInstPrintVisitor::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
380 unsigned Indent) {
381 for (BasicBlock::iterator I = Succ->begin();
382 PHINode *PN = dyn_cast<PHINode>(*I); ++I) {
383 // now we have to do the printing
384 Out << string(Indent, ' ');
385 outputLValue(PN);
386 CW.writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
387 Out << "; /* for PHI node */\n";
388 }
389
390 if (!BBFollowsBB(CurBB, Succ)) {
391 Out << string(Indent, ' ') << " goto ";
392 CW.writeOperand(Succ);
393 Out << ";\n";
394 }
395}
396
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000397// Brach instruction printing - Avoid printing out a brach to a basic block that
398// immediately succeeds the current one.
399//
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000400void CInstPrintVisitor::visitBranchInst(BranchInst *I) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000401 if (I->isConditional()) {
Chris Lattner1f02c892002-05-09 20:14:10 +0000402 if (isGotoCodeNeccessary(I->getParent(), I->getSuccessor(0))) {
403 Out << " if (";
404 CW.writeOperand(I->getCondition());
405 Out << ") {\n";
406
407 printBranchToBlock(I->getParent(), I->getSuccessor(0), 2);
408
409 if (isGotoCodeNeccessary(I->getParent(), I->getSuccessor(1))) {
410 Out << " } else {\n";
411 printBranchToBlock(I->getParent(), I->getSuccessor(1), 2);
412 }
413 } else {
414 // First goto not neccesary, assume second one is...
415 Out << " if (!";
416 CW.writeOperand(I->getCondition());
417 Out << ") {\n";
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000418
Chris Lattner1f02c892002-05-09 20:14:10 +0000419 printBranchToBlock(I->getParent(), I->getSuccessor(1), 2);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000420 }
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000421
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000422 Out << " }\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000423 } else {
Chris Lattner1f02c892002-05-09 20:14:10 +0000424 printBranchToBlock(I->getParent(), I->getSuccessor(0), 0);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000425 }
426 Out << "\n";
427}
428
429void CInstPrintVisitor::visitSwitchInst(SwitchInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000430 assert(0 && "Switch not implemented!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000431}
432
433void CInstPrintVisitor::visitInvokeInst(InvokeInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000434 assert(0 && "Invoke not implemented!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000435}
436
437void CInstPrintVisitor::visitMallocInst(MallocInst *I) {
438 outputLValue(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000439 Out << "(";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000440 CW.printType(I->getType());
441 Out << ")malloc(sizeof(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000442 CW.printType(I->getType()->getElementType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000443 Out << ")";
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000444
445 if (I->isArrayAllocation()) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000446 Out << " * " ;
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000447 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000448 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000449 Out << ");\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000450}
451
452void CInstPrintVisitor::visitAllocaInst(AllocaInst *I) {
453 outputLValue(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000454 Out << "(";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000455 CW.printType(I->getType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000456 Out << ") alloca(sizeof(";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000457 CW.printType(I->getType()->getElementType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000458 Out << ")";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000459 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 }
463 Out << ");\n";
464}
465
Chris Lattner1f02c892002-05-09 20:14:10 +0000466void CInstPrintVisitor::visitFreeInst(FreeInst *I) {
467 Out << " free(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000468 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000469 Out << ");\n";
470}
471
Chris Lattner44408262002-05-09 03:50:42 +0000472void CInstPrintVisitor::printIndexingExpr(MemAccessInst *MAI) {
Chris Lattner1f02c892002-05-09 20:14:10 +0000473 MemAccessInst::op_iterator I = MAI->idx_begin(), E = MAI->idx_end();
474 if (I == E)
475 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
476
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000477 CW.writeOperand(MAI->getPointerOperand());
Chris Lattner44408262002-05-09 03:50:42 +0000478
Chris Lattner8c8a3702002-05-09 15:59:50 +0000479 if (I == E) return;
480
481 // Print out the -> operator if possible...
482 Constant *CI = dyn_cast<Constant>(*I);
483 if (CI && CI->isNullValue() && I+1 != E &&
484 (*(I+1))->getType() == Type::UByteTy) {
485 ++I;
486 Out << "->field" << cast<ConstantUInt>(*I)->getValue();
487 ++I;
488 }
489
490 for (; I != E; ++I)
Chris Lattner44408262002-05-09 03:50:42 +0000491 if ((*I)->getType() == Type::UIntTy) {
492 Out << "[";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000493 CW.writeOperand(*I);
Chris Lattner44408262002-05-09 03:50:42 +0000494 Out << "]";
495 } else {
496 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000497 }
Chris Lattner44408262002-05-09 03:50:42 +0000498}
499
500void CInstPrintVisitor::visitLoadInst(LoadInst *I) {
501 outputLValue(I);
502 printIndexingExpr(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000503 Out << ";\n";
504}
505
Chris Lattner44408262002-05-09 03:50:42 +0000506void CInstPrintVisitor::visitStoreInst(StoreInst *I) {
507 Out << " ";
508 printIndexingExpr(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000509 Out << " = ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000510 CW.writeOperand(I->getOperand(0));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000511 Out << ";\n";
512}
513
514void CInstPrintVisitor::visitGetElementPtrInst(GetElementPtrInst *I) {
515 outputLValue(I);
Chris Lattner44408262002-05-09 03:50:42 +0000516 Out << "&";
517 printIndexingExpr(I);
518 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000519}
520
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000521void CInstPrintVisitor::visitNot(GenericUnaryInst *I) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000522 outputLValue(I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000523 Out << "~";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000524 CW.writeOperand(I->getOperand(0));
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000525 Out << ";\n";
526}
527
528void CInstPrintVisitor::visitBinaryOperator(Instruction *I) {
529 // binary instructions, shift instructions, setCond instructions.
530 outputLValue(I);
531 if (isa<PointerType>(I->getType())) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000532 Out << "(";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000533 CW.printType(I->getType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000534 Out << ")";
535 }
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000536
537 if (isa<PointerType>(I->getType())) Out << "(long long)";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000538 CW.writeOperand(I->getOperand(0));
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000539
540 switch (I->getOpcode()) {
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000541 case Instruction::Add: Out << " + "; break;
542 case Instruction::Sub: Out << " - "; break;
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000543 case Instruction::Mul: Out << "*"; break;
544 case Instruction::Div: Out << "/"; break;
545 case Instruction::Rem: Out << "%"; break;
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000546 case Instruction::And: Out << " & "; break;
547 case Instruction::Or: Out << " | "; break;
548 case Instruction::Xor: Out << " ^ "; break;
549 case Instruction::SetEQ: Out << " == "; break;
550 case Instruction::SetNE: Out << " != "; break;
551 case Instruction::SetLE: Out << " <= "; break;
552 case Instruction::SetGE: Out << " >= "; break;
553 case Instruction::SetLT: Out << " < "; break;
554 case Instruction::SetGT: Out << " > "; break;
555 case Instruction::Shl : Out << " << "; break;
556 case Instruction::Shr : Out << " >> "; break;
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000557 default: cerr << "Invalid operator type!" << I; abort();
558 }
559
560 if (isa<PointerType>(I->getType())) Out << "(long long)";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000561 CW.writeOperand(I->getOperand(1));
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000562 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000563}
564
565/* END : CInstPrintVisitor implementation */
566
Chris Lattner2f499022002-05-09 04:21:21 +0000567// We dont want identifier names with ., space, - in them.
568// So we replace them with _
569static string makeNameProper(string x) {
570 string tmp;
571 for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
572 switch (*sI) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000573 case '.': tmp += "d_"; break;
574 case ' ': tmp += "s_"; break;
575 case '-': tmp += "D_"; break;
Chris Lattner2f499022002-05-09 04:21:21 +0000576 case '_': tmp += "__"; break;
577 default: tmp += *sI;
578 }
579
580 return tmp;
581}
582
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000583string CWriter::getValueName(const Value *V) {
Chris Lattner2f499022002-05-09 04:21:21 +0000584 if (V->hasName()) { // Print out the label if it exists...
585 if (isa<GlobalValue>(V)) // Do not mangle globals...
586 return makeNameProper(V->getName());
587
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000588 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
589 makeNameProper(V->getName());
Chris Lattner2f499022002-05-09 04:21:21 +0000590 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000591
592 int Slot = Table.getValSlot(V);
593 assert(Slot >= 0 && "Invalid value!");
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000594 return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000595}
596
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000597void CWriter::printModule(Module *M) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000598 // printing stdlib inclusion
599 // Out << "#include <stdlib.h>\n";
600
Chris Lattner16c7bb22002-05-09 02:28:59 +0000601 // get declaration for alloca
602 Out << "/* Provide Declarations */\n"
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000603 << "#include <alloca.h>\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000604
605 // Provide a definition for null if one does not already exist.
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000606 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000607 << "typedef unsigned char bool;\n"
608
609 << "\n\n/* Global Symbols */\n";
610
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000611 // Loop over the symbol table, emitting all named constants...
612 if (M->hasSymbolTable())
613 printSymbolTable(*M->getSymbolTable());
614
Chris Lattner16c7bb22002-05-09 02:28:59 +0000615 Out << "\n\n/* Global Data */\n";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000616 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
617 GlobalVariable *GV = *I;
618 if (GV->hasInternalLinkage()) Out << "static ";
619 printTypeVar(GV->getType()->getElementType(), getValueName(GV));
620
621 if (GV->hasInitializer()) {
622 Out << " = " ;
623 writeOperand(GV->getInitializer());
624 }
625 Out << ";\n";
626 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000627
Chris Lattner16c7bb22002-05-09 02:28:59 +0000628 // First output all the declarations of the functions as C requires Functions
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000629 // be declared before they are used.
Chris Lattner16c7bb22002-05-09 02:28:59 +0000630 //
631 Out << "\n\n/* Function Declarations */\n";
632 for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunctionDecl));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000633
Chris Lattner16c7bb22002-05-09 02:28:59 +0000634 // Output all of the functions...
635 Out << "\n\n/* Function Bodies */\n";
636 for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunction));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000637}
638
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000639
640// printSymbolTable - Run through symbol table looking for named constants
641// if a named constant is found, emit it's declaration...
642// Assuming that symbol table has only types and constants.
643void CWriter::printSymbolTable(const SymbolTable &ST) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000644 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
645 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
646 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
647
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000648 for (; I != End; ++I)
649 if (const Type *Ty = dyn_cast<const StructType>(I->second)) {
650 string Name = "struct l_" + I->first;
651 Out << Name << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000652
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000653 TypeNames.insert(std::make_pair(Ty, Name));
654 }
655 }
656
657 Out << "\n";
658
659 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
660 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
661 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
662
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000663 for (; I != End; ++I) {
664 const Value *V = I->second;
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000665 if (const Type *Ty = dyn_cast<const Type>(V)) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000666 string Name = "l_" + I->first;
Chris Lattner1f02c892002-05-09 20:14:10 +0000667 if (isa<StructType>(Ty))
668 Name = "struct " + Name;
669 else
670 Out << "typedef ";
671
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000672 Out << calcTypeNameVar(Ty, TypeNames, Name, true) << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000673 }
674 }
675 }
676}
677
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000678
Chris Lattner16c7bb22002-05-09 02:28:59 +0000679// printFunctionDecl - Print function declaration
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000680//
Chris Lattner16c7bb22002-05-09 02:28:59 +0000681void CWriter::printFunctionDecl(const Function *F) {
682 printFunctionSignature(F);
683 Out << ";\n";
684}
685
686void CWriter::printFunctionSignature(const Function *F) {
687 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000688
689 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000690 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000691
Chris Lattner16c7bb22002-05-09 02:28:59 +0000692 // Print out the return type and name...
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000693 printType(F->getReturnType());
Chris Lattner1f02c892002-05-09 20:14:10 +0000694 Out << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000695
Chris Lattner16c7bb22002-05-09 02:28:59 +0000696 if (!F->isExternal()) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000697 if (!F->getArgumentList().empty()) {
698 printTypeVar(F->getArgumentList().front()->getType(),
699 getValueName(F->getArgumentList().front()));
700
701 for (Function::ArgumentListType::const_iterator
702 I = F->getArgumentList().begin()+1,
703 E = F->getArgumentList().end(); I != E; ++I) {
704 Out << ", ";
705 printTypeVar((*I)->getType(), getValueName(*I));
706 }
707 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000708 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000709 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000710 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000711 FT->getParamTypes().begin(),
712 E = FT->getParamTypes().end(); I != E; ++I) {
713 if (I != FT->getParamTypes().begin()) Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000714 printType(*I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000715 }
716 }
717
718 // Finish printing arguments...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000719 if (FT->isVarArg()) {
720 if (FT->getParamTypes().size()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000721 Out << "..."; // Output varargs portion of signature!
722 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000723 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000724}
725
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000726
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000727void CWriter::printFunction(Function *F) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000728 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000729
Chris Lattnerf34ee812002-05-09 03:12:34 +0000730 Table.incorporateFunction(F);
731
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000732 printFunctionSignature(F);
733 Out << " {\n";
734
Chris Lattner497e19a2002-05-09 20:39:03 +0000735 // print local variable information for the function
Chris Lattner7683a122002-05-09 20:33:35 +0000736 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner497e19a2002-05-09 20:39:03 +0000737 if ((*I)->getType() != Type::VoidTy) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000738 Out << " ";
Chris Lattner497e19a2002-05-09 20:39:03 +0000739 printTypeVar((*I)->getType(), getValueName(*I));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000740 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000741 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000742
743 // print the basic blocks
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000744 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
745 BasicBlock *BB = *I, *Prev = I != F->begin() ? *(I-1) : 0;
746
747 // Don't print the label for the basic block if there are no uses, or if the
748 // only terminator use is the precessor basic block's terminator. We have
749 // to scan the use list because PHI nodes use basic blocks too but do not
750 // require a label to be generated.
751 //
752 bool NeedsLabel = false;
753 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
754 UI != UE; ++UI)
755 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
756 if (TI != Prev->getTerminator()) {
757 NeedsLabel = true;
758 break;
759 }
760
761 if (NeedsLabel) Out << getValueName(BB) << ":\n";
762
763 // Output all of the instructions in the basic block...
764 // print the basic blocks
765 CInstPrintVisitor CIPV(*this, Table, Out);
766 CIPV.visit(BB);
767 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000768
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000769 Out << "}\n\n";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000770 Table.purgeFunction();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000771}
772
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000773void CWriter::writeOperand(const Value *Operand) {
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000774 if (isa<GlobalVariable>(Operand))
775 Out << "(&"; // Global variables are references as their addresses by llvm
Chris Lattner44408262002-05-09 03:50:42 +0000776
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000777 if (Operand->hasName()) {
Chris Lattner2f499022002-05-09 04:21:21 +0000778 Out << getValueName(Operand);
Chris Lattner44408262002-05-09 03:50:42 +0000779 } else if (const Constant *CPV = dyn_cast<const Constant>(Operand)) {
Chris Lattner1f02c892002-05-09 20:14:10 +0000780 if (isa<ConstantPointerNull>(CPV)) {
781 Out << "((";
782 printTypeVar(CPV->getType(), "");
783 Out << ")NULL)";
784 } else
Chris Lattner16c7bb22002-05-09 02:28:59 +0000785 Out << getConstStrValue(CPV);
Chris Lattner44408262002-05-09 03:50:42 +0000786 } else {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000787 int Slot = Table.getValSlot(Operand);
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000788 assert(Slot >= 0 && "Malformed LLVM!");
789 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
Chris Lattner16c7bb22002-05-09 02:28:59 +0000790 }
Chris Lattner44408262002-05-09 03:50:42 +0000791
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000792 if (isa<GlobalVariable>(Operand))
Chris Lattner44408262002-05-09 03:50:42 +0000793 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000794}
795
796
797//===----------------------------------------------------------------------===//
798// External Interface declaration
799//===----------------------------------------------------------------------===//
800
Chris Lattnerf34ee812002-05-09 03:12:34 +0000801void WriteToC(const Module *M, ostream &Out) {
802 assert(M && "You can't write a null module!!");
803 SlotCalculator SlotTable(M, false);
804 CWriter W(Out, SlotTable, M);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000805 W.write((Module*)M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000806 Out.flush();
807}