blob: 59a2fd9b967339127b8b3c00bbc954dc38e7d174 [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//
Chris Lattnerd1cf1b42002-09-20 23:26:33 +00003// This library converts LLVM code to C code, compilable by GCC.
Chris Lattner16c7bb22002-05-09 02:28:59 +00004//
Chris Lattneredd8ce12003-05-03 03:14:35 +00005//===----------------------------------------------------------------------===//
Chris Lattner84e66652003-05-03 07:11:00 +00006
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00007#include "llvm/Assembly/CWriter.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00008#include "llvm/Constants.h"
Chris Lattner2f5f51a2002-05-09 03:28:37 +00009#include "llvm/DerivedTypes.h"
10#include "llvm/Module.h"
Chris Lattnera9f5e052003-04-22 20:19:52 +000011#include "llvm/Instructions.h"
Chris Lattner0e9f93e2002-08-31 00:29:16 +000012#include "llvm/Pass.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000013#include "llvm/SymbolTable.h"
Chris Lattner3af3ba82002-05-09 21:31:18 +000014#include "llvm/SlotCalculator.h"
Chris Lattner0e9f93e2002-08-31 00:29:16 +000015#include "llvm/Analysis/FindUsedTypes.h"
Chris Lattnerd1cf1b42002-09-20 23:26:33 +000016#include "llvm/Analysis/ConstantsScanner.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000017#include "llvm/Support/InstVisitor.h"
Chris Lattner7683a122002-05-09 20:33:35 +000018#include "llvm/Support/InstIterator.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000019#include "Support/StringExtras.h"
20#include "Support/STLExtras.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000021#include <algorithm>
Chris Lattner78771c82002-05-17 04:55:35 +000022#include <set>
Nick Hildenbrandt98502372002-11-18 20:55:50 +000023#include <sstream>
Nick Hildenbrandt3cecdc52002-10-23 18:59:40 +000024
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000025namespace {
Chris Lattner0e9f93e2002-08-31 00:29:16 +000026 class CWriter : public Pass, public InstVisitor<CWriter> {
Chris Lattneredd8ce12003-05-03 03:14:35 +000027 std::ostream &Out;
Chris Lattner0e9f93e2002-08-31 00:29:16 +000028 SlotCalculator *Table;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000029 const Module *TheModule;
Chris Lattneredd8ce12003-05-03 03:14:35 +000030 std::map<const Type *, std::string> TypeNames;
Chris Lattner78771c82002-05-17 04:55:35 +000031 std::set<const Value*> MangledGlobals;
Nick Hildenbrandt3cecdc52002-10-23 18:59:40 +000032 bool needsMalloc;
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +000033
Chris Lattneredd8ce12003-05-03 03:14:35 +000034 std::map<const ConstantFP *, unsigned> FPConstantMap;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000035 public:
Chris Lattneredd8ce12003-05-03 03:14:35 +000036 CWriter(std::ostream &o) : Out(o) {}
Chris Lattner0e9f93e2002-08-31 00:29:16 +000037
38 void getAnalysisUsage(AnalysisUsage &AU) const {
39 AU.setPreservesAll();
40 AU.addRequired<FindUsedTypes>();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000041 }
Chris Lattner0e9f93e2002-08-31 00:29:16 +000042
43 virtual bool run(Module &M) {
44 // Initialize
45 Table = new SlotCalculator(&M, false);
46 TheModule = &M;
47
48 // Ensure that all structure types have names...
49 bool Changed = nameAllUsedStructureTypes(M);
50
51 // Run...
52 printModule(&M);
53
54 // Free memory...
55 delete Table;
56 TypeNames.clear();
57 MangledGlobals.clear();
58 return false;
59 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000060
Chris Lattneredd8ce12003-05-03 03:14:35 +000061 std::ostream &printType(std::ostream &Out, const Type *Ty,
62 const std::string &VariableName = "",
63 bool IgnoreName = false, bool namedContext = true);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000064
Chris Lattnerbb03efd2002-06-25 15:57:03 +000065 void writeOperand(Value *Operand);
66 void writeOperandInternal(Value *Operand);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000067
Chris Lattneredd8ce12003-05-03 03:14:35 +000068 std::string getValueName(const Value *V);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000069
Chris Lattner4fbf26d2002-05-09 20:53:56 +000070 private :
Chris Lattner0e9f93e2002-08-31 00:29:16 +000071 bool nameAllUsedStructureTypes(Module &M);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +000072 void printModule(Module *M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000073 void printSymbolTable(const SymbolTable &ST);
Chris Lattner2c601a72002-09-20 15:05:40 +000074 void printContainedStructs(const Type *Ty, std::set<const StructType *> &);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000075 void printGlobal(const GlobalVariable *GV);
Chris Lattner4cda8352002-08-20 16:55:48 +000076 void printFunctionSignature(const Function *F, bool Prototype);
77
Chris Lattner8c3c4bf2002-05-09 15:49:41 +000078 void printFunction(Function *);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000079
Chris Lattner6d492922002-08-19 21:32:41 +000080 void printConstant(Constant *CPV);
81 void printConstantArray(ConstantArray *CPA);
82
Chris Lattnerd0c668c2002-05-09 21:18:38 +000083 // isInlinableInst - Attempt to inline instructions into their uses to build
84 // trees as much as possible. To do this, we have to consistently decide
85 // what is acceptable to inline, so that variable declarations don't get
86 // printed and an extra copy of the expr is not emitted.
87 //
Chris Lattnerbb03efd2002-06-25 15:57:03 +000088 static bool isInlinableInst(const Instruction &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +000089 // Must be an expression, must be used exactly once. If it is dead, we
90 // emit it inline where it would go.
Chris Lattnerbb03efd2002-06-25 15:57:03 +000091 if (I.getType() == Type::VoidTy || I.use_size() != 1 ||
Nick Hildenbrandt2cf2cbc2002-11-06 20:07:54 +000092 isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I) ||
93 isa<LoadInst>(I)) // Don't inline a load across a store!
Chris Lattnerd0c668c2002-05-09 21:18:38 +000094 return false;
95
96 // Only inline instruction it it's use is in the same BB as the inst.
Chris Lattnerbb03efd2002-06-25 15:57:03 +000097 return I.getParent() == cast<Instruction>(I.use_back())->getParent();
Chris Lattnerd0c668c2002-05-09 21:18:38 +000098 }
99
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000100 // Instruction visitation functions
101 friend class InstVisitor<CWriter>;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000102
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000103 void visitReturnInst(ReturnInst &I);
104 void visitBranchInst(BranchInst &I);
Chris Lattnera9f5e052003-04-22 20:19:52 +0000105 void visitSwitchInst(SwitchInst &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000106
Chris Lattner84e66652003-05-03 07:11:00 +0000107 void visitPHINode(PHINode &I);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000108 void visitBinaryOperator(Instruction &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000109
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000110 void visitCastInst (CastInst &I);
111 void visitCallInst (CallInst &I);
112 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000113
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000114 void visitMallocInst(MallocInst &I);
115 void visitAllocaInst(AllocaInst &I);
116 void visitFreeInst (FreeInst &I);
117 void visitLoadInst (LoadInst &I);
118 void visitStoreInst (StoreInst &I);
119 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000120
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000121 void visitInstruction(Instruction &I) {
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000122 std::cerr << "C Writer does not know about " << I;
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000123 abort();
124 }
125
126 void outputLValue(Instruction *I) {
127 Out << " " << getValueName(I) << " = ";
128 }
129 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
130 unsigned Indent);
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000131 void printIndexingExpression(Value *Ptr, User::op_iterator I,
132 User::op_iterator E);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000133 };
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000134}
135
Chris Lattner2f499022002-05-09 04:21:21 +0000136// We dont want identifier names with ., space, - in them.
137// So we replace them with _
Chris Lattneredd8ce12003-05-03 03:14:35 +0000138static std::string makeNameProper(std::string x) {
139 std::string tmp;
140 for (std::string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
Chris Lattner2f499022002-05-09 04:21:21 +0000141 switch (*sI) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000142 case '.': tmp += "d_"; break;
143 case ' ': tmp += "s_"; break;
144 case '-': tmp += "D_"; break;
Chris Lattner2f499022002-05-09 04:21:21 +0000145 default: tmp += *sI;
146 }
147
148 return tmp;
149}
150
Chris Lattneredd8ce12003-05-03 03:14:35 +0000151std::string CWriter::getValueName(const Value *V) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000152 if (V->hasName()) { // Print out the label if it exists...
153 if (isa<GlobalValue>(V) && // Do not mangle globals...
Chris Lattneredd8ce12003-05-03 03:14:35 +0000154 (cast<GlobalValue>(V)->hasExternalLinkage() &&// Unless it's internal or
155 !MangledGlobals.count(V))) // Unless the name would collide if we don't
Chris Lattner2f499022002-05-09 04:21:21 +0000156 return makeNameProper(V->getName());
157
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000158 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
159 makeNameProper(V->getName());
Chris Lattner2f499022002-05-09 04:21:21 +0000160 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000161
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000162 int Slot = Table->getValSlot(V);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000163 assert(Slot >= 0 && "Invalid value!");
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000164 return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000165}
166
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000167// A pointer type should not use parens around *'s alone, e.g., (**)
Chris Lattneredd8ce12003-05-03 03:14:35 +0000168inline bool ptrTypeNameNeedsParens(const std::string &NameSoFar) {
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000169 return (NameSoFar.find_last_not_of('*') != std::string::npos);
170}
171
Chris Lattner83c57752002-08-19 22:17:53 +0000172// Pass the Type* and the variable name and this prints out the variable
173// declaration.
174//
Chris Lattneredd8ce12003-05-03 03:14:35 +0000175std::ostream &CWriter::printType(std::ostream &Out, const Type *Ty,
176 const std::string &NameSoFar,
177 bool IgnoreName, bool namedContext) {
Chris Lattner83c57752002-08-19 22:17:53 +0000178 if (Ty->isPrimitiveType())
179 switch (Ty->getPrimitiveID()) {
180 case Type::VoidTyID: return Out << "void " << NameSoFar;
181 case Type::BoolTyID: return Out << "bool " << NameSoFar;
182 case Type::UByteTyID: return Out << "unsigned char " << NameSoFar;
183 case Type::SByteTyID: return Out << "signed char " << NameSoFar;
184 case Type::UShortTyID: return Out << "unsigned short " << NameSoFar;
185 case Type::ShortTyID: return Out << "short " << NameSoFar;
186 case Type::UIntTyID: return Out << "unsigned " << NameSoFar;
187 case Type::IntTyID: return Out << "int " << NameSoFar;
188 case Type::ULongTyID: return Out << "unsigned long long " << NameSoFar;
189 case Type::LongTyID: return Out << "signed long long " << NameSoFar;
190 case Type::FloatTyID: return Out << "float " << NameSoFar;
191 case Type::DoubleTyID: return Out << "double " << NameSoFar;
192 default :
193 std::cerr << "Unknown primitive type: " << Ty << "\n";
194 abort();
195 }
196
197 // Check to see if the type is named.
Chris Lattner04b72c82002-10-16 00:08:22 +0000198 if (!IgnoreName || isa<OpaqueType>(Ty)) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000199 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner83c57752002-08-19 22:17:53 +0000200 if (I != TypeNames.end()) {
201 return Out << I->second << " " << NameSoFar;
202 }
Chris Lattner04b72c82002-10-16 00:08:22 +0000203 }
Chris Lattner83c57752002-08-19 22:17:53 +0000204
Chris Lattner83c57752002-08-19 22:17:53 +0000205 switch (Ty->getPrimitiveID()) {
206 case Type::FunctionTyID: {
207 const FunctionType *MTy = cast<FunctionType>(Ty);
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000208 std::stringstream FunctionInards;
209 FunctionInards << " (" << NameSoFar << ") (";
Chris Lattner83c57752002-08-19 22:17:53 +0000210 for (FunctionType::ParamTypes::const_iterator
211 I = MTy->getParamTypes().begin(),
212 E = MTy->getParamTypes().end(); I != E; ++I) {
213 if (I != MTy->getParamTypes().begin())
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000214 FunctionInards << ", ";
215 printType(FunctionInards, *I, "");
Chris Lattner83c57752002-08-19 22:17:53 +0000216 }
217 if (MTy->isVarArg()) {
218 if (!MTy->getParamTypes().empty())
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000219 FunctionInards << ", ";
220 FunctionInards << "...";
Chris Lattner83c57752002-08-19 22:17:53 +0000221 }
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000222 FunctionInards << ")";
Chris Lattneredd8ce12003-05-03 03:14:35 +0000223 std::string tstr = FunctionInards.str();
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000224 printType(Out, MTy->getReturnType(), tstr);
225 return Out;
Chris Lattner83c57752002-08-19 22:17:53 +0000226 }
227 case Type::StructTyID: {
228 const StructType *STy = cast<StructType>(Ty);
229 Out << NameSoFar + " {\n";
230 unsigned Idx = 0;
231 for (StructType::ElementTypes::const_iterator
232 I = STy->getElementTypes().begin(),
233 E = STy->getElementTypes().end(); I != E; ++I) {
234 Out << " ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000235 printType(Out, *I, "field" + utostr(Idx++));
Chris Lattner83c57752002-08-19 22:17:53 +0000236 Out << ";\n";
237 }
238 return Out << "}";
239 }
240
241 case Type::PointerTyID: {
242 const PointerType *PTy = cast<PointerType>(Ty);
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000243 std::string ptrName = "*" + NameSoFar;
244
245 // Do not need parens around "* NameSoFar" if NameSoFar consists only
246 // of zero or more '*' chars *and* this is not an unnamed pointer type
247 // such as the result type in a cast statement. Otherwise, enclose in ( ).
Nick Hildenbrandtc14ded42002-09-23 21:02:50 +0000248 if (ptrTypeNameNeedsParens(NameSoFar) || !namedContext ||
249 PTy->getElementType()->getPrimitiveID() == Type::ArrayTyID)
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000250 ptrName = "(" + ptrName + ")"; //
251
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000252 return printType(Out, PTy->getElementType(), ptrName);
253 }Out <<"--";
Chris Lattner83c57752002-08-19 22:17:53 +0000254
255 case Type::ArrayTyID: {
256 const ArrayType *ATy = cast<ArrayType>(Ty);
257 unsigned NumElements = ATy->getNumElements();
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000258 return printType(Out, ATy->getElementType(),
Chris Lattner83c57752002-08-19 22:17:53 +0000259 NameSoFar + "[" + utostr(NumElements) + "]");
260 }
Chris Lattner04b72c82002-10-16 00:08:22 +0000261
262 case Type::OpaqueTyID: {
263 static int Count = 0;
Chris Lattneredd8ce12003-05-03 03:14:35 +0000264 std::string TyName = "struct opaque_" + itostr(Count++);
Chris Lattner04b72c82002-10-16 00:08:22 +0000265 assert(TypeNames.find(Ty) == TypeNames.end());
266 TypeNames[Ty] = TyName;
267 return Out << TyName << " " << NameSoFar;
268 }
Chris Lattner83c57752002-08-19 22:17:53 +0000269 default:
270 assert(0 && "Unhandled case in getTypeProps!");
271 abort();
272 }
273
274 return Out;
275}
276
Chris Lattner6d492922002-08-19 21:32:41 +0000277void CWriter::printConstantArray(ConstantArray *CPA) {
278
279 // As a special case, print the array as a string if it is an array of
280 // ubytes or an array of sbytes with positive values.
281 //
282 const Type *ETy = CPA->getType()->getElementType();
283 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
284
285 // Make sure the last character is a null char, as automatically added by C
286 if (CPA->getNumOperands() == 0 ||
287 !cast<Constant>(*(CPA->op_end()-1))->isNullValue())
288 isString = false;
289
290 if (isString) {
291 Out << "\"";
292 // Do not include the last character, which we know is null
293 for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) {
294 unsigned char C = (ETy == Type::SByteTy) ?
295 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
296 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
297
298 if (isprint(C)) {
Nick Hildenbrandt088b4722002-11-06 21:40:23 +0000299 if (C == '"' || C == '\\')
300 Out << "\\" << C;
Nick Hildenbrandtc3dd2af2002-09-30 21:11:55 +0000301 else
302 Out << C;
Chris Lattner6d492922002-08-19 21:32:41 +0000303 } else {
304 switch (C) {
305 case '\n': Out << "\\n"; break;
306 case '\t': Out << "\\t"; break;
307 case '\r': Out << "\\r"; break;
308 case '\v': Out << "\\v"; break;
309 case '\a': Out << "\\a"; break;
Nick Hildenbrandtc3dd2af2002-09-30 21:11:55 +0000310 case '\"': Out << "\\\""; break;
311 case '\'': Out << "\\\'"; break;
Chris Lattner6d492922002-08-19 21:32:41 +0000312 default:
313 Out << "\\x";
314 Out << ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
315 Out << ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
316 break;
317 }
318 }
319 }
320 Out << "\"";
321 } else {
322 Out << "{";
323 if (CPA->getNumOperands()) {
324 Out << " ";
325 printConstant(cast<Constant>(CPA->getOperand(0)));
326 for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
327 Out << ", ";
328 printConstant(cast<Constant>(CPA->getOperand(i)));
329 }
330 }
331 Out << " }";
332 }
333}
334
335
336// printConstant - The LLVM Constant to C Constant converter.
337void CWriter::printConstant(Constant *CPV) {
338 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
339 switch (CE->getOpcode()) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000340 case Instruction::Cast:
341 Out << "((";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000342 printType(Out, CPV->getType());
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000343 Out << ")";
344 printConstant(cast<Constant>(CPV->getOperand(0)));
345 Out << ")";
346 return;
347
348 case Instruction::GetElementPtr:
Nick Hildenbrandtd2eb3862002-10-03 20:47:24 +0000349 Out << "(&(";
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000350 printIndexingExpression(CPV->getOperand(0),
351 CPV->op_begin()+1, CPV->op_end());
Nick Hildenbrandtd2eb3862002-10-03 20:47:24 +0000352 Out << "))";
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000353 return;
354 case Instruction::Add:
355 Out << "(";
356 printConstant(cast<Constant>(CPV->getOperand(0)));
357 Out << " + ";
358 printConstant(cast<Constant>(CPV->getOperand(1)));
359 Out << ")";
360 return;
361 case Instruction::Sub:
362 Out << "(";
363 printConstant(cast<Constant>(CPV->getOperand(0)));
364 Out << " - ";
365 printConstant(cast<Constant>(CPV->getOperand(1)));
366 Out << ")";
367 return;
368
Chris Lattner6d492922002-08-19 21:32:41 +0000369 default:
370 std::cerr << "CWriter Error: Unhandled constant expression: "
371 << CE << "\n";
372 abort();
373 }
374 }
375
376 switch (CPV->getType()->getPrimitiveID()) {
377 case Type::BoolTyID:
378 Out << (CPV == ConstantBool::False ? "0" : "1"); break;
379 case Type::SByteTyID:
380 case Type::ShortTyID:
381 case Type::IntTyID:
382 Out << cast<ConstantSInt>(CPV)->getValue(); break;
383 case Type::LongTyID:
384 Out << cast<ConstantSInt>(CPV)->getValue() << "ll"; break;
385
386 case Type::UByteTyID:
387 case Type::UShortTyID:
388 Out << cast<ConstantUInt>(CPV)->getValue(); break;
389 case Type::UIntTyID:
390 Out << cast<ConstantUInt>(CPV)->getValue() << "u"; break;
391 case Type::ULongTyID:
392 Out << cast<ConstantUInt>(CPV)->getValue() << "ull"; break;
393
394 case Type::FloatTyID:
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000395 case Type::DoubleTyID: {
396 ConstantFP *FPC = cast<ConstantFP>(CPV);
Chris Lattneredd8ce12003-05-03 03:14:35 +0000397 std::map<const ConstantFP*, unsigned>::iterator I = FPConstantMap.find(FPC);
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000398 if (I != FPConstantMap.end()) {
399 // Because of FP precision problems we must load from a stack allocated
400 // value that holds the value in hex.
401 Out << "(*(" << (FPC->getType() == Type::FloatTy ? "float" : "double")
402 << "*)&FloatConstant" << I->second << ")";
403 } else {
404 Out << FPC->getValue();
405 }
406 break;
407 }
Chris Lattner6d492922002-08-19 21:32:41 +0000408
409 case Type::ArrayTyID:
410 printConstantArray(cast<ConstantArray>(CPV));
411 break;
412
413 case Type::StructTyID: {
414 Out << "{";
415 if (CPV->getNumOperands()) {
416 Out << " ";
417 printConstant(cast<Constant>(CPV->getOperand(0)));
418 for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) {
419 Out << ", ";
420 printConstant(cast<Constant>(CPV->getOperand(i)));
421 }
422 }
423 Out << " }";
424 break;
425 }
426
427 case Type::PointerTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000428 if (isa<ConstantPointerNull>(CPV)) {
Nick Hildenbrandtf0fca362002-10-28 19:54:06 +0000429 Out << "(NULL)";
Chris Lattner6d492922002-08-19 21:32:41 +0000430 break;
431 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
432 writeOperand(CPR->getValue());
433 break;
434 }
435 // FALL THROUGH
436 default:
437 std::cerr << "Unknown constant type: " << CPV << "\n";
438 abort();
439 }
440}
441
442void CWriter::writeOperandInternal(Value *Operand) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000443 if (Instruction *I = dyn_cast<Instruction>(Operand))
444 if (isInlinableInst(*I)) {
445 // Should we inline this instruction to build a tree?
446 Out << "(";
447 visit(*I);
448 Out << ")";
449 return;
450 }
451
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +0000452 if (Operand->hasName()) {
Chris Lattner6d492922002-08-19 21:32:41 +0000453 Out << getValueName(Operand);
454 } else if (Constant *CPV = dyn_cast<Constant>(Operand)) {
455 printConstant(CPV);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000456 } else {
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000457 int Slot = Table->getValSlot(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000458 assert(Slot >= 0 && "Malformed LLVM!");
459 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
460 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000461}
462
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000463void CWriter::writeOperand(Value *Operand) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000464 if (isa<GlobalVariable>(Operand))
465 Out << "(&"; // Global variables are references as their addresses by llvm
466
467 writeOperandInternal(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000468
469 if (isa<GlobalVariable>(Operand))
470 Out << ")";
471}
472
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000473// nameAllUsedStructureTypes - If there are structure types in the module that
474// are used but do not have names assigned to them in the symbol table yet then
475// we assign them names now.
476//
477bool CWriter::nameAllUsedStructureTypes(Module &M) {
478 // Get a set of types that are used by the program...
479 std::set<const Type *> UT = getAnalysis<FindUsedTypes>().getTypes();
480
481 // Loop over the module symbol table, removing types from UT that are already
482 // named.
483 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000484 SymbolTable &MST = M.getSymbolTable();
485 if (MST.find(Type::TypeTy) != MST.end())
486 for (SymbolTable::type_iterator I = MST.type_begin(Type::TypeTy),
487 E = MST.type_end(Type::TypeTy); I != E; ++I)
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000488 UT.erase(cast<Type>(I->second));
489
490 // UT now contains types that are not named. Loop over it, naming structure
491 // types.
492 //
493 bool Changed = false;
494 for (std::set<const Type *>::const_iterator I = UT.begin(), E = UT.end();
495 I != E; ++I)
496 if (const StructType *ST = dyn_cast<StructType>(*I)) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000497 ((Value*)ST)->setName("unnamed", &MST);
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000498 Changed = true;
499 }
500 return Changed;
501}
502
Chris Lattneredd8ce12003-05-03 03:14:35 +0000503static void generateAllocaDecl(std::ostream& Out) {
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000504 // On SunOS, we need to insert the alloca macro & proto for the builtin.
505 Out << "#ifdef sun\n"
506 << "extern void *__builtin_alloca(unsigned long);\n"
507 << "#define alloca(x) __builtin_alloca(x)\n"
508 << "#else\n"
509 << "#include <alloca.h>\n"
510 << "#endif\n\n";
511}
512
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000513void CWriter::printModule(Module *M) {
Chris Lattner78771c82002-05-17 04:55:35 +0000514 // Calculate which global values have names that will collide when we throw
515 // away type information.
Chris Lattner594b9fa2002-05-21 21:10:04 +0000516 { // Scope to delete the FoundNames set when we are done with it...
Chris Lattneredd8ce12003-05-03 03:14:35 +0000517 std::set<std::string> FoundNames;
Chris Lattner78771c82002-05-17 04:55:35 +0000518 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000519 if (I->hasName()) // If the global has a name...
520 if (FoundNames.count(I->getName())) // And the name is already used
521 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000522 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000523 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000524
525 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000526 if (I->hasName()) // If the global has a name...
527 if (FoundNames.count(I->getName())) // And the name is already used
528 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000529 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000530 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000531 }
532
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000533 // printing stdlib inclusion
Nick Hildenbrandt98360a12002-10-11 21:40:44 +0000534 //Out << "#include <stdlib.h>\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000535
Chris Lattner16c7bb22002-05-09 02:28:59 +0000536 // get declaration for alloca
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000537 Out << "/* Provide Declarations */\n";
538 generateAllocaDecl(Out);
539
540 // Provide a definition for null if one does not already exist,
541 // and for `bool' if not compiling with a C++ compiler.
542 Out << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000543 << "#ifndef __cplusplus\ntypedef unsigned char bool;\n#endif\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000544
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000545 << "\n\n/* Support for floating point constants */\n"
546 << "typedef unsigned long long ConstantDoubleTy;\n"
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000547 << "typedef unsigned int ConstantFloatTy;\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000548
Chris Lattnera4d4a852002-08-19 21:48:40 +0000549 << "\n\n/* Global Declarations */\n";
550
551 // First output all the declarations for the program, because C requires
552 // Functions & globals to be declared before they are used.
553 //
Chris Lattner16c7bb22002-05-09 02:28:59 +0000554
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000555 // Loop over the symbol table, emitting all named constants...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000556 printSymbolTable(M->getSymbolTable());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000557
Chris Lattnera4d4a852002-08-19 21:48:40 +0000558 // Global variable declarations...
559 if (!M->gempty()) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000560 Out << "\n/* External Global Variable Declarations */\n";
Chris Lattnera4d4a852002-08-19 21:48:40 +0000561 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000562 if (I->hasExternalLinkage()) {
563 Out << "extern ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000564 printType(Out, I->getType()->getElementType(), getValueName(I));
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000565 Out << ";\n";
566 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000567 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000568 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000569
Chris Lattnera4d4a852002-08-19 21:48:40 +0000570 // Function declarations
571 if (!M->empty()) {
572 Out << "\n/* Function Declarations */\n";
Nick Hildenbrandt3cecdc52002-10-23 18:59:40 +0000573 needsMalloc = true;
Chris Lattner4cda8352002-08-20 16:55:48 +0000574 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
Nick Hildenbrandta1a64f82002-11-18 22:21:52 +0000575 // If the function is external and the name collides don't print it.
Chris Lattneredd8ce12003-05-03 03:14:35 +0000576 // Sometimes the bytecode likes to have multiple "declerations" for
577 // external functions
Nick Hildenbrandta1a64f82002-11-18 22:21:52 +0000578 if (I->hasInternalLinkage() || !MangledGlobals.count(I)){
579 printFunctionSignature(I, true);
580 Out << ";\n";
581 }
Chris Lattner4cda8352002-08-20 16:55:48 +0000582 }
Chris Lattnera4d4a852002-08-19 21:48:40 +0000583 }
584
Nick Hildenbrandt3cecdc52002-10-23 18:59:40 +0000585 // Print Malloc prototype if needed
586 if (needsMalloc){
587 Out << "\n/* Malloc to make sun happy */\n";
588 Out << "extern void * malloc(size_t);\n\n";
589 }
590
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +0000591 // Output the global variable declerations
592 if (!M->gempty()) {
593 Out << "\n\n/* Global Variable Declerations */\n";
594 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
595 if (!I->isExternal()) {
596 Out << "extern ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000597 printType(Out, I->getType()->getElementType(), getValueName(I));
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +0000598
599 Out << ";\n";
600 }
601 }
602
603
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000604 // Output the global variable definitions and contents...
Chris Lattnera4d4a852002-08-19 21:48:40 +0000605 if (!M->gempty()) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000606 Out << "\n\n/* Global Variable Definitions and Initialization */\n";
Chris Lattnere8e035b2002-10-16 20:08:47 +0000607 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
608 if (!I->isExternal()) {
609 if (I->hasInternalLinkage())
610 Out << "static ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000611 printType(Out, I->getType()->getElementType(), getValueName(I));
Chris Lattnera4d4a852002-08-19 21:48:40 +0000612
Chris Lattnera4d4a852002-08-19 21:48:40 +0000613 Out << " = " ;
614 writeOperand(I->getInitializer());
Chris Lattnere8e035b2002-10-16 20:08:47 +0000615 Out << ";\n";
Chris Lattnera4d4a852002-08-19 21:48:40 +0000616 }
Chris Lattnera4d4a852002-08-19 21:48:40 +0000617 }
618
Chris Lattner16c7bb22002-05-09 02:28:59 +0000619 // Output all of the functions...
Chris Lattnera4d4a852002-08-19 21:48:40 +0000620 if (!M->empty()) {
621 Out << "\n\n/* Function Bodies */\n";
622 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
623 printFunction(I);
624 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000625}
626
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000627
Chris Lattner2db41cd2002-09-20 15:12:13 +0000628/// printSymbolTable - Run through symbol table looking for type names. If a
629/// type name is found, emit it's declaration...
Chris Lattnera4c047e2002-09-20 14:56:54 +0000630///
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000631void CWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattner2db41cd2002-09-20 15:12:13 +0000632 // If there are no type names, exit early.
633 if (ST.find(Type::TypeTy) == ST.end())
634 return;
635
636 // We are only interested in the type plane of the symbol table...
637 SymbolTable::type_const_iterator I = ST.type_begin(Type::TypeTy);
638 SymbolTable::type_const_iterator End = ST.type_end(Type::TypeTy);
639
Chris Lattner58d04d42002-09-20 15:18:30 +0000640 // Print out forward declarations for structure types before anything else!
641 Out << "/* Structure forward decls */\n";
642 for (; I != End; ++I)
643 if (const Type *STy = dyn_cast<StructType>(I->second)) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000644 std::string Name = "struct l_" + makeNameProper(I->first);
Chris Lattner58d04d42002-09-20 15:18:30 +0000645 Out << Name << ";\n";
646 TypeNames.insert(std::make_pair(STy, Name));
647 }
648
649 Out << "\n";
650
651 // Now we can print out typedefs...
652 Out << "/* Typedefs */\n";
653 for (I = ST.type_begin(Type::TypeTy); I != End; ++I) {
654 const Type *Ty = cast<Type>(I->second);
Chris Lattneredd8ce12003-05-03 03:14:35 +0000655 std::string Name = "l_" + makeNameProper(I->first);
Chris Lattner58d04d42002-09-20 15:18:30 +0000656 Out << "typedef ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000657 printType(Out, Ty, Name);
Chris Lattner58d04d42002-09-20 15:18:30 +0000658 Out << ";\n";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000659 }
660
661 Out << "\n";
662
Chris Lattner2c601a72002-09-20 15:05:40 +0000663 // Keep track of which structures have been printed so far...
664 std::set<const StructType *> StructPrinted;
665
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000666 // Loop over all structures then push them into the stack so they are
667 // printed in the correct order.
Chris Lattner2db41cd2002-09-20 15:12:13 +0000668 //
Chris Lattner58d04d42002-09-20 15:18:30 +0000669 Out << "/* Structure contents */\n";
Chris Lattner2db41cd2002-09-20 15:12:13 +0000670 for (I = ST.type_begin(Type::TypeTy); I != End; ++I)
671 if (const StructType *STy = dyn_cast<StructType>(I->second))
672 printContainedStructs(STy, StructPrinted);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000673}
674
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000675// Push the struct onto the stack and recursively push all structs
676// this one depends on.
Chris Lattner2c601a72002-09-20 15:05:40 +0000677void CWriter::printContainedStructs(const Type *Ty,
678 std::set<const StructType*> &StructPrinted){
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000679 if (const StructType *STy = dyn_cast<StructType>(Ty)){
680 //Check to see if we have already printed this struct
Chris Lattner2c601a72002-09-20 15:05:40 +0000681 if (StructPrinted.count(STy) == 0) {
682 // Print all contained types first...
Chris Lattnera4c047e2002-09-20 14:56:54 +0000683 for (StructType::ElementTypes::const_iterator
684 I = STy->getElementTypes().begin(),
685 E = STy->getElementTypes().end(); I != E; ++I) {
Chris Lattner2c601a72002-09-20 15:05:40 +0000686 const Type *Ty1 = I->get();
Chris Lattnera4c047e2002-09-20 14:56:54 +0000687 if (isa<StructType>(Ty1) || isa<ArrayType>(Ty1))
Chris Lattner2c601a72002-09-20 15:05:40 +0000688 printContainedStructs(Ty1, StructPrinted);
Chris Lattnera4c047e2002-09-20 14:56:54 +0000689 }
690
Chris Lattner2c601a72002-09-20 15:05:40 +0000691 //Print structure type out..
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000692 StructPrinted.insert(STy);
Chris Lattneredd8ce12003-05-03 03:14:35 +0000693 std::string Name = TypeNames[STy];
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000694 printType(Out, STy, Name, true);
Chris Lattner58d04d42002-09-20 15:18:30 +0000695 Out << ";\n\n";
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000696 }
Chris Lattnera4c047e2002-09-20 14:56:54 +0000697
Chris Lattner2c601a72002-09-20 15:05:40 +0000698 // If it is an array, check contained types and continue
699 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)){
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000700 const Type *Ty1 = ATy->getElementType();
701 if (isa<StructType>(Ty1) || isa<ArrayType>(Ty1))
Chris Lattner2c601a72002-09-20 15:05:40 +0000702 printContainedStructs(Ty1, StructPrinted);
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000703 }
704}
705
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000706
Chris Lattner4cda8352002-08-20 16:55:48 +0000707void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
Nick Hildenbrandt3cecdc52002-10-23 18:59:40 +0000708 // If the program provides it's own malloc prototype we don't need
709 // to include the general one.
710 if (getValueName(F) == "malloc")
711 needsMalloc = false;
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000712 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000713 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000714 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000715
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000716 std::stringstream FunctionInards;
717
718 // Print out the name...
719 FunctionInards << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000720
Chris Lattner16c7bb22002-05-09 02:28:59 +0000721 if (!F->isExternal()) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000722 if (!F->aempty()) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000723 std::string ArgName;
Chris Lattner4cda8352002-08-20 16:55:48 +0000724 if (F->abegin()->hasName() || !Prototype)
725 ArgName = getValueName(F->abegin());
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000726 printType(FunctionInards, F->afront().getType(), ArgName);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000727 for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
728 I != E; ++I) {
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000729 FunctionInards << ", ";
Chris Lattner4cda8352002-08-20 16:55:48 +0000730 if (I->hasName() || !Prototype)
731 ArgName = getValueName(I);
732 else
733 ArgName = "";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000734 printType(FunctionInards, I->getType(), ArgName);
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000735 }
736 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000737 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000738 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000739 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000740 FT->getParamTypes().begin(),
741 E = FT->getParamTypes().end(); I != E; ++I) {
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000742 if (I != FT->getParamTypes().begin()) FunctionInards << ", ";
743 printType(FunctionInards, *I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000744 }
745 }
746
Chris Lattner1f8c4a12002-09-20 22:32:30 +0000747 // Finish printing arguments... if this is a vararg function, print the ...,
748 // unless there are no known types, in which case, we just emit ().
749 //
750 if (FT->isVarArg() && !FT->getParamTypes().empty()) {
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000751 if (FT->getParamTypes().size()) FunctionInards << ", ";
752 FunctionInards << "..."; // Output varargs portion of signature!
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000753 }
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000754 FunctionInards << ")";
755 // Print out the return type and the entire signature for that matter
756 printType(Out, F->getReturnType(), FunctionInards.str());
757
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000758}
759
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000760
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000761void CWriter::printFunction(Function *F) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000762 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000763
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000764 Table->incorporateFunction(F);
Chris Lattnerf34ee812002-05-09 03:12:34 +0000765
Chris Lattner4cda8352002-08-20 16:55:48 +0000766 printFunctionSignature(F, false);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000767 Out << " {\n";
768
Chris Lattner497e19a2002-05-09 20:39:03 +0000769 // print local variable information for the function
Chris Lattner7683a122002-05-09 20:33:35 +0000770 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000771 if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000772 Out << " ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000773 printType(Out, (*I)->getType(), getValueName(*I));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000774 Out << ";\n";
Chris Lattner84e66652003-05-03 07:11:00 +0000775
776 if (isa<PHINode>(*I)) { // Print out PHI node temporaries as well...
777 Out << " ";
778 printType(Out, (*I)->getType(), getValueName(*I)+"__PHI_TEMPORARY");
779 Out << ";\n";
780 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000781 }
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000782
783 Out << "\n";
784
785 // Scan the function for floating point constants. If any FP constant is used
786 // in the function, we want to redirect it here so that we do not depend on
787 // the precision of the printed form.
788 //
789 unsigned FPCounter = 0;
790 for (constant_iterator I = constant_begin(F), E = constant_end(F); I != E;++I)
791 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(*I))
792 if (FPConstantMap.find(FPC) == FPConstantMap.end()) {
793 double Val = FPC->getValue();
794
795 FPConstantMap[FPC] = FPCounter; // Number the FP constants
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000796
797 if (FPC->getType() == Type::DoubleTy)
798 Out << " const ConstantDoubleTy FloatConstant" << FPCounter++
799 << " = 0x" << std::hex << *(unsigned long long*)&Val << std::dec
800 << "; /* " << Val << " */\n";
Chris Lattner9c1338b2002-11-07 22:12:53 +0000801 else if (FPC->getType() == Type::FloatTy) {
802 float fVal = Val;
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000803 Out << " const ConstantFloatTy FloatConstant" << FPCounter++
Chris Lattner9c1338b2002-11-07 22:12:53 +0000804 << " = 0x" << std::hex << *(unsigned*)&fVal << std::dec
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000805 << "; /* " << Val << " */\n";
Chris Lattner9c1338b2002-11-07 22:12:53 +0000806 } else
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000807 assert(0 && "Unknown float type!");
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000808 }
809
810 Out << "\n";
Chris Lattner16c7bb22002-05-09 02:28:59 +0000811
812 // print the basic blocks
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000813 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
814 BasicBlock *Prev = BB->getPrev();
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000815
816 // Don't print the label for the basic block if there are no uses, or if the
817 // only terminator use is the precessor basic block's terminator. We have
818 // to scan the use list because PHI nodes use basic blocks too but do not
819 // require a label to be generated.
820 //
821 bool NeedsLabel = false;
822 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
823 UI != UE; ++UI)
824 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
Chris Lattnerf1acd962003-04-23 19:15:13 +0000825 if (TI != Prev->getTerminator() ||
826 isa<SwitchInst>(Prev->getTerminator())) {
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000827 NeedsLabel = true;
828 break;
829 }
830
831 if (NeedsLabel) Out << getValueName(BB) << ":\n";
832
833 // Output all of the instructions in the basic block...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000834 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II){
Chris Lattner84e66652003-05-03 07:11:00 +0000835 if (!isInlinableInst(*II)) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000836 if (II->getType() != Type::VoidTy)
837 outputLValue(II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000838 else
839 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000840 visit(*II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000841 Out << ";\n";
842 }
843 }
844
845 // Don't emit prefix or suffix for the terminator...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000846 visit(*BB->getTerminator());
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000847 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000848
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000849 Out << "}\n\n";
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000850 Table->purgeFunction();
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000851 FPConstantMap.clear();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000852}
853
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000854// Specific Instruction type classes... note that all of the casts are
855// neccesary because we use the instruction classes as opaque types...
856//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000857void CWriter::visitReturnInst(ReturnInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000858 // Don't output a void return if this is the last basic block in the function
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000859 if (I.getNumOperands() == 0 &&
860 &*--I.getParent()->getParent()->end() == I.getParent() &&
861 !I.getParent()->size() == 1) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000862 return;
Chris Lattner963b70b2002-05-21 18:05:19 +0000863 }
Chris Lattner44408262002-05-09 03:50:42 +0000864
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000865 Out << " return";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000866 if (I.getNumOperands()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000867 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000868 writeOperand(I.getOperand(0));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000869 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000870 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000871}
872
Chris Lattnera9f5e052003-04-22 20:19:52 +0000873void CWriter::visitSwitchInst(SwitchInst &SI) {
874 Out << " switch (";
875 writeOperand(SI.getOperand(0));
Chris Lattnerf1acd962003-04-23 19:15:13 +0000876 Out << ") {\n default:\n";
877 printBranchToBlock(SI.getParent(), SI.getDefaultDest(), 2);
Chris Lattnera9f5e052003-04-22 20:19:52 +0000878 Out << ";\n";
879 for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) {
880 Out << " case ";
881 writeOperand(SI.getOperand(i));
882 Out << ":\n";
883 BasicBlock *Succ = cast<BasicBlock>(SI.getOperand(i+1));
884 printBranchToBlock(SI.getParent(), Succ, 2);
885 if (Succ == SI.getParent()->getNext())
886 Out << " break;\n";
887 }
888 Out << " }\n";
889}
890
891
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000892static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
893 // If PHI nodes need copies, we need the copy code...
894 if (isa<PHINode>(To->front()) ||
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000895 From->getNext() != To) // Not directly successor, need goto
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000896 return true;
897
898 // Otherwise we don't need the code.
899 return false;
900}
901
902void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
903 unsigned Indent) {
904 for (BasicBlock::iterator I = Succ->begin();
Chris Lattner2ee82e02003-04-23 16:36:11 +0000905 PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000906 // now we have to do the printing
Chris Lattneredd8ce12003-05-03 03:14:35 +0000907 Out << std::string(Indent, ' ');
Chris Lattner84e66652003-05-03 07:11:00 +0000908 Out << " " << getValueName(I) << "__PHI_TEMPORARY = ";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000909 writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
910 Out << "; /* for PHI node */\n";
911 }
912
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000913 if (CurBB->getNext() != Succ) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000914 Out << std::string(Indent, ' ') << " goto ";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000915 writeOperand(Succ);
916 Out << ";\n";
917 }
918}
919
920// Brach instruction printing - Avoid printing out a brach to a basic block that
921// immediately succeeds the current one.
922//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000923void CWriter::visitBranchInst(BranchInst &I) {
924 if (I.isConditional()) {
925 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(0))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000926 Out << " if (";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000927 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000928 Out << ") {\n";
929
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000930 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000931
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000932 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(1))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000933 Out << " } else {\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000934 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000935 }
936 } else {
937 // First goto not neccesary, assume second one is...
938 Out << " if (!";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000939 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000940 Out << ") {\n";
941
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000942 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000943 }
944
945 Out << " }\n";
946 } else {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000947 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000948 }
949 Out << "\n";
950}
951
Chris Lattner84e66652003-05-03 07:11:00 +0000952// PHI nodes get copied into temporary values at the end of predecessor basic
953// blocks. We now need to copy these temporary values into the REAL value for
954// the PHI.
955void CWriter::visitPHINode(PHINode &I) {
956 writeOperand(&I);
957 Out << "__PHI_TEMPORARY";
958}
959
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000960
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000961void CWriter::visitBinaryOperator(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000962 // binary instructions, shift instructions, setCond instructions.
Chris Lattnerf5612b762003-04-23 19:09:22 +0000963 assert(!isa<PointerType>(I.getType()));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000964
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000965 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000966
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000967 switch (I.getOpcode()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000968 case Instruction::Add: Out << " + "; break;
969 case Instruction::Sub: Out << " - "; break;
970 case Instruction::Mul: Out << "*"; break;
971 case Instruction::Div: Out << "/"; break;
972 case Instruction::Rem: Out << "%"; break;
973 case Instruction::And: Out << " & "; break;
974 case Instruction::Or: Out << " | "; break;
975 case Instruction::Xor: Out << " ^ "; break;
976 case Instruction::SetEQ: Out << " == "; break;
977 case Instruction::SetNE: Out << " != "; break;
978 case Instruction::SetLE: Out << " <= "; break;
979 case Instruction::SetGE: Out << " >= "; break;
980 case Instruction::SetLT: Out << " < "; break;
981 case Instruction::SetGT: Out << " > "; break;
982 case Instruction::Shl : Out << " << "; break;
983 case Instruction::Shr : Out << " >> "; break;
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000984 default: std::cerr << "Invalid operator type!" << I; abort();
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000985 }
986
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000987 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000988}
989
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000990void CWriter::visitCastInst(CastInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000991 Out << "(";
Chris Lattneredd8ce12003-05-03 03:14:35 +0000992 printType(Out, I.getType(), "", /*ignoreName*/false, /*namedContext*/false);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000993 Out << ")";
Chris Lattnerf5612b762003-04-23 19:09:22 +0000994 if (isa<PointerType>(I.getType())&&I.getOperand(0)->getType()->isIntegral() ||
995 isa<PointerType>(I.getOperand(0)->getType())&&I.getType()->isIntegral()) {
996 // Avoid "cast to pointer from integer of different size" warnings
997 Out << "(long)";
998 }
999
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001000 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001001}
1002
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001003void CWriter::visitCallInst(CallInst &I) {
1004 const PointerType *PTy = cast<PointerType>(I.getCalledValue()->getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001005 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1006 const Type *RetTy = FTy->getReturnType();
1007
Chris Lattnerfabc8802002-08-26 20:50:09 +00001008 writeOperand(I.getOperand(0));
1009 Out << "(";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001010
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001011 if (I.getNumOperands() > 1) {
1012 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001013
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001014 for (unsigned op = 2, Eop = I.getNumOperands(); op != Eop; ++op) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001015 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001016 writeOperand(I.getOperand(op));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001017 }
1018 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001019 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001020}
1021
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001022void CWriter::visitMallocInst(MallocInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001023 Out << "(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001024 printType(Out, I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001025 Out << ")malloc(sizeof(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001026 printType(Out, I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001027 Out << ")";
1028
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001029 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001030 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001031 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001032 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001033 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001034}
1035
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001036void CWriter::visitAllocaInst(AllocaInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001037 Out << "(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001038 printType(Out, I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001039 Out << ") alloca(sizeof(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001040 printType(Out, I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001041 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001042 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001043 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001044 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001045 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001046 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001047}
1048
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001049void CWriter::visitFreeInst(FreeInst &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001050 Out << "free(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001051 writeOperand(I.getOperand(0));
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001052 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001053}
1054
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001055void CWriter::printIndexingExpression(Value *Ptr, User::op_iterator I,
1056 User::op_iterator E) {
1057 bool HasImplicitAddress = false;
1058 // If accessing a global value with no indexing, avoid *(&GV) syndrome
1059 if (GlobalValue *V = dyn_cast<GlobalValue>(Ptr)) {
1060 HasImplicitAddress = true;
1061 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Ptr)) {
1062 HasImplicitAddress = true;
1063 Ptr = CPR->getValue(); // Get to the global...
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001064 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001065
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001066 if (I == E) {
1067 if (!HasImplicitAddress)
1068 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001069
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001070 writeOperandInternal(Ptr);
1071 return;
1072 }
1073
1074 const Constant *CI = dyn_cast<Constant>(I->get());
1075 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
1076 Out << "(&";
1077
1078 writeOperandInternal(Ptr);
1079
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001080 if (HasImplicitAddress && (!CI || !CI->isNullValue())) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001081 Out << ")";
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001082 HasImplicitAddress = false; // HIA is only true if we haven't addressed yet
1083 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001084
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001085 assert(!HasImplicitAddress || (CI && CI->isNullValue()) &&
1086 "Can only have implicit address with direct accessing");
1087
1088 if (HasImplicitAddress) {
1089 ++I;
1090 } else if (CI && CI->isNullValue() && I+1 != E) {
1091 // Print out the -> operator if possible...
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001092 if ((*(I+1))->getType() == Type::UByteTy) {
1093 Out << (HasImplicitAddress ? "." : "->");
1094 Out << "field" << cast<ConstantUInt>(*(I+1))->getValue();
1095 I += 2;
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001096 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001097 }
Chris Lattner5dfe7672002-08-22 22:48:55 +00001098
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001099 for (; I != E; ++I)
Chris Lattner106ff452002-09-11 01:21:29 +00001100 if ((*I)->getType() == Type::LongTy) {
Vikram S. Adve9d6f13f2002-09-15 21:51:04 +00001101 Out << "[";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001102 writeOperand(*I);
Vikram S. Adve9d6f13f2002-09-15 21:51:04 +00001103 Out << "]";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001104 } else {
Chris Lattner59afbf32002-09-12 20:34:47 +00001105 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001106 }
1107}
1108
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001109void CWriter::visitLoadInst(LoadInst &I) {
Nick Hildenbrandtca626922002-10-02 21:14:33 +00001110 Out << "*";
Chris Lattner5dfe7672002-08-22 22:48:55 +00001111 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001112}
1113
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001114void CWriter::visitStoreInst(StoreInst &I) {
Nick Hildenbrandtca626922002-10-02 21:14:33 +00001115 Out << "*";
Chris Lattner5dfe7672002-08-22 22:48:55 +00001116 writeOperand(I.getPointerOperand());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001117 Out << " = ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001118 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001119}
1120
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001121void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
Nick Hildenbrandtca626922002-10-02 21:14:33 +00001122 Out << "&";
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001123 printIndexingExpression(I.getPointerOperand(), I.idx_begin(), I.idx_end());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001124}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001125
1126//===----------------------------------------------------------------------===//
1127// External Interface declaration
1128//===----------------------------------------------------------------------===//
1129
Chris Lattner0e9f93e2002-08-31 00:29:16 +00001130Pass *createWriteToCPass(std::ostream &o) { return new CWriter(o); }