blob: 27db6cf3e9bffb58f52f4988538c8f35ca534626 [file] [log] [blame]
Chris Lattner3af3ba82002-05-09 21:31:18 +00001//===-- Writer.cpp - Library for converting LLVM code to C ----------------===//
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00002//
3// This library implements the functionality defined in llvm/Assembly/CWriter.h
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00004//
5// TODO : Recursive types.
Chris Lattner16c7bb22002-05-09 02:28:59 +00006//
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00007//===-----------------------------------------------------------------------==//
Chris Lattner16c7bb22002-05-09 02:28:59 +00008
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00009#include "llvm/Assembly/CWriter.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000010#include "llvm/Constants.h"
Chris Lattner2f5f51a2002-05-09 03:28:37 +000011#include "llvm/DerivedTypes.h"
12#include "llvm/Module.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000013#include "llvm/iMemory.h"
14#include "llvm/iTerminators.h"
15#include "llvm/iPHINode.h"
16#include "llvm/iOther.h"
Chris Lattner2f5f51a2002-05-09 03:28:37 +000017#include "llvm/iOperators.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000018#include "llvm/SymbolTable.h"
Chris Lattner3af3ba82002-05-09 21:31:18 +000019#include "llvm/SlotCalculator.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000020#include "llvm/Support/InstVisitor.h"
Chris Lattner7683a122002-05-09 20:33:35 +000021#include "llvm/Support/InstIterator.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000022#include "Support/StringExtras.h"
23#include "Support/STLExtras.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000024#include <algorithm>
Chris Lattner78771c82002-05-17 04:55:35 +000025#include <set>
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000026using std::string;
27using std::map;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000028using std::ostream;
29
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000030static std::string getConstStrValue(const Constant* CPV);
31
32
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000033static std::string getConstArrayStrValue(const Constant* CPV) {
34 std::string Result;
35
36 // As a special case, print the array as a string if it is an array of
37 // ubytes or an array of sbytes with positive values.
38 //
39 const Type *ETy = cast<ArrayType>(CPV->getType())->getElementType();
40 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
41
Chris Lattner4fbf26d2002-05-09 20:53:56 +000042 // Make sure the last character is a null char, as automatically added by C
43 if (CPV->getNumOperands() == 0 ||
44 !cast<Constant>(*(CPV->op_end()-1))->isNullValue())
45 isString = false;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000046
47 if (isString) {
48 Result = "\"";
Chris Lattner2f5eb4e2002-05-09 03:56:52 +000049 // Do not include the last character, which we know is null
50 for (unsigned i = 0, e = CPV->getNumOperands()-1; i != e; ++i) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000051 unsigned char C = (ETy == Type::SByteTy) ?
52 (unsigned char)cast<ConstantSInt>(CPV->getOperand(i))->getValue() :
53 (unsigned char)cast<ConstantUInt>(CPV->getOperand(i))->getValue();
54
55 if (isprint(C)) {
56 Result += C;
57 } else {
Chris Lattner2f5eb4e2002-05-09 03:56:52 +000058 switch (C) {
59 case '\n': Result += "\\n"; break;
60 case '\t': Result += "\\t"; break;
61 case '\r': Result += "\\r"; break;
62 case '\v': Result += "\\v"; break;
63 case '\a': Result += "\\a"; break;
64 default:
65 Result += "\\x";
66 Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
67 Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
68 break;
69 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000070 }
71 }
72 Result += "\"";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000073 } else {
74 Result = "{";
75 if (CPV->getNumOperands()) {
76 Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
77 for (unsigned i = 1; i < CPV->getNumOperands(); i++)
78 Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
79 }
80 Result += " }";
81 }
82
83 return Result;
84}
85
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000086static std::string getConstStrValue(const Constant* CPV) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +000087 switch (CPV->getType()->getPrimitiveID()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +000088 case Type::BoolTyID: return CPV == ConstantBool::False ? "0" : "1";
Chris Lattner3ef6dc72002-05-09 14:40:11 +000089 case Type::SByteTyID:
90 case Type::ShortTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +000091 case Type::IntTyID: return itostr(cast<ConstantSInt>(CPV)->getValue());
92 case Type::LongTyID: return itostr(cast<ConstantSInt>(CPV)->getValue())+"ll";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000093
Chris Lattner3ef6dc72002-05-09 14:40:11 +000094 case Type::UByteTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +000095 case Type::UShortTyID:return utostr(cast<ConstantUInt>(CPV)->getValue());
96 case Type::UIntTyID: return utostr(cast<ConstantUInt>(CPV)->getValue())+"u";
97 case Type::ULongTyID:return utostr(cast<ConstantUInt>(CPV)->getValue())+"ull";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000098
Chris Lattner3ef6dc72002-05-09 14:40:11 +000099 case Type::FloatTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000100 case Type::DoubleTyID: return ftostr(cast<ConstantFP>(CPV)->getValue());
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000101
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000102 case Type::ArrayTyID: return getConstArrayStrValue(CPV);
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000103
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000104 case Type::StructTyID: {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000105 std::string Result = "{";
106 if (CPV->getNumOperands()) {
107 Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
108 for (unsigned i = 1; i < CPV->getNumOperands(); i++)
109 Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000110 }
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000111 return Result + " }";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000112 }
113
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000114 default:
115 cerr << "Unknown constant type: " << CPV << "\n";
116 abort();
117 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000118}
119
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000120// Pass the Type* variable and and the variable name and this prints out the
121// variable declaration.
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000122//
Chris Lattner16c7bb22002-05-09 02:28:59 +0000123static string calcTypeNameVar(const Type *Ty,
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000124 map<const Type *, string> &TypeNames,
125 const string &NameSoFar, bool ignoreName = false){
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000126 if (Ty->isPrimitiveType())
127 switch (Ty->getPrimitiveID()) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000128 case Type::VoidTyID: return "void " + NameSoFar;
129 case Type::BoolTyID: return "bool " + NameSoFar;
130 case Type::UByteTyID: return "unsigned char " + NameSoFar;
131 case Type::SByteTyID: return "signed char " + NameSoFar;
132 case Type::UShortTyID: return "unsigned short " + NameSoFar;
133 case Type::ShortTyID: return "short " + NameSoFar;
134 case Type::UIntTyID: return "unsigned " + NameSoFar;
135 case Type::IntTyID: return "int " + NameSoFar;
136 case Type::ULongTyID: return "unsigned long long " + NameSoFar;
137 case Type::LongTyID: return "signed long long " + NameSoFar;
Chris Lattner1f02c892002-05-09 20:14:10 +0000138 case Type::FloatTyID: return "float " + NameSoFar;
139 case Type::DoubleTyID: return "double " + NameSoFar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000140 default :
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000141 cerr << "Unknown primitive type: " << Ty << "\n";
142 abort();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000143 }
144
145 // Check to see if the type is named.
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000146 if (!ignoreName) {
147 map<const Type *, string>::iterator I = TypeNames.find(Ty);
148 if (I != TypeNames.end())
149 return I->second + " " + NameSoFar;
150 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000151
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000152 string Result;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000153 switch (Ty->getPrimitiveID()) {
154 case Type::FunctionTyID: {
Chris Lattner7683a122002-05-09 20:33:35 +0000155 const FunctionType *MTy = cast<FunctionType>(Ty);
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000156 Result += calcTypeNameVar(MTy->getReturnType(), TypeNames, "");
Chris Lattner7683a122002-05-09 20:33:35 +0000157 Result += " " + NameSoFar + " (";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000158 for (FunctionType::ParamTypes::const_iterator
159 I = MTy->getParamTypes().begin(),
160 E = MTy->getParamTypes().end(); I != E; ++I) {
161 if (I != MTy->getParamTypes().begin())
162 Result += ", ";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000163 Result += calcTypeNameVar(*I, TypeNames, "");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000164 }
165 if (MTy->isVarArg()) {
166 if (!MTy->getParamTypes().empty())
167 Result += ", ";
168 Result += "...";
169 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000170 return Result + ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000171 }
172 case Type::StructTyID: {
173 const StructType *STy = cast<const StructType>(Ty);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000174 Result = NameSoFar + " {\n";
175 unsigned indx = 0;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000176 for (StructType::ElementTypes::const_iterator
177 I = STy->getElementTypes().begin(),
178 E = STy->getElementTypes().end(); I != E; ++I) {
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000179 Result += " " +calcTypeNameVar(*I, TypeNames, "field" + utostr(indx++));
180 Result += ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000181 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000182 return Result + "}";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000183 }
184
Chris Lattner3af3ba82002-05-09 21:31:18 +0000185 case Type::PointerTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000186 return calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(),
187 TypeNames, "*" + NameSoFar);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000188
189 case Type::ArrayTyID: {
190 const ArrayType *ATy = cast<const ArrayType>(Ty);
191 int NumElements = ATy->getNumElements();
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000192 return calcTypeNameVar(ATy->getElementType(), TypeNames,
193 NameSoFar + "[" + itostr(NumElements) + "]");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000194 }
195 default:
196 assert(0 && "Unhandled case in getTypeProps!");
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000197 abort();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000198 }
199
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000200 return Result;
201}
202
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000203namespace {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000204 class CWriter : public InstVisitor<CWriter> {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000205 ostream& Out;
206 SlotCalculator &Table;
207 const Module *TheModule;
208 map<const Type *, string> TypeNames;
Chris Lattner78771c82002-05-17 04:55:35 +0000209 std::set<const Value*> MangledGlobals;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000210 public:
211 inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M)
212 : Out(o), Table(Tab), TheModule(M) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000213 }
214
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000215 inline void write(Module *M) { printModule(M); }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000216
Chris Lattner3af3ba82002-05-09 21:31:18 +0000217 ostream& printType(const Type *Ty, const string &VariableName = "") {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000218 return Out << calcTypeNameVar(Ty, TypeNames, VariableName);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000219 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000220
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000221 void writeOperand(Value *Operand);
222 void writeOperandInternal(Value *Operand);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000223
224 string getValueName(const Value *V);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000225
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000226 private :
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000227 void printModule(Module *M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000228 void printSymbolTable(const SymbolTable &ST);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000229 void printGlobal(const GlobalVariable *GV);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000230 void printFunctionSignature(const Function *F);
231 void printFunctionDecl(const Function *F); // Print just the forward decl
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000232
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000233 void printFunction(Function *);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000234
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000235 // isInlinableInst - Attempt to inline instructions into their uses to build
236 // trees as much as possible. To do this, we have to consistently decide
237 // what is acceptable to inline, so that variable declarations don't get
238 // printed and an extra copy of the expr is not emitted.
239 //
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000240 static bool isInlinableInst(const Instruction &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000241 // Must be an expression, must be used exactly once. If it is dead, we
242 // emit it inline where it would go.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000243 if (I.getType() == Type::VoidTy || I.use_size() != 1 ||
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000244 isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I))
245 return false;
246
247 // Only inline instruction it it's use is in the same BB as the inst.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000248 return I.getParent() == cast<Instruction>(I.use_back())->getParent();
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000249 }
250
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000251 // Instruction visitation functions
252 friend class InstVisitor<CWriter>;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000253
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000254 void visitReturnInst(ReturnInst &I);
255 void visitBranchInst(BranchInst &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000256
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000257 void visitPHINode(PHINode &I) {}
258 void visitNot(GenericUnaryInst &I);
259 void visitBinaryOperator(Instruction &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000260
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000261 void visitCastInst (CastInst &I);
262 void visitCallInst (CallInst &I);
263 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000264
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000265 void visitMallocInst(MallocInst &I);
266 void visitAllocaInst(AllocaInst &I);
267 void visitFreeInst (FreeInst &I);
268 void visitLoadInst (LoadInst &I);
269 void visitStoreInst (StoreInst &I);
270 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000271
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000272 void visitInstruction(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000273 cerr << "C Writer does not know about " << I;
274 abort();
275 }
276
277 void outputLValue(Instruction *I) {
278 Out << " " << getValueName(I) << " = ";
279 }
280 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
281 unsigned Indent);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000282 void printIndexingExpr(MemAccessInst &MAI);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000283 };
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000284}
285
Chris Lattner2f499022002-05-09 04:21:21 +0000286// We dont want identifier names with ., space, - in them.
287// So we replace them with _
288static string makeNameProper(string x) {
289 string tmp;
290 for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
291 switch (*sI) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000292 case '.': tmp += "d_"; break;
293 case ' ': tmp += "s_"; break;
294 case '-': tmp += "D_"; break;
Chris Lattner2f499022002-05-09 04:21:21 +0000295 default: tmp += *sI;
296 }
297
298 return tmp;
299}
300
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000301string CWriter::getValueName(const Value *V) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000302 if (V->hasName()) { // Print out the label if it exists...
303 if (isa<GlobalValue>(V) && // Do not mangle globals...
304 cast<GlobalValue>(V)->hasExternalLinkage() && // Unless it's internal or
305 !MangledGlobals.count(V)) // Unless the name would collide if we don't
Chris Lattner2f499022002-05-09 04:21:21 +0000306 return makeNameProper(V->getName());
307
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000308 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
309 makeNameProper(V->getName());
Chris Lattner2f499022002-05-09 04:21:21 +0000310 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000311
312 int Slot = Table.getValSlot(V);
313 assert(Slot >= 0 && "Invalid value!");
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000314 return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000315}
316
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000317void CWriter::writeOperandInternal(Value *Operand) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000318 if (Operand->hasName()) {
319 Out << getValueName(Operand);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000320 } else if (Constant *CPV = dyn_cast<Constant>(Operand)) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000321 if (isa<ConstantPointerNull>(CPV)) {
322 Out << "((";
Chris Lattner3af3ba82002-05-09 21:31:18 +0000323 printType(CPV->getType(), "");
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000324 Out << ")NULL)";
325 } else
326 Out << getConstStrValue(CPV);
327 } else {
328 int Slot = Table.getValSlot(Operand);
329 assert(Slot >= 0 && "Malformed LLVM!");
330 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
331 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000332}
333
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000334void CWriter::writeOperand(Value *Operand) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000335 if (Instruction *I = dyn_cast<Instruction>(Operand))
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000336 if (isInlinableInst(*I)) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000337 // Should we inline this instruction to build a tree?
338 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000339 visit(*I);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000340 Out << ")";
341 return;
342 }
343
344 if (isa<GlobalVariable>(Operand))
345 Out << "(&"; // Global variables are references as their addresses by llvm
346
347 writeOperandInternal(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000348
349 if (isa<GlobalVariable>(Operand))
350 Out << ")";
351}
352
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000353void CWriter::printModule(Module *M) {
Chris Lattner78771c82002-05-17 04:55:35 +0000354 // Calculate which global values have names that will collide when we throw
355 // away type information.
Chris Lattner594b9fa2002-05-21 21:10:04 +0000356 { // Scope to delete the FoundNames set when we are done with it...
Chris Lattner78771c82002-05-17 04:55:35 +0000357 std::set<string> FoundNames;
358 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000359 if (I->hasName()) // If the global has a name...
360 if (FoundNames.count(I->getName())) // And the name is already used
361 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000362 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000363 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000364
365 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000366 if (I->hasName()) // If the global has a name...
367 if (FoundNames.count(I->getName())) // And the name is already used
368 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000369 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000370 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000371 }
372
373
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000374 // printing stdlib inclusion
375 // Out << "#include <stdlib.h>\n";
376
Chris Lattner16c7bb22002-05-09 02:28:59 +0000377 // get declaration for alloca
378 Out << "/* Provide Declarations */\n"
Chris Lattner594b9fa2002-05-21 21:10:04 +0000379 << "#include <malloc.h>\n"
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000380 << "#include <alloca.h>\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000381
382 // Provide a definition for null if one does not already exist.
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000383 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000384 << "typedef unsigned char bool;\n"
385
386 << "\n\n/* Global Symbols */\n";
387
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000388 // Loop over the symbol table, emitting all named constants...
389 if (M->hasSymbolTable())
390 printSymbolTable(*M->getSymbolTable());
391
Chris Lattner16c7bb22002-05-09 02:28:59 +0000392 Out << "\n\n/* Global Data */\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000393 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
394 if (I->hasInternalLinkage()) Out << "static ";
395 printType(I->getType()->getElementType(), getValueName(I));
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000396
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000397 if (I->hasInitializer()) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000398 Out << " = " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000399 writeOperand(I->getInitializer());
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000400 }
401 Out << ";\n";
402 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000403
Chris Lattner16c7bb22002-05-09 02:28:59 +0000404 // First output all the declarations of the functions as C requires Functions
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000405 // be declared before they are used.
Chris Lattner16c7bb22002-05-09 02:28:59 +0000406 //
407 Out << "\n\n/* Function Declarations */\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000408 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
409 printFunctionDecl(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000410
Chris Lattner16c7bb22002-05-09 02:28:59 +0000411 // Output all of the functions...
412 Out << "\n\n/* Function Bodies */\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000413 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
414 printFunction(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000415}
416
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000417
418// printSymbolTable - Run through symbol table looking for named constants
419// if a named constant is found, emit it's declaration...
420// Assuming that symbol table has only types and constants.
421void CWriter::printSymbolTable(const SymbolTable &ST) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000422 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
423 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
424 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
425
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000426 for (; I != End; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000427 if (const Type *Ty = dyn_cast<StructType>(I->second)) {
428 string Name = "struct l_" + makeNameProper(I->first);
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000429 Out << Name << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000430
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000431 TypeNames.insert(std::make_pair(Ty, Name));
432 }
433 }
434
435 Out << "\n";
436
437 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
438 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
439 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
440
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000441 for (; I != End; ++I) {
442 const Value *V = I->second;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000443 if (const Type *Ty = dyn_cast<Type>(V)) {
444 string Name = "l_" + makeNameProper(I->first);
Chris Lattner1f02c892002-05-09 20:14:10 +0000445 if (isa<StructType>(Ty))
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000446 Name = "struct " + makeNameProper(Name);
Chris Lattner1f02c892002-05-09 20:14:10 +0000447 else
448 Out << "typedef ";
449
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000450 Out << calcTypeNameVar(Ty, TypeNames, Name, true) << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000451 }
452 }
453 }
454}
455
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000456
Chris Lattner16c7bb22002-05-09 02:28:59 +0000457// printFunctionDecl - Print function declaration
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000458//
Chris Lattner16c7bb22002-05-09 02:28:59 +0000459void CWriter::printFunctionDecl(const Function *F) {
460 printFunctionSignature(F);
461 Out << ";\n";
462}
463
464void CWriter::printFunctionSignature(const Function *F) {
465 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000466
467 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000468 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000469
Chris Lattner16c7bb22002-05-09 02:28:59 +0000470 // Print out the return type and name...
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000471 printType(F->getReturnType());
Chris Lattner1f02c892002-05-09 20:14:10 +0000472 Out << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000473
Chris Lattner16c7bb22002-05-09 02:28:59 +0000474 if (!F->isExternal()) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000475 if (!F->aempty()) {
476 printType(F->afront().getType(), getValueName(F->abegin()));
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000477
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000478 for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
479 I != E; ++I) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000480 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000481 printType(I->getType(), getValueName(I));
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000482 }
483 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000484 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000485 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000486 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000487 FT->getParamTypes().begin(),
488 E = FT->getParamTypes().end(); I != E; ++I) {
489 if (I != FT->getParamTypes().begin()) Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000490 printType(*I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000491 }
492 }
493
494 // Finish printing arguments...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000495 if (FT->isVarArg()) {
496 if (FT->getParamTypes().size()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000497 Out << "..."; // Output varargs portion of signature!
498 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000499 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000500}
501
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000502
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000503void CWriter::printFunction(Function *F) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000504 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000505
Chris Lattnerf34ee812002-05-09 03:12:34 +0000506 Table.incorporateFunction(F);
507
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000508 printFunctionSignature(F);
509 Out << " {\n";
510
Chris Lattner497e19a2002-05-09 20:39:03 +0000511 // print local variable information for the function
Chris Lattner7683a122002-05-09 20:33:35 +0000512 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000513 if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000514 Out << " ";
Chris Lattner3af3ba82002-05-09 21:31:18 +0000515 printType((*I)->getType(), getValueName(*I));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000516 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000517 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000518
519 // print the basic blocks
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000520 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
521 BasicBlock *Prev = BB->getPrev();
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000522
523 // Don't print the label for the basic block if there are no uses, or if the
524 // only terminator use is the precessor basic block's terminator. We have
525 // to scan the use list because PHI nodes use basic blocks too but do not
526 // require a label to be generated.
527 //
528 bool NeedsLabel = false;
529 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
530 UI != UE; ++UI)
531 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
532 if (TI != Prev->getTerminator()) {
533 NeedsLabel = true;
534 break;
535 }
536
537 if (NeedsLabel) Out << getValueName(BB) << ":\n";
538
539 // Output all of the instructions in the basic block...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000540 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II){
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000541 if (!isInlinableInst(*II) && !isa<PHINode>(*II)) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000542 if (II->getType() != Type::VoidTy)
543 outputLValue(II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000544 else
545 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000546 visit(*II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000547 Out << ";\n";
548 }
549 }
550
551 // Don't emit prefix or suffix for the terminator...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000552 visit(*BB->getTerminator());
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000553 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000554
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000555 Out << "}\n\n";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000556 Table.purgeFunction();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000557}
558
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000559// Specific Instruction type classes... note that all of the casts are
560// neccesary because we use the instruction classes as opaque types...
561//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000562void CWriter::visitReturnInst(ReturnInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000563 // Don't output a void return if this is the last basic block in the function
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000564 if (I.getNumOperands() == 0 &&
565 &*--I.getParent()->getParent()->end() == I.getParent() &&
566 !I.getParent()->size() == 1) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000567 return;
Chris Lattner963b70b2002-05-21 18:05:19 +0000568 }
Chris Lattner44408262002-05-09 03:50:42 +0000569
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000570 Out << " return";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000571 if (I.getNumOperands()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000572 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000573 writeOperand(I.getOperand(0));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000574 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000575 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000576}
577
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000578static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
579 // If PHI nodes need copies, we need the copy code...
580 if (isa<PHINode>(To->front()) ||
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000581 From->getNext() != To) // Not directly successor, need goto
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000582 return true;
583
584 // Otherwise we don't need the code.
585 return false;
586}
587
588void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
589 unsigned Indent) {
590 for (BasicBlock::iterator I = Succ->begin();
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000591 PHINode *PN = dyn_cast<PHINode>(&*I); ++I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000592 // now we have to do the printing
593 Out << string(Indent, ' ');
594 outputLValue(PN);
595 writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
596 Out << "; /* for PHI node */\n";
597 }
598
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000599 if (CurBB->getNext() != Succ) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000600 Out << string(Indent, ' ') << " goto ";
601 writeOperand(Succ);
602 Out << ";\n";
603 }
604}
605
606// Brach instruction printing - Avoid printing out a brach to a basic block that
607// immediately succeeds the current one.
608//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000609void CWriter::visitBranchInst(BranchInst &I) {
610 if (I.isConditional()) {
611 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(0))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000612 Out << " if (";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000613 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000614 Out << ") {\n";
615
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000616 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000617
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000618 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(1))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000619 Out << " } else {\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000620 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000621 }
622 } else {
623 // First goto not neccesary, assume second one is...
624 Out << " if (!";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000625 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000626 Out << ") {\n";
627
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000628 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000629 }
630
631 Out << " }\n";
632 } else {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000633 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000634 }
635 Out << "\n";
636}
637
638
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000639void CWriter::visitNot(GenericUnaryInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000640 Out << "~";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000641 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000642}
643
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000644void CWriter::visitBinaryOperator(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000645 // binary instructions, shift instructions, setCond instructions.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000646 if (isa<PointerType>(I.getType())) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000647 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000648 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000649 Out << ")";
650 }
651
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000652 if (isa<PointerType>(I.getType())) Out << "(long long)";
653 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000654
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000655 switch (I.getOpcode()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000656 case Instruction::Add: Out << " + "; break;
657 case Instruction::Sub: Out << " - "; break;
658 case Instruction::Mul: Out << "*"; break;
659 case Instruction::Div: Out << "/"; break;
660 case Instruction::Rem: Out << "%"; break;
661 case Instruction::And: Out << " & "; break;
662 case Instruction::Or: Out << " | "; break;
663 case Instruction::Xor: Out << " ^ "; break;
664 case Instruction::SetEQ: Out << " == "; break;
665 case Instruction::SetNE: Out << " != "; break;
666 case Instruction::SetLE: Out << " <= "; break;
667 case Instruction::SetGE: Out << " >= "; break;
668 case Instruction::SetLT: Out << " < "; break;
669 case Instruction::SetGT: Out << " > "; break;
670 case Instruction::Shl : Out << " << "; break;
671 case Instruction::Shr : Out << " >> "; break;
672 default: cerr << "Invalid operator type!" << I; abort();
673 }
674
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000675 if (isa<PointerType>(I.getType())) Out << "(long long)";
676 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000677}
678
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000679void CWriter::visitCastInst(CastInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000680 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000681 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000682 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000683 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000684}
685
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000686void CWriter::visitCallInst(CallInst &I) {
687 const PointerType *PTy = cast<PointerType>(I.getCalledValue()->getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000688 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
689 const Type *RetTy = FTy->getReturnType();
690
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000691 Out << getValueName(I.getOperand(0)) << "(";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000692
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000693 if (I.getNumOperands() > 1) {
694 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000695
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000696 for (unsigned op = 2, Eop = I.getNumOperands(); op != Eop; ++op) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000697 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000698 writeOperand(I.getOperand(op));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000699 }
700 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000701 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000702}
703
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000704void CWriter::visitMallocInst(MallocInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000705 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000706 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000707 Out << ")malloc(sizeof(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000708 printType(I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000709 Out << ")";
710
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000711 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000712 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000713 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000714 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000715 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000716}
717
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000718void CWriter::visitAllocaInst(AllocaInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000719 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000720 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000721 Out << ") alloca(sizeof(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000722 printType(I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000723 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000724 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000725 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000726 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000727 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000728 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000729}
730
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000731void CWriter::visitFreeInst(FreeInst &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000732 Out << "free(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000733 writeOperand(I.getOperand(0));
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000734 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000735}
736
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000737void CWriter::printIndexingExpr(MemAccessInst &MAI) {
738 MemAccessInst::op_iterator I = MAI.idx_begin(), E = MAI.idx_end();
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000739 if (I == E) {
740 // If accessing a global value with no indexing, avoid *(&GV) syndrome
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000741 if (GlobalValue *V = dyn_cast<GlobalValue>(MAI.getPointerOperand())) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000742 writeOperandInternal(V);
743 return;
744 }
745
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000746 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000747 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000748
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000749 writeOperand(MAI.getPointerOperand());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000750
751 if (I == E) return;
752
753 // Print out the -> operator if possible...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000754 const Constant *CI = dyn_cast<Constant>(I->get());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000755 if (CI && CI->isNullValue() && I+1 != E &&
756 (*(I+1))->getType() == Type::UByteTy) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000757 Out << "->field" << cast<ConstantUInt>(*(I+1))->getValue();
758 I += 2;
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000759 }
760
761 for (; I != E; ++I)
762 if ((*I)->getType() == Type::UIntTy) {
763 Out << "[";
764 writeOperand(*I);
765 Out << "]";
766 } else {
767 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
768 }
769}
770
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000771void CWriter::visitLoadInst(LoadInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000772 printIndexingExpr(I);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000773}
774
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000775void CWriter::visitStoreInst(StoreInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000776 printIndexingExpr(I);
777 Out << " = ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000778 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000779}
780
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000781void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000782 Out << "&";
783 printIndexingExpr(I);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000784}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000785
786//===----------------------------------------------------------------------===//
787// External Interface declaration
788//===----------------------------------------------------------------------===//
789
Chris Lattnerf34ee812002-05-09 03:12:34 +0000790void WriteToC(const Module *M, ostream &Out) {
791 assert(M && "You can't write a null module!!");
792 SlotCalculator SlotTable(M, false);
793 CWriter W(Out, SlotTable, M);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000794 W.write((Module*)M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000795 Out.flush();
796}