blob: df3fdac6a235648b195d0390f0bfd88d7a05bc1e [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>
Anand Shuklae6f74a92002-06-25 21:29:10 +000026#include <iostream>
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000027using std::string;
28using std::map;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000029using std::ostream;
Anand Shuklaeea60fc2002-06-25 20:44:04 +000030using std::cerr;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000031
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000032static std::string getConstStrValue(const Constant* CPV);
33
34
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000035static std::string getConstArrayStrValue(const Constant* CPV) {
36 std::string Result;
37
38 // As a special case, print the array as a string if it is an array of
39 // ubytes or an array of sbytes with positive values.
40 //
41 const Type *ETy = cast<ArrayType>(CPV->getType())->getElementType();
42 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
43
Chris Lattner4fbf26d2002-05-09 20:53:56 +000044 // Make sure the last character is a null char, as automatically added by C
45 if (CPV->getNumOperands() == 0 ||
46 !cast<Constant>(*(CPV->op_end()-1))->isNullValue())
47 isString = false;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000048
49 if (isString) {
50 Result = "\"";
Chris Lattner2f5eb4e2002-05-09 03:56:52 +000051 // Do not include the last character, which we know is null
52 for (unsigned i = 0, e = CPV->getNumOperands()-1; i != e; ++i) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000053 unsigned char C = (ETy == Type::SByteTy) ?
54 (unsigned char)cast<ConstantSInt>(CPV->getOperand(i))->getValue() :
55 (unsigned char)cast<ConstantUInt>(CPV->getOperand(i))->getValue();
56
57 if (isprint(C)) {
58 Result += C;
59 } else {
Chris Lattner2f5eb4e2002-05-09 03:56:52 +000060 switch (C) {
61 case '\n': Result += "\\n"; break;
62 case '\t': Result += "\\t"; break;
63 case '\r': Result += "\\r"; break;
64 case '\v': Result += "\\v"; break;
65 case '\a': Result += "\\a"; break;
66 default:
67 Result += "\\x";
68 Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
69 Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
70 break;
71 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000072 }
73 }
74 Result += "\"";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000075 } else {
76 Result = "{";
77 if (CPV->getNumOperands()) {
78 Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
79 for (unsigned i = 1; i < CPV->getNumOperands(); i++)
80 Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
81 }
82 Result += " }";
83 }
84
85 return Result;
86}
87
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000088static std::string getConstStrValue(const Constant* CPV) {
Chris Lattner3ef6dc72002-05-09 14:40:11 +000089 switch (CPV->getType()->getPrimitiveID()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +000090 case Type::BoolTyID: return CPV == ConstantBool::False ? "0" : "1";
Chris Lattner3ef6dc72002-05-09 14:40:11 +000091 case Type::SByteTyID:
92 case Type::ShortTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +000093 case Type::IntTyID: return itostr(cast<ConstantSInt>(CPV)->getValue());
94 case Type::LongTyID: return itostr(cast<ConstantSInt>(CPV)->getValue())+"ll";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000095
Chris Lattner3ef6dc72002-05-09 14:40:11 +000096 case Type::UByteTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +000097 case Type::UShortTyID:return utostr(cast<ConstantUInt>(CPV)->getValue());
98 case Type::UIntTyID: return utostr(cast<ConstantUInt>(CPV)->getValue())+"u";
99 case Type::ULongTyID:return utostr(cast<ConstantUInt>(CPV)->getValue())+"ull";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000100
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000101 case Type::FloatTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000102 case Type::DoubleTyID: return ftostr(cast<ConstantFP>(CPV)->getValue());
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000103
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000104 case Type::ArrayTyID: return getConstArrayStrValue(CPV);
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000105
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000106 case Type::StructTyID: {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000107 std::string Result = "{";
108 if (CPV->getNumOperands()) {
109 Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
110 for (unsigned i = 1; i < CPV->getNumOperands(); i++)
111 Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000112 }
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000113 return Result + " }";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000114 }
115
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000116 default:
117 cerr << "Unknown constant type: " << CPV << "\n";
118 abort();
119 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000120}
121
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000122// Pass the Type* variable and and the variable name and this prints out the
123// variable declaration.
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000124//
Chris Lattner16c7bb22002-05-09 02:28:59 +0000125static string calcTypeNameVar(const Type *Ty,
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000126 map<const Type *, string> &TypeNames,
127 const string &NameSoFar, bool ignoreName = false){
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000128 if (Ty->isPrimitiveType())
129 switch (Ty->getPrimitiveID()) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000130 case Type::VoidTyID: return "void " + NameSoFar;
131 case Type::BoolTyID: return "bool " + NameSoFar;
132 case Type::UByteTyID: return "unsigned char " + NameSoFar;
133 case Type::SByteTyID: return "signed char " + NameSoFar;
134 case Type::UShortTyID: return "unsigned short " + NameSoFar;
135 case Type::ShortTyID: return "short " + NameSoFar;
136 case Type::UIntTyID: return "unsigned " + NameSoFar;
137 case Type::IntTyID: return "int " + NameSoFar;
138 case Type::ULongTyID: return "unsigned long long " + NameSoFar;
139 case Type::LongTyID: return "signed long long " + NameSoFar;
Chris Lattner1f02c892002-05-09 20:14:10 +0000140 case Type::FloatTyID: return "float " + NameSoFar;
141 case Type::DoubleTyID: return "double " + NameSoFar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000142 default :
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000143 cerr << "Unknown primitive type: " << Ty << "\n";
144 abort();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000145 }
146
147 // Check to see if the type is named.
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000148 if (!ignoreName) {
149 map<const Type *, string>::iterator I = TypeNames.find(Ty);
150 if (I != TypeNames.end())
151 return I->second + " " + NameSoFar;
152 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000153
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000154 string Result;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000155 switch (Ty->getPrimitiveID()) {
156 case Type::FunctionTyID: {
Chris Lattner7683a122002-05-09 20:33:35 +0000157 const FunctionType *MTy = cast<FunctionType>(Ty);
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000158 Result += calcTypeNameVar(MTy->getReturnType(), TypeNames, "");
Chris Lattner7683a122002-05-09 20:33:35 +0000159 Result += " " + NameSoFar + " (";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000160 for (FunctionType::ParamTypes::const_iterator
161 I = MTy->getParamTypes().begin(),
162 E = MTy->getParamTypes().end(); I != E; ++I) {
163 if (I != MTy->getParamTypes().begin())
164 Result += ", ";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000165 Result += calcTypeNameVar(*I, TypeNames, "");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000166 }
167 if (MTy->isVarArg()) {
168 if (!MTy->getParamTypes().empty())
169 Result += ", ";
170 Result += "...";
171 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000172 return Result + ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000173 }
174 case Type::StructTyID: {
175 const StructType *STy = cast<const StructType>(Ty);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000176 Result = NameSoFar + " {\n";
177 unsigned indx = 0;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000178 for (StructType::ElementTypes::const_iterator
179 I = STy->getElementTypes().begin(),
180 E = STy->getElementTypes().end(); I != E; ++I) {
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000181 Result += " " +calcTypeNameVar(*I, TypeNames, "field" + utostr(indx++));
182 Result += ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000183 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000184 return Result + "}";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000185 }
186
Chris Lattner3af3ba82002-05-09 21:31:18 +0000187 case Type::PointerTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000188 return calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(),
189 TypeNames, "*" + NameSoFar);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000190
191 case Type::ArrayTyID: {
192 const ArrayType *ATy = cast<const ArrayType>(Ty);
193 int NumElements = ATy->getNumElements();
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000194 return calcTypeNameVar(ATy->getElementType(), TypeNames,
195 NameSoFar + "[" + itostr(NumElements) + "]");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000196 }
197 default:
198 assert(0 && "Unhandled case in getTypeProps!");
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000199 abort();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000200 }
201
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000202 return Result;
203}
204
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000205namespace {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000206 class CWriter : public InstVisitor<CWriter> {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000207 ostream& Out;
208 SlotCalculator &Table;
209 const Module *TheModule;
210 map<const Type *, string> TypeNames;
Chris Lattner78771c82002-05-17 04:55:35 +0000211 std::set<const Value*> MangledGlobals;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000212 public:
213 inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M)
214 : Out(o), Table(Tab), TheModule(M) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000215 }
216
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000217 inline void write(Module *M) { printModule(M); }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000218
Chris Lattner3af3ba82002-05-09 21:31:18 +0000219 ostream& printType(const Type *Ty, const string &VariableName = "") {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000220 return Out << calcTypeNameVar(Ty, TypeNames, VariableName);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000221 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000222
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000223 void writeOperand(Value *Operand);
224 void writeOperandInternal(Value *Operand);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000225
226 string getValueName(const Value *V);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000227
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000228 private :
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000229 void printModule(Module *M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000230 void printSymbolTable(const SymbolTable &ST);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000231 void printGlobal(const GlobalVariable *GV);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000232 void printFunctionSignature(const Function *F);
233 void printFunctionDecl(const Function *F); // Print just the forward decl
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000234
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000235 void printFunction(Function *);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000236
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000237 // isInlinableInst - Attempt to inline instructions into their uses to build
238 // trees as much as possible. To do this, we have to consistently decide
239 // what is acceptable to inline, so that variable declarations don't get
240 // printed and an extra copy of the expr is not emitted.
241 //
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000242 static bool isInlinableInst(const Instruction &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000243 // Must be an expression, must be used exactly once. If it is dead, we
244 // emit it inline where it would go.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000245 if (I.getType() == Type::VoidTy || I.use_size() != 1 ||
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000246 isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I))
247 return false;
248
249 // Only inline instruction it it's use is in the same BB as the inst.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000250 return I.getParent() == cast<Instruction>(I.use_back())->getParent();
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000251 }
252
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000253 // Instruction visitation functions
254 friend class InstVisitor<CWriter>;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000255
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000256 void visitReturnInst(ReturnInst &I);
257 void visitBranchInst(BranchInst &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000258
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000259 void visitPHINode(PHINode &I) {}
260 void visitNot(GenericUnaryInst &I);
261 void visitBinaryOperator(Instruction &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000262
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000263 void visitCastInst (CastInst &I);
264 void visitCallInst (CallInst &I);
265 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000266
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000267 void visitMallocInst(MallocInst &I);
268 void visitAllocaInst(AllocaInst &I);
269 void visitFreeInst (FreeInst &I);
270 void visitLoadInst (LoadInst &I);
271 void visitStoreInst (StoreInst &I);
272 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000273
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000274 void visitInstruction(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000275 cerr << "C Writer does not know about " << I;
276 abort();
277 }
278
279 void outputLValue(Instruction *I) {
280 Out << " " << getValueName(I) << " = ";
281 }
282 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
283 unsigned Indent);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000284 void printIndexingExpr(MemAccessInst &MAI);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000285 };
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000286}
287
Chris Lattner2f499022002-05-09 04:21:21 +0000288// We dont want identifier names with ., space, - in them.
289// So we replace them with _
290static string makeNameProper(string x) {
291 string tmp;
292 for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
293 switch (*sI) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000294 case '.': tmp += "d_"; break;
295 case ' ': tmp += "s_"; break;
296 case '-': tmp += "D_"; break;
Chris Lattner2f499022002-05-09 04:21:21 +0000297 default: tmp += *sI;
298 }
299
300 return tmp;
301}
302
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000303string CWriter::getValueName(const Value *V) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000304 if (V->hasName()) { // Print out the label if it exists...
305 if (isa<GlobalValue>(V) && // Do not mangle globals...
306 cast<GlobalValue>(V)->hasExternalLinkage() && // Unless it's internal or
307 !MangledGlobals.count(V)) // Unless the name would collide if we don't
Chris Lattner2f499022002-05-09 04:21:21 +0000308 return makeNameProper(V->getName());
309
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000310 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
311 makeNameProper(V->getName());
Chris Lattner2f499022002-05-09 04:21:21 +0000312 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000313
314 int Slot = Table.getValSlot(V);
315 assert(Slot >= 0 && "Invalid value!");
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000316 return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000317}
318
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000319void CWriter::writeOperandInternal(Value *Operand) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000320 if (Operand->hasName()) {
321 Out << getValueName(Operand);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000322 } else if (Constant *CPV = dyn_cast<Constant>(Operand)) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000323 if (isa<ConstantPointerNull>(CPV)) {
324 Out << "((";
Chris Lattner3af3ba82002-05-09 21:31:18 +0000325 printType(CPV->getType(), "");
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000326 Out << ")NULL)";
327 } else
328 Out << getConstStrValue(CPV);
329 } else {
330 int Slot = Table.getValSlot(Operand);
331 assert(Slot >= 0 && "Malformed LLVM!");
332 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
333 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000334}
335
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000336void CWriter::writeOperand(Value *Operand) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000337 if (Instruction *I = dyn_cast<Instruction>(Operand))
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000338 if (isInlinableInst(*I)) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000339 // Should we inline this instruction to build a tree?
340 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000341 visit(*I);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000342 Out << ")";
343 return;
344 }
345
346 if (isa<GlobalVariable>(Operand))
347 Out << "(&"; // Global variables are references as their addresses by llvm
348
349 writeOperandInternal(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000350
351 if (isa<GlobalVariable>(Operand))
352 Out << ")";
353}
354
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000355void CWriter::printModule(Module *M) {
Chris Lattner78771c82002-05-17 04:55:35 +0000356 // Calculate which global values have names that will collide when we throw
357 // away type information.
Chris Lattner594b9fa2002-05-21 21:10:04 +0000358 { // Scope to delete the FoundNames set when we are done with it...
Chris Lattner78771c82002-05-17 04:55:35 +0000359 std::set<string> FoundNames;
360 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000361 if (I->hasName()) // If the global has a name...
362 if (FoundNames.count(I->getName())) // And the name is already used
363 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000364 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000365 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000366
367 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000368 if (I->hasName()) // If the global has a name...
369 if (FoundNames.count(I->getName())) // And the name is already used
370 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000371 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000372 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000373 }
374
375
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000376 // printing stdlib inclusion
377 // Out << "#include <stdlib.h>\n";
378
Chris Lattner16c7bb22002-05-09 02:28:59 +0000379 // get declaration for alloca
380 Out << "/* Provide Declarations */\n"
Chris Lattner594b9fa2002-05-21 21:10:04 +0000381 << "#include <malloc.h>\n"
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000382 << "#include <alloca.h>\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000383
384 // Provide a definition for null if one does not already exist.
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000385 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000386 << "typedef unsigned char bool;\n"
387
388 << "\n\n/* Global Symbols */\n";
389
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000390 // Loop over the symbol table, emitting all named constants...
391 if (M->hasSymbolTable())
392 printSymbolTable(*M->getSymbolTable());
393
Chris Lattner16c7bb22002-05-09 02:28:59 +0000394 Out << "\n\n/* Global Data */\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000395 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
396 if (I->hasInternalLinkage()) Out << "static ";
397 printType(I->getType()->getElementType(), getValueName(I));
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000398
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000399 if (I->hasInitializer()) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000400 Out << " = " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000401 writeOperand(I->getInitializer());
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000402 }
403 Out << ";\n";
404 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000405
Chris Lattner16c7bb22002-05-09 02:28:59 +0000406 // First output all the declarations of the functions as C requires Functions
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000407 // be declared before they are used.
Chris Lattner16c7bb22002-05-09 02:28:59 +0000408 //
409 Out << "\n\n/* Function Declarations */\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000410 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
411 printFunctionDecl(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000412
Chris Lattner16c7bb22002-05-09 02:28:59 +0000413 // Output all of the functions...
414 Out << "\n\n/* Function Bodies */\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000415 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
416 printFunction(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000417}
418
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000419
420// printSymbolTable - Run through symbol table looking for named constants
421// if a named constant is found, emit it's declaration...
422// Assuming that symbol table has only types and constants.
423void CWriter::printSymbolTable(const SymbolTable &ST) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000424 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
425 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
426 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
427
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000428 for (; I != End; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000429 if (const Type *Ty = dyn_cast<StructType>(I->second)) {
430 string Name = "struct l_" + makeNameProper(I->first);
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000431 Out << Name << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000432
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000433 TypeNames.insert(std::make_pair(Ty, Name));
434 }
435 }
436
437 Out << "\n";
438
439 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
440 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
441 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
442
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000443 for (; I != End; ++I) {
444 const Value *V = I->second;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000445 if (const Type *Ty = dyn_cast<Type>(V)) {
446 string Name = "l_" + makeNameProper(I->first);
Chris Lattner1f02c892002-05-09 20:14:10 +0000447 if (isa<StructType>(Ty))
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000448 Name = "struct " + makeNameProper(Name);
Chris Lattner1f02c892002-05-09 20:14:10 +0000449 else
450 Out << "typedef ";
451
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000452 Out << calcTypeNameVar(Ty, TypeNames, Name, true) << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000453 }
454 }
455 }
456}
457
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000458
Chris Lattner16c7bb22002-05-09 02:28:59 +0000459// printFunctionDecl - Print function declaration
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000460//
Chris Lattner16c7bb22002-05-09 02:28:59 +0000461void CWriter::printFunctionDecl(const Function *F) {
462 printFunctionSignature(F);
463 Out << ";\n";
464}
465
466void CWriter::printFunctionSignature(const Function *F) {
467 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000468
469 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000470 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000471
Chris Lattner16c7bb22002-05-09 02:28:59 +0000472 // Print out the return type and name...
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000473 printType(F->getReturnType());
Chris Lattner1f02c892002-05-09 20:14:10 +0000474 Out << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000475
Chris Lattner16c7bb22002-05-09 02:28:59 +0000476 if (!F->isExternal()) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000477 if (!F->aempty()) {
478 printType(F->afront().getType(), getValueName(F->abegin()));
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000479
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000480 for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
481 I != E; ++I) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000482 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000483 printType(I->getType(), getValueName(I));
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000484 }
485 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000486 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000487 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000488 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000489 FT->getParamTypes().begin(),
490 E = FT->getParamTypes().end(); I != E; ++I) {
491 if (I != FT->getParamTypes().begin()) Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000492 printType(*I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000493 }
494 }
495
496 // Finish printing arguments...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000497 if (FT->isVarArg()) {
498 if (FT->getParamTypes().size()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000499 Out << "..."; // Output varargs portion of signature!
500 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000501 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000502}
503
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000504
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000505void CWriter::printFunction(Function *F) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000506 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000507
Chris Lattnerf34ee812002-05-09 03:12:34 +0000508 Table.incorporateFunction(F);
509
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000510 printFunctionSignature(F);
511 Out << " {\n";
512
Chris Lattner497e19a2002-05-09 20:39:03 +0000513 // print local variable information for the function
Chris Lattner7683a122002-05-09 20:33:35 +0000514 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000515 if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000516 Out << " ";
Chris Lattner3af3ba82002-05-09 21:31:18 +0000517 printType((*I)->getType(), getValueName(*I));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000518 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000519 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000520
521 // print the basic blocks
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000522 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
523 BasicBlock *Prev = BB->getPrev();
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000524
525 // Don't print the label for the basic block if there are no uses, or if the
526 // only terminator use is the precessor basic block's terminator. We have
527 // to scan the use list because PHI nodes use basic blocks too but do not
528 // require a label to be generated.
529 //
530 bool NeedsLabel = false;
531 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
532 UI != UE; ++UI)
533 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
534 if (TI != Prev->getTerminator()) {
535 NeedsLabel = true;
536 break;
537 }
538
539 if (NeedsLabel) Out << getValueName(BB) << ":\n";
540
541 // Output all of the instructions in the basic block...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000542 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II){
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000543 if (!isInlinableInst(*II) && !isa<PHINode>(*II)) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000544 if (II->getType() != Type::VoidTy)
545 outputLValue(II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000546 else
547 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000548 visit(*II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000549 Out << ";\n";
550 }
551 }
552
553 // Don't emit prefix or suffix for the terminator...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000554 visit(*BB->getTerminator());
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000555 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000556
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000557 Out << "}\n\n";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000558 Table.purgeFunction();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000559}
560
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000561// Specific Instruction type classes... note that all of the casts are
562// neccesary because we use the instruction classes as opaque types...
563//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000564void CWriter::visitReturnInst(ReturnInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000565 // Don't output a void return if this is the last basic block in the function
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000566 if (I.getNumOperands() == 0 &&
567 &*--I.getParent()->getParent()->end() == I.getParent() &&
568 !I.getParent()->size() == 1) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000569 return;
Chris Lattner963b70b2002-05-21 18:05:19 +0000570 }
Chris Lattner44408262002-05-09 03:50:42 +0000571
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000572 Out << " return";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000573 if (I.getNumOperands()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000574 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000575 writeOperand(I.getOperand(0));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000576 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000577 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000578}
579
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000580static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
581 // If PHI nodes need copies, we need the copy code...
582 if (isa<PHINode>(To->front()) ||
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000583 From->getNext() != To) // Not directly successor, need goto
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000584 return true;
585
586 // Otherwise we don't need the code.
587 return false;
588}
589
590void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
591 unsigned Indent) {
592 for (BasicBlock::iterator I = Succ->begin();
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000593 PHINode *PN = dyn_cast<PHINode>(&*I); ++I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000594 // now we have to do the printing
595 Out << string(Indent, ' ');
596 outputLValue(PN);
597 writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
598 Out << "; /* for PHI node */\n";
599 }
600
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000601 if (CurBB->getNext() != Succ) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000602 Out << string(Indent, ' ') << " goto ";
603 writeOperand(Succ);
604 Out << ";\n";
605 }
606}
607
608// Brach instruction printing - Avoid printing out a brach to a basic block that
609// immediately succeeds the current one.
610//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000611void CWriter::visitBranchInst(BranchInst &I) {
612 if (I.isConditional()) {
613 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(0))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000614 Out << " if (";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000615 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000616 Out << ") {\n";
617
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000618 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000619
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000620 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(1))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000621 Out << " } else {\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000622 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000623 }
624 } else {
625 // First goto not neccesary, assume second one is...
626 Out << " if (!";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000627 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000628 Out << ") {\n";
629
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000630 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000631 }
632
633 Out << " }\n";
634 } else {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000635 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000636 }
637 Out << "\n";
638}
639
640
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000641void CWriter::visitNot(GenericUnaryInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000642 Out << "~";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000643 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000644}
645
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000646void CWriter::visitBinaryOperator(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000647 // binary instructions, shift instructions, setCond instructions.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000648 if (isa<PointerType>(I.getType())) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000649 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000650 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000651 Out << ")";
652 }
653
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000654 if (isa<PointerType>(I.getType())) Out << "(long long)";
655 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000656
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000657 switch (I.getOpcode()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000658 case Instruction::Add: Out << " + "; break;
659 case Instruction::Sub: Out << " - "; break;
660 case Instruction::Mul: Out << "*"; break;
661 case Instruction::Div: Out << "/"; break;
662 case Instruction::Rem: Out << "%"; break;
663 case Instruction::And: Out << " & "; break;
664 case Instruction::Or: Out << " | "; break;
665 case Instruction::Xor: Out << " ^ "; break;
666 case Instruction::SetEQ: Out << " == "; break;
667 case Instruction::SetNE: Out << " != "; break;
668 case Instruction::SetLE: Out << " <= "; break;
669 case Instruction::SetGE: Out << " >= "; break;
670 case Instruction::SetLT: Out << " < "; break;
671 case Instruction::SetGT: Out << " > "; break;
672 case Instruction::Shl : Out << " << "; break;
673 case Instruction::Shr : Out << " >> "; break;
674 default: cerr << "Invalid operator type!" << I; abort();
675 }
676
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000677 if (isa<PointerType>(I.getType())) Out << "(long long)";
678 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000679}
680
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000681void CWriter::visitCastInst(CastInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000682 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000683 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000684 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000685 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000686}
687
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000688void CWriter::visitCallInst(CallInst &I) {
689 const PointerType *PTy = cast<PointerType>(I.getCalledValue()->getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000690 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
691 const Type *RetTy = FTy->getReturnType();
692
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000693 Out << getValueName(I.getOperand(0)) << "(";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000694
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000695 if (I.getNumOperands() > 1) {
696 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000697
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000698 for (unsigned op = 2, Eop = I.getNumOperands(); op != Eop; ++op) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000699 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000700 writeOperand(I.getOperand(op));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000701 }
702 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000703 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000704}
705
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000706void CWriter::visitMallocInst(MallocInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000707 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000708 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000709 Out << ")malloc(sizeof(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000710 printType(I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000711 Out << ")";
712
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000713 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000714 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000715 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000716 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000717 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000718}
719
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000720void CWriter::visitAllocaInst(AllocaInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000721 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000722 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000723 Out << ") alloca(sizeof(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000724 printType(I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000725 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000726 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000727 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000728 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000729 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000730 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000731}
732
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000733void CWriter::visitFreeInst(FreeInst &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000734 Out << "free(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000735 writeOperand(I.getOperand(0));
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000736 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000737}
738
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000739void CWriter::printIndexingExpr(MemAccessInst &MAI) {
740 MemAccessInst::op_iterator I = MAI.idx_begin(), E = MAI.idx_end();
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000741 if (I == E) {
742 // If accessing a global value with no indexing, avoid *(&GV) syndrome
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000743 if (GlobalValue *V = dyn_cast<GlobalValue>(MAI.getPointerOperand())) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000744 writeOperandInternal(V);
745 return;
746 }
747
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000748 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000749 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000750
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000751 writeOperand(MAI.getPointerOperand());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000752
753 if (I == E) return;
754
755 // Print out the -> operator if possible...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000756 const Constant *CI = dyn_cast<Constant>(I->get());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000757 if (CI && CI->isNullValue() && I+1 != E &&
758 (*(I+1))->getType() == Type::UByteTy) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000759 Out << "->field" << cast<ConstantUInt>(*(I+1))->getValue();
760 I += 2;
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000761 }
762
763 for (; I != E; ++I)
764 if ((*I)->getType() == Type::UIntTy) {
765 Out << "[";
766 writeOperand(*I);
767 Out << "]";
768 } else {
769 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
770 }
771}
772
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000773void CWriter::visitLoadInst(LoadInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000774 printIndexingExpr(I);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000775}
776
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000777void CWriter::visitStoreInst(StoreInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000778 printIndexingExpr(I);
779 Out << " = ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000780 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000781}
782
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000783void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000784 Out << "&";
785 printIndexingExpr(I);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000786}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000787
788//===----------------------------------------------------------------------===//
789// External Interface declaration
790//===----------------------------------------------------------------------===//
791
Chris Lattnerf34ee812002-05-09 03:12:34 +0000792void WriteToC(const Module *M, ostream &Out) {
793 assert(M && "You can't write a null module!!");
794 SlotCalculator SlotTable(M, false);
795 CWriter W(Out, SlotTable, M);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000796 W.write((Module*)M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000797 Out.flush();
798}