blob: dd88653449cb3c17d30ee0efe144c8dfdaec877b [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:
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000115 std::cerr << "Unknown constant type: " << CPV << "\n";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000116 abort();
Chris Lattner4b367212002-07-25 16:30:45 +0000117 return "";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000118 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000119}
120
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000121// Pass the Type* variable and and the variable name and this prints out the
122// variable declaration.
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000123//
Chris Lattner16c7bb22002-05-09 02:28:59 +0000124static string calcTypeNameVar(const Type *Ty,
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000125 map<const Type *, string> &TypeNames,
126 const string &NameSoFar, bool ignoreName = false){
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000127 if (Ty->isPrimitiveType())
128 switch (Ty->getPrimitiveID()) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000129 case Type::VoidTyID: return "void " + NameSoFar;
130 case Type::BoolTyID: return "bool " + NameSoFar;
131 case Type::UByteTyID: return "unsigned char " + NameSoFar;
132 case Type::SByteTyID: return "signed char " + NameSoFar;
133 case Type::UShortTyID: return "unsigned short " + NameSoFar;
134 case Type::ShortTyID: return "short " + NameSoFar;
135 case Type::UIntTyID: return "unsigned " + NameSoFar;
136 case Type::IntTyID: return "int " + NameSoFar;
137 case Type::ULongTyID: return "unsigned long long " + NameSoFar;
138 case Type::LongTyID: return "signed long long " + NameSoFar;
Chris Lattner1f02c892002-05-09 20:14:10 +0000139 case Type::FloatTyID: return "float " + NameSoFar;
140 case Type::DoubleTyID: return "double " + NameSoFar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000141 default :
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000142 std::cerr << "Unknown primitive type: " << Ty << "\n";
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000143 abort();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000144 }
145
146 // Check to see if the type is named.
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000147 if (!ignoreName) {
148 map<const Type *, string>::iterator I = TypeNames.find(Ty);
149 if (I != TypeNames.end())
150 return I->second + " " + NameSoFar;
151 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000152
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000153 string Result;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000154 switch (Ty->getPrimitiveID()) {
155 case Type::FunctionTyID: {
Chris Lattner7683a122002-05-09 20:33:35 +0000156 const FunctionType *MTy = cast<FunctionType>(Ty);
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000157 Result += calcTypeNameVar(MTy->getReturnType(), TypeNames, "");
Chris Lattner7683a122002-05-09 20:33:35 +0000158 Result += " " + NameSoFar + " (";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000159 for (FunctionType::ParamTypes::const_iterator
160 I = MTy->getParamTypes().begin(),
161 E = MTy->getParamTypes().end(); I != E; ++I) {
162 if (I != MTy->getParamTypes().begin())
163 Result += ", ";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000164 Result += calcTypeNameVar(*I, TypeNames, "");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000165 }
166 if (MTy->isVarArg()) {
167 if (!MTy->getParamTypes().empty())
168 Result += ", ";
169 Result += "...";
170 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000171 return Result + ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000172 }
173 case Type::StructTyID: {
174 const StructType *STy = cast<const StructType>(Ty);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000175 Result = NameSoFar + " {\n";
176 unsigned indx = 0;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000177 for (StructType::ElementTypes::const_iterator
178 I = STy->getElementTypes().begin(),
179 E = STy->getElementTypes().end(); I != E; ++I) {
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000180 Result += " " +calcTypeNameVar(*I, TypeNames, "field" + utostr(indx++));
181 Result += ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000182 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000183 return Result + "}";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000184 }
185
Chris Lattner3af3ba82002-05-09 21:31:18 +0000186 case Type::PointerTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000187 return calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(),
188 TypeNames, "*" + NameSoFar);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000189
190 case Type::ArrayTyID: {
191 const ArrayType *ATy = cast<const ArrayType>(Ty);
192 int NumElements = ATy->getNumElements();
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000193 return calcTypeNameVar(ATy->getElementType(), TypeNames,
194 NameSoFar + "[" + itostr(NumElements) + "]");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000195 }
196 default:
197 assert(0 && "Unhandled case in getTypeProps!");
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000198 abort();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000199 }
200
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000201 return Result;
202}
203
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000204namespace {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000205 class CWriter : public InstVisitor<CWriter> {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000206 ostream& Out;
207 SlotCalculator &Table;
208 const Module *TheModule;
209 map<const Type *, string> TypeNames;
Chris Lattner78771c82002-05-17 04:55:35 +0000210 std::set<const Value*> MangledGlobals;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000211 public:
212 inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M)
213 : Out(o), Table(Tab), TheModule(M) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000214 }
215
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000216 inline void write(Module *M) { printModule(M); }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000217
Chris Lattner3af3ba82002-05-09 21:31:18 +0000218 ostream& printType(const Type *Ty, const string &VariableName = "") {
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000219 return Out << calcTypeNameVar(Ty, TypeNames, VariableName);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000220 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000221
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000222 void writeOperand(Value *Operand);
223 void writeOperandInternal(Value *Operand);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000224
225 string getValueName(const Value *V);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000226
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000227 private :
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000228 void printModule(Module *M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000229 void printSymbolTable(const SymbolTable &ST);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000230 void printGlobal(const GlobalVariable *GV);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000231 void printFunctionSignature(const Function *F);
232 void printFunctionDecl(const Function *F); // Print just the forward decl
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000233
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000234 void printFunction(Function *);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000235
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000236 // isInlinableInst - Attempt to inline instructions into their uses to build
237 // trees as much as possible. To do this, we have to consistently decide
238 // what is acceptable to inline, so that variable declarations don't get
239 // printed and an extra copy of the expr is not emitted.
240 //
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000241 static bool isInlinableInst(const Instruction &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000242 // Must be an expression, must be used exactly once. If it is dead, we
243 // emit it inline where it would go.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000244 if (I.getType() == Type::VoidTy || I.use_size() != 1 ||
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000245 isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I))
246 return false;
247
248 // Only inline instruction it it's use is in the same BB as the inst.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000249 return I.getParent() == cast<Instruction>(I.use_back())->getParent();
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000250 }
251
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000252 // Instruction visitation functions
253 friend class InstVisitor<CWriter>;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000254
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000255 void visitReturnInst(ReturnInst &I);
256 void visitBranchInst(BranchInst &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000257
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000258 void visitPHINode(PHINode &I) {}
259 void visitNot(GenericUnaryInst &I);
260 void visitBinaryOperator(Instruction &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000261
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000262 void visitCastInst (CastInst &I);
263 void visitCallInst (CallInst &I);
264 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000265
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000266 void visitMallocInst(MallocInst &I);
267 void visitAllocaInst(AllocaInst &I);
268 void visitFreeInst (FreeInst &I);
269 void visitLoadInst (LoadInst &I);
270 void visitStoreInst (StoreInst &I);
271 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000272
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000273 void visitInstruction(Instruction &I) {
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000274 std::cerr << "C Writer does not know about " << I;
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000275 abort();
276 }
277
278 void outputLValue(Instruction *I) {
279 Out << " " << getValueName(I) << " = ";
280 }
281 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
282 unsigned Indent);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000283 void printIndexingExpr(MemAccessInst &MAI);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000284 };
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000285}
286
Chris Lattner2f499022002-05-09 04:21:21 +0000287// We dont want identifier names with ., space, - in them.
288// So we replace them with _
289static string makeNameProper(string x) {
290 string tmp;
291 for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
292 switch (*sI) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000293 case '.': tmp += "d_"; break;
294 case ' ': tmp += "s_"; break;
295 case '-': tmp += "D_"; break;
Chris Lattner2f499022002-05-09 04:21:21 +0000296 default: tmp += *sI;
297 }
298
299 return tmp;
300}
301
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000302string CWriter::getValueName(const Value *V) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000303 if (V->hasName()) { // Print out the label if it exists...
304 if (isa<GlobalValue>(V) && // Do not mangle globals...
305 cast<GlobalValue>(V)->hasExternalLinkage() && // Unless it's internal or
306 !MangledGlobals.count(V)) // Unless the name would collide if we don't
Chris Lattner2f499022002-05-09 04:21:21 +0000307 return makeNameProper(V->getName());
308
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000309 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
310 makeNameProper(V->getName());
Chris Lattner2f499022002-05-09 04:21:21 +0000311 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000312
313 int Slot = Table.getValSlot(V);
314 assert(Slot >= 0 && "Invalid value!");
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000315 return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000316}
317
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000318void CWriter::writeOperandInternal(Value *Operand) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000319 if (Operand->hasName()) {
320 Out << getValueName(Operand);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000321 } else if (Constant *CPV = dyn_cast<Constant>(Operand)) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000322 if (isa<ConstantPointerNull>(CPV)) {
323 Out << "((";
Chris Lattner3af3ba82002-05-09 21:31:18 +0000324 printType(CPV->getType(), "");
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000325 Out << ")NULL)";
326 } else
327 Out << getConstStrValue(CPV);
328 } else {
329 int Slot = Table.getValSlot(Operand);
330 assert(Slot >= 0 && "Malformed LLVM!");
331 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
332 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000333}
334
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000335void CWriter::writeOperand(Value *Operand) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000336 if (Instruction *I = dyn_cast<Instruction>(Operand))
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000337 if (isInlinableInst(*I)) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000338 // Should we inline this instruction to build a tree?
339 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000340 visit(*I);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000341 Out << ")";
342 return;
343 }
344
345 if (isa<GlobalVariable>(Operand))
346 Out << "(&"; // Global variables are references as their addresses by llvm
347
348 writeOperandInternal(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000349
350 if (isa<GlobalVariable>(Operand))
351 Out << ")";
352}
353
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000354void CWriter::printModule(Module *M) {
Chris Lattner78771c82002-05-17 04:55:35 +0000355 // Calculate which global values have names that will collide when we throw
356 // away type information.
Chris Lattner594b9fa2002-05-21 21:10:04 +0000357 { // Scope to delete the FoundNames set when we are done with it...
Chris Lattner78771c82002-05-17 04:55:35 +0000358 std::set<string> FoundNames;
359 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000360 if (I->hasName()) // If the global has a name...
361 if (FoundNames.count(I->getName())) // And the name is already used
362 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000363 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000364 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000365
366 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000367 if (I->hasName()) // If the global has a name...
368 if (FoundNames.count(I->getName())) // And the name is already used
369 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000370 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000371 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000372 }
373
374
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000375 // printing stdlib inclusion
376 // Out << "#include <stdlib.h>\n";
377
Chris Lattner16c7bb22002-05-09 02:28:59 +0000378 // get declaration for alloca
379 Out << "/* Provide Declarations */\n"
Chris Lattner594b9fa2002-05-21 21:10:04 +0000380 << "#include <malloc.h>\n"
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000381 << "#include <alloca.h>\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000382
383 // Provide a definition for null if one does not already exist.
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000384 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000385 << "typedef unsigned char bool;\n"
386
387 << "\n\n/* Global Symbols */\n";
388
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000389 // Loop over the symbol table, emitting all named constants...
390 if (M->hasSymbolTable())
391 printSymbolTable(*M->getSymbolTable());
392
Chris Lattner16c7bb22002-05-09 02:28:59 +0000393 Out << "\n\n/* Global Data */\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000394 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
395 if (I->hasInternalLinkage()) Out << "static ";
396 printType(I->getType()->getElementType(), getValueName(I));
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000397
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000398 if (I->hasInitializer()) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000399 Out << " = " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000400 writeOperand(I->getInitializer());
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000401 }
402 Out << ";\n";
403 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000404
Chris Lattner16c7bb22002-05-09 02:28:59 +0000405 // First output all the declarations of the functions as C requires Functions
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000406 // be declared before they are used.
Chris Lattner16c7bb22002-05-09 02:28:59 +0000407 //
408 Out << "\n\n/* Function Declarations */\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000409 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
410 printFunctionDecl(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000411
Chris Lattner16c7bb22002-05-09 02:28:59 +0000412 // Output all of the functions...
413 Out << "\n\n/* Function Bodies */\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000414 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
415 printFunction(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000416}
417
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000418
419// printSymbolTable - Run through symbol table looking for named constants
420// if a named constant is found, emit it's declaration...
421// Assuming that symbol table has only types and constants.
422void CWriter::printSymbolTable(const SymbolTable &ST) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000423 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
424 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
425 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
426
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000427 for (; I != End; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000428 if (const Type *Ty = dyn_cast<StructType>(I->second)) {
429 string Name = "struct l_" + makeNameProper(I->first);
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000430 Out << Name << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000431
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000432 TypeNames.insert(std::make_pair(Ty, Name));
433 }
434 }
435
436 Out << "\n";
437
438 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
439 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
440 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
441
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000442 for (; I != End; ++I) {
443 const Value *V = I->second;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000444 if (const Type *Ty = dyn_cast<Type>(V)) {
445 string Name = "l_" + makeNameProper(I->first);
Chris Lattner1f02c892002-05-09 20:14:10 +0000446 if (isa<StructType>(Ty))
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000447 Name = "struct " + makeNameProper(Name);
Chris Lattner1f02c892002-05-09 20:14:10 +0000448 else
449 Out << "typedef ";
450
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000451 Out << calcTypeNameVar(Ty, TypeNames, Name, true) << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000452 }
453 }
454 }
455}
456
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000457
Chris Lattner16c7bb22002-05-09 02:28:59 +0000458// printFunctionDecl - Print function declaration
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000459//
Chris Lattner16c7bb22002-05-09 02:28:59 +0000460void CWriter::printFunctionDecl(const Function *F) {
461 printFunctionSignature(F);
462 Out << ";\n";
463}
464
465void CWriter::printFunctionSignature(const Function *F) {
466 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000467
468 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000469 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000470
Chris Lattner16c7bb22002-05-09 02:28:59 +0000471 // Print out the return type and name...
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000472 printType(F->getReturnType());
Chris Lattner1f02c892002-05-09 20:14:10 +0000473 Out << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000474
Chris Lattner16c7bb22002-05-09 02:28:59 +0000475 if (!F->isExternal()) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000476 if (!F->aempty()) {
477 printType(F->afront().getType(), getValueName(F->abegin()));
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000478
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000479 for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
480 I != E; ++I) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000481 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000482 printType(I->getType(), getValueName(I));
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000483 }
484 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000485 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000486 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000487 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000488 FT->getParamTypes().begin(),
489 E = FT->getParamTypes().end(); I != E; ++I) {
490 if (I != FT->getParamTypes().begin()) Out << ", ";
Chris Lattner2a7ab2e2002-05-09 04:39:00 +0000491 printType(*I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000492 }
493 }
494
495 // Finish printing arguments...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000496 if (FT->isVarArg()) {
497 if (FT->getParamTypes().size()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000498 Out << "..."; // Output varargs portion of signature!
499 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000500 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000501}
502
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000503
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000504void CWriter::printFunction(Function *F) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000505 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000506
Chris Lattnerf34ee812002-05-09 03:12:34 +0000507 Table.incorporateFunction(F);
508
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000509 printFunctionSignature(F);
510 Out << " {\n";
511
Chris Lattner497e19a2002-05-09 20:39:03 +0000512 // print local variable information for the function
Chris Lattner7683a122002-05-09 20:33:35 +0000513 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000514 if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000515 Out << " ";
Chris Lattner3af3ba82002-05-09 21:31:18 +0000516 printType((*I)->getType(), getValueName(*I));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000517 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000518 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000519
520 // print the basic blocks
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000521 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
522 BasicBlock *Prev = BB->getPrev();
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000523
524 // Don't print the label for the basic block if there are no uses, or if the
525 // only terminator use is the precessor basic block's terminator. We have
526 // to scan the use list because PHI nodes use basic blocks too but do not
527 // require a label to be generated.
528 //
529 bool NeedsLabel = false;
530 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
531 UI != UE; ++UI)
532 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
533 if (TI != Prev->getTerminator()) {
534 NeedsLabel = true;
535 break;
536 }
537
538 if (NeedsLabel) Out << getValueName(BB) << ":\n";
539
540 // Output all of the instructions in the basic block...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000541 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II){
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000542 if (!isInlinableInst(*II) && !isa<PHINode>(*II)) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000543 if (II->getType() != Type::VoidTy)
544 outputLValue(II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000545 else
546 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000547 visit(*II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000548 Out << ";\n";
549 }
550 }
551
552 // Don't emit prefix or suffix for the terminator...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000553 visit(*BB->getTerminator());
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000554 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000555
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000556 Out << "}\n\n";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000557 Table.purgeFunction();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000558}
559
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000560// Specific Instruction type classes... note that all of the casts are
561// neccesary because we use the instruction classes as opaque types...
562//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000563void CWriter::visitReturnInst(ReturnInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000564 // Don't output a void return if this is the last basic block in the function
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000565 if (I.getNumOperands() == 0 &&
566 &*--I.getParent()->getParent()->end() == I.getParent() &&
567 !I.getParent()->size() == 1) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000568 return;
Chris Lattner963b70b2002-05-21 18:05:19 +0000569 }
Chris Lattner44408262002-05-09 03:50:42 +0000570
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000571 Out << " return";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000572 if (I.getNumOperands()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000573 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000574 writeOperand(I.getOperand(0));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000575 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000576 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000577}
578
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000579static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
580 // If PHI nodes need copies, we need the copy code...
581 if (isa<PHINode>(To->front()) ||
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000582 From->getNext() != To) // Not directly successor, need goto
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000583 return true;
584
585 // Otherwise we don't need the code.
586 return false;
587}
588
589void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
590 unsigned Indent) {
591 for (BasicBlock::iterator I = Succ->begin();
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000592 PHINode *PN = dyn_cast<PHINode>(&*I); ++I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000593 // now we have to do the printing
594 Out << string(Indent, ' ');
595 outputLValue(PN);
596 writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
597 Out << "; /* for PHI node */\n";
598 }
599
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000600 if (CurBB->getNext() != Succ) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000601 Out << string(Indent, ' ') << " goto ";
602 writeOperand(Succ);
603 Out << ";\n";
604 }
605}
606
607// Brach instruction printing - Avoid printing out a brach to a basic block that
608// immediately succeeds the current one.
609//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000610void CWriter::visitBranchInst(BranchInst &I) {
611 if (I.isConditional()) {
612 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(0))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000613 Out << " if (";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000614 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000615 Out << ") {\n";
616
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000617 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000618
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000619 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(1))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000620 Out << " } else {\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000621 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000622 }
623 } else {
624 // First goto not neccesary, assume second one is...
625 Out << " if (!";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000626 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000627 Out << ") {\n";
628
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000629 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000630 }
631
632 Out << " }\n";
633 } else {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000634 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000635 }
636 Out << "\n";
637}
638
639
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000640void CWriter::visitNot(GenericUnaryInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000641 Out << "~";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000642 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000643}
644
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000645void CWriter::visitBinaryOperator(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000646 // binary instructions, shift instructions, setCond instructions.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000647 if (isa<PointerType>(I.getType())) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000648 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000649 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000650 Out << ")";
651 }
652
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000653 if (isa<PointerType>(I.getType())) Out << "(long long)";
654 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000655
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000656 switch (I.getOpcode()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000657 case Instruction::Add: Out << " + "; break;
658 case Instruction::Sub: Out << " - "; break;
659 case Instruction::Mul: Out << "*"; break;
660 case Instruction::Div: Out << "/"; break;
661 case Instruction::Rem: Out << "%"; break;
662 case Instruction::And: Out << " & "; break;
663 case Instruction::Or: Out << " | "; break;
664 case Instruction::Xor: Out << " ^ "; break;
665 case Instruction::SetEQ: Out << " == "; break;
666 case Instruction::SetNE: Out << " != "; break;
667 case Instruction::SetLE: Out << " <= "; break;
668 case Instruction::SetGE: Out << " >= "; break;
669 case Instruction::SetLT: Out << " < "; break;
670 case Instruction::SetGT: Out << " > "; break;
671 case Instruction::Shl : Out << " << "; break;
672 case Instruction::Shr : Out << " >> "; break;
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000673 default: std::cerr << "Invalid operator type!" << I; abort();
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000674 }
675
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000676 if (isa<PointerType>(I.getType())) Out << "(long long)";
677 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000678}
679
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000680void CWriter::visitCastInst(CastInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000681 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000682 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000683 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000684 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000685}
686
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000687void CWriter::visitCallInst(CallInst &I) {
688 const PointerType *PTy = cast<PointerType>(I.getCalledValue()->getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000689 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
690 const Type *RetTy = FTy->getReturnType();
691
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000692 Out << getValueName(I.getOperand(0)) << "(";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000693
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000694 if (I.getNumOperands() > 1) {
695 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000696
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000697 for (unsigned op = 2, Eop = I.getNumOperands(); op != Eop; ++op) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000698 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000699 writeOperand(I.getOperand(op));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000700 }
701 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000702 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000703}
704
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000705void CWriter::visitMallocInst(MallocInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000706 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000707 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000708 Out << ")malloc(sizeof(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000709 printType(I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000710 Out << ")";
711
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000712 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000713 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000714 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000715 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000716 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000717}
718
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000719void CWriter::visitAllocaInst(AllocaInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000720 Out << "(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000721 printType(I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000722 Out << ") alloca(sizeof(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000723 printType(I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000724 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000725 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000726 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000727 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000728 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000729 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000730}
731
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000732void CWriter::visitFreeInst(FreeInst &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000733 Out << "free(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000734 writeOperand(I.getOperand(0));
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000735 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000736}
737
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000738void CWriter::printIndexingExpr(MemAccessInst &MAI) {
739 MemAccessInst::op_iterator I = MAI.idx_begin(), E = MAI.idx_end();
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000740 if (I == E) {
741 // If accessing a global value with no indexing, avoid *(&GV) syndrome
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000742 if (GlobalValue *V = dyn_cast<GlobalValue>(MAI.getPointerOperand())) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000743 writeOperandInternal(V);
744 return;
745 }
746
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000747 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000748 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000749
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000750 writeOperand(MAI.getPointerOperand());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000751
752 if (I == E) return;
753
754 // Print out the -> operator if possible...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000755 const Constant *CI = dyn_cast<Constant>(I->get());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000756 if (CI && CI->isNullValue() && I+1 != E &&
757 (*(I+1))->getType() == Type::UByteTy) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000758 Out << "->field" << cast<ConstantUInt>(*(I+1))->getValue();
759 I += 2;
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000760 }
761
762 for (; I != E; ++I)
763 if ((*I)->getType() == Type::UIntTy) {
764 Out << "[";
765 writeOperand(*I);
766 Out << "]";
767 } else {
768 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
769 }
770}
771
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000772void CWriter::visitLoadInst(LoadInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000773 printIndexingExpr(I);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000774}
775
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000776void CWriter::visitStoreInst(StoreInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000777 printIndexingExpr(I);
778 Out << " = ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000779 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000780}
781
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000782void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000783 Out << "&";
784 printIndexingExpr(I);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000785}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000786
787//===----------------------------------------------------------------------===//
788// External Interface declaration
789//===----------------------------------------------------------------------===//
790
Chris Lattnerf34ee812002-05-09 03:12:34 +0000791void WriteToC(const Module *M, ostream &Out) {
792 assert(M && "You can't write a null module!!");
793 SlotCalculator SlotTable(M, false);
794 CWriter W(Out, SlotTable, M);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000795 W.write((Module*)M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000796 Out.flush();
797}