blob: 2c4f6c2f9f152ec0f85d379925afdf5760bf9d2e [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 Lattner18ac3c82003-05-08 18:41:45 +000014#include "llvm/Intrinsics.h"
Chris Lattner3af3ba82002-05-09 21:31:18 +000015#include "llvm/SlotCalculator.h"
Chris Lattner0e9f93e2002-08-31 00:29:16 +000016#include "llvm/Analysis/FindUsedTypes.h"
Chris Lattnerd1cf1b42002-09-20 23:26:33 +000017#include "llvm/Analysis/ConstantsScanner.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000018#include "llvm/Support/InstVisitor.h"
Chris Lattner7683a122002-05-09 20:33:35 +000019#include "llvm/Support/InstIterator.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000020#include "Support/StringExtras.h"
21#include "Support/STLExtras.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000022#include <algorithm>
Chris Lattner78771c82002-05-17 04:55:35 +000023#include <set>
Nick Hildenbrandt98502372002-11-18 20:55:50 +000024#include <sstream>
Nick Hildenbrandt3cecdc52002-10-23 18:59:40 +000025
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000026namespace {
Chris Lattner0e9f93e2002-08-31 00:29:16 +000027 class CWriter : public Pass, public InstVisitor<CWriter> {
Chris Lattneredd8ce12003-05-03 03:14:35 +000028 std::ostream &Out;
Chris Lattner0e9f93e2002-08-31 00:29:16 +000029 SlotCalculator *Table;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000030 const Module *TheModule;
Chris Lattneredd8ce12003-05-03 03:14:35 +000031 std::map<const Type *, std::string> TypeNames;
Chris Lattner78771c82002-05-17 04:55:35 +000032 std::set<const Value*> MangledGlobals;
Nick Hildenbrandt3cecdc52002-10-23 18:59:40 +000033 bool needsMalloc;
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +000034
Chris Lattneredd8ce12003-05-03 03:14:35 +000035 std::map<const ConstantFP *, unsigned> FPConstantMap;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000036 public:
Chris Lattneredd8ce12003-05-03 03:14:35 +000037 CWriter(std::ostream &o) : Out(o) {}
Chris Lattner0e9f93e2002-08-31 00:29:16 +000038
39 void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.setPreservesAll();
41 AU.addRequired<FindUsedTypes>();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000042 }
Chris Lattner0e9f93e2002-08-31 00:29:16 +000043
44 virtual bool run(Module &M) {
45 // Initialize
46 Table = new SlotCalculator(&M, false);
47 TheModule = &M;
48
49 // Ensure that all structure types have names...
50 bool Changed = nameAllUsedStructureTypes(M);
51
52 // Run...
53 printModule(&M);
54
55 // Free memory...
56 delete Table;
57 TypeNames.clear();
58 MangledGlobals.clear();
59 return false;
60 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000061
Chris Lattneredd8ce12003-05-03 03:14:35 +000062 std::ostream &printType(std::ostream &Out, const Type *Ty,
63 const std::string &VariableName = "",
64 bool IgnoreName = false, bool namedContext = true);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000065
Chris Lattnerbb03efd2002-06-25 15:57:03 +000066 void writeOperand(Value *Operand);
67 void writeOperandInternal(Value *Operand);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000068
Chris Lattneredd8ce12003-05-03 03:14:35 +000069 std::string getValueName(const Value *V);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000070
Chris Lattner4fbf26d2002-05-09 20:53:56 +000071 private :
Chris Lattner0e9f93e2002-08-31 00:29:16 +000072 bool nameAllUsedStructureTypes(Module &M);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +000073 void printModule(Module *M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000074 void printSymbolTable(const SymbolTable &ST);
Chris Lattner2c601a72002-09-20 15:05:40 +000075 void printContainedStructs(const Type *Ty, std::set<const StructType *> &);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000076 void printGlobal(const GlobalVariable *GV);
Chris Lattner4cda8352002-08-20 16:55:48 +000077 void printFunctionSignature(const Function *F, bool Prototype);
78
Chris Lattner8c3c4bf2002-05-09 15:49:41 +000079 void printFunction(Function *);
Chris Lattnerb5af06a2002-05-09 03:06:06 +000080
Chris Lattner6d492922002-08-19 21:32:41 +000081 void printConstant(Constant *CPV);
82 void printConstantArray(ConstantArray *CPA);
83
Chris Lattnerd0c668c2002-05-09 21:18:38 +000084 // isInlinableInst - Attempt to inline instructions into their uses to build
85 // trees as much as possible. To do this, we have to consistently decide
86 // what is acceptable to inline, so that variable declarations don't get
87 // printed and an extra copy of the expr is not emitted.
88 //
Chris Lattnerbb03efd2002-06-25 15:57:03 +000089 static bool isInlinableInst(const Instruction &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +000090 // Must be an expression, must be used exactly once. If it is dead, we
91 // emit it inline where it would go.
Chris Lattnerbb03efd2002-06-25 15:57:03 +000092 if (I.getType() == Type::VoidTy || I.use_size() != 1 ||
Nick Hildenbrandt2cf2cbc2002-11-06 20:07:54 +000093 isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I) ||
94 isa<LoadInst>(I)) // Don't inline a load across a store!
Chris Lattnerd0c668c2002-05-09 21:18:38 +000095 return false;
96
97 // Only inline instruction it it's use is in the same BB as the inst.
Chris Lattnerbb03efd2002-06-25 15:57:03 +000098 return I.getParent() == cast<Instruction>(I.use_back())->getParent();
Chris Lattnerd0c668c2002-05-09 21:18:38 +000099 }
100
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000101 // Instruction visitation functions
102 friend class InstVisitor<CWriter>;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000103
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000104 void visitReturnInst(ReturnInst &I);
105 void visitBranchInst(BranchInst &I);
Chris Lattnera9f5e052003-04-22 20:19:52 +0000106 void visitSwitchInst(SwitchInst &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000107
Chris Lattner84e66652003-05-03 07:11:00 +0000108 void visitPHINode(PHINode &I);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000109 void visitBinaryOperator(Instruction &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000110
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000111 void visitCastInst (CastInst &I);
112 void visitCallInst (CallInst &I);
113 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000114
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000115 void visitMallocInst(MallocInst &I);
116 void visitAllocaInst(AllocaInst &I);
117 void visitFreeInst (FreeInst &I);
118 void visitLoadInst (LoadInst &I);
119 void visitStoreInst (StoreInst &I);
120 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner18ac3c82003-05-08 18:41:45 +0000121 void visitVarArgInst(VarArgInst &I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000122
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000123 void visitInstruction(Instruction &I) {
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000124 std::cerr << "C Writer does not know about " << I;
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000125 abort();
126 }
127
128 void outputLValue(Instruction *I) {
129 Out << " " << getValueName(I) << " = ";
130 }
131 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
132 unsigned Indent);
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000133 void printIndexingExpression(Value *Ptr, User::op_iterator I,
134 User::op_iterator E);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000135 };
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000136}
137
Chris Lattner2f499022002-05-09 04:21:21 +0000138// We dont want identifier names with ., space, - in them.
139// So we replace them with _
Chris Lattneredd8ce12003-05-03 03:14:35 +0000140static std::string makeNameProper(std::string x) {
141 std::string tmp;
142 for (std::string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
Chris Lattner2f499022002-05-09 04:21:21 +0000143 switch (*sI) {
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000144 case '.': tmp += "d_"; break;
145 case ' ': tmp += "s_"; break;
146 case '-': tmp += "D_"; break;
Chris Lattner2f499022002-05-09 04:21:21 +0000147 default: tmp += *sI;
148 }
149
150 return tmp;
151}
152
Chris Lattneredd8ce12003-05-03 03:14:35 +0000153std::string CWriter::getValueName(const Value *V) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000154 if (V->hasName()) { // Print out the label if it exists...
155 if (isa<GlobalValue>(V) && // Do not mangle globals...
Chris Lattneredd8ce12003-05-03 03:14:35 +0000156 (cast<GlobalValue>(V)->hasExternalLinkage() &&// Unless it's internal or
157 !MangledGlobals.count(V))) // Unless the name would collide if we don't
Chris Lattner2f499022002-05-09 04:21:21 +0000158 return makeNameProper(V->getName());
159
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000160 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
161 makeNameProper(V->getName());
Chris Lattner2f499022002-05-09 04:21:21 +0000162 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000163
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000164 int Slot = Table->getValSlot(V);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000165 assert(Slot >= 0 && "Invalid value!");
Chris Lattner2d05a1a2002-05-09 05:16:40 +0000166 return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000167}
168
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000169// A pointer type should not use parens around *'s alone, e.g., (**)
Chris Lattneredd8ce12003-05-03 03:14:35 +0000170inline bool ptrTypeNameNeedsParens(const std::string &NameSoFar) {
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000171 return (NameSoFar.find_last_not_of('*') != std::string::npos);
172}
173
Chris Lattner83c57752002-08-19 22:17:53 +0000174// Pass the Type* and the variable name and this prints out the variable
175// declaration.
176//
Chris Lattneredd8ce12003-05-03 03:14:35 +0000177std::ostream &CWriter::printType(std::ostream &Out, const Type *Ty,
178 const std::string &NameSoFar,
179 bool IgnoreName, bool namedContext) {
Chris Lattner83c57752002-08-19 22:17:53 +0000180 if (Ty->isPrimitiveType())
181 switch (Ty->getPrimitiveID()) {
182 case Type::VoidTyID: return Out << "void " << NameSoFar;
183 case Type::BoolTyID: return Out << "bool " << NameSoFar;
184 case Type::UByteTyID: return Out << "unsigned char " << NameSoFar;
185 case Type::SByteTyID: return Out << "signed char " << NameSoFar;
186 case Type::UShortTyID: return Out << "unsigned short " << NameSoFar;
187 case Type::ShortTyID: return Out << "short " << NameSoFar;
188 case Type::UIntTyID: return Out << "unsigned " << NameSoFar;
189 case Type::IntTyID: return Out << "int " << NameSoFar;
190 case Type::ULongTyID: return Out << "unsigned long long " << NameSoFar;
191 case Type::LongTyID: return Out << "signed long long " << NameSoFar;
192 case Type::FloatTyID: return Out << "float " << NameSoFar;
193 case Type::DoubleTyID: return Out << "double " << NameSoFar;
194 default :
195 std::cerr << "Unknown primitive type: " << Ty << "\n";
196 abort();
197 }
198
199 // Check to see if the type is named.
Chris Lattner04b72c82002-10-16 00:08:22 +0000200 if (!IgnoreName || isa<OpaqueType>(Ty)) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000201 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner83c57752002-08-19 22:17:53 +0000202 if (I != TypeNames.end()) {
203 return Out << I->second << " " << NameSoFar;
204 }
Chris Lattner04b72c82002-10-16 00:08:22 +0000205 }
Chris Lattner83c57752002-08-19 22:17:53 +0000206
Chris Lattner83c57752002-08-19 22:17:53 +0000207 switch (Ty->getPrimitiveID()) {
208 case Type::FunctionTyID: {
209 const FunctionType *MTy = cast<FunctionType>(Ty);
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000210 std::stringstream FunctionInards;
211 FunctionInards << " (" << NameSoFar << ") (";
Chris Lattner83c57752002-08-19 22:17:53 +0000212 for (FunctionType::ParamTypes::const_iterator
213 I = MTy->getParamTypes().begin(),
214 E = MTy->getParamTypes().end(); I != E; ++I) {
215 if (I != MTy->getParamTypes().begin())
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000216 FunctionInards << ", ";
217 printType(FunctionInards, *I, "");
Chris Lattner83c57752002-08-19 22:17:53 +0000218 }
219 if (MTy->isVarArg()) {
220 if (!MTy->getParamTypes().empty())
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000221 FunctionInards << ", ";
222 FunctionInards << "...";
Chris Lattner83c57752002-08-19 22:17:53 +0000223 }
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000224 FunctionInards << ")";
Chris Lattneredd8ce12003-05-03 03:14:35 +0000225 std::string tstr = FunctionInards.str();
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000226 printType(Out, MTy->getReturnType(), tstr);
227 return Out;
Chris Lattner83c57752002-08-19 22:17:53 +0000228 }
229 case Type::StructTyID: {
230 const StructType *STy = cast<StructType>(Ty);
231 Out << NameSoFar + " {\n";
232 unsigned Idx = 0;
233 for (StructType::ElementTypes::const_iterator
234 I = STy->getElementTypes().begin(),
235 E = STy->getElementTypes().end(); I != E; ++I) {
236 Out << " ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000237 printType(Out, *I, "field" + utostr(Idx++));
Chris Lattner83c57752002-08-19 22:17:53 +0000238 Out << ";\n";
239 }
240 return Out << "}";
241 }
242
243 case Type::PointerTyID: {
244 const PointerType *PTy = cast<PointerType>(Ty);
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000245 std::string ptrName = "*" + NameSoFar;
246
247 // Do not need parens around "* NameSoFar" if NameSoFar consists only
248 // of zero or more '*' chars *and* this is not an unnamed pointer type
249 // such as the result type in a cast statement. Otherwise, enclose in ( ).
Nick Hildenbrandtc14ded42002-09-23 21:02:50 +0000250 if (ptrTypeNameNeedsParens(NameSoFar) || !namedContext ||
251 PTy->getElementType()->getPrimitiveID() == Type::ArrayTyID)
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000252 ptrName = "(" + ptrName + ")"; //
253
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000254 return printType(Out, PTy->getElementType(), ptrName);
255 }Out <<"--";
Chris Lattner83c57752002-08-19 22:17:53 +0000256
257 case Type::ArrayTyID: {
258 const ArrayType *ATy = cast<ArrayType>(Ty);
259 unsigned NumElements = ATy->getNumElements();
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000260 return printType(Out, ATy->getElementType(),
Chris Lattner83c57752002-08-19 22:17:53 +0000261 NameSoFar + "[" + utostr(NumElements) + "]");
262 }
Chris Lattner04b72c82002-10-16 00:08:22 +0000263
264 case Type::OpaqueTyID: {
265 static int Count = 0;
Chris Lattneredd8ce12003-05-03 03:14:35 +0000266 std::string TyName = "struct opaque_" + itostr(Count++);
Chris Lattner04b72c82002-10-16 00:08:22 +0000267 assert(TypeNames.find(Ty) == TypeNames.end());
268 TypeNames[Ty] = TyName;
269 return Out << TyName << " " << NameSoFar;
270 }
Chris Lattner83c57752002-08-19 22:17:53 +0000271 default:
272 assert(0 && "Unhandled case in getTypeProps!");
273 abort();
274 }
275
276 return Out;
277}
278
Chris Lattner6d492922002-08-19 21:32:41 +0000279void CWriter::printConstantArray(ConstantArray *CPA) {
280
281 // As a special case, print the array as a string if it is an array of
282 // ubytes or an array of sbytes with positive values.
283 //
284 const Type *ETy = CPA->getType()->getElementType();
285 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
286
287 // Make sure the last character is a null char, as automatically added by C
288 if (CPA->getNumOperands() == 0 ||
289 !cast<Constant>(*(CPA->op_end()-1))->isNullValue())
290 isString = false;
291
292 if (isString) {
293 Out << "\"";
294 // Do not include the last character, which we know is null
295 for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) {
296 unsigned char C = (ETy == Type::SByteTy) ?
297 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
298 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
299
300 if (isprint(C)) {
Nick Hildenbrandt088b4722002-11-06 21:40:23 +0000301 if (C == '"' || C == '\\')
302 Out << "\\" << C;
Nick Hildenbrandtc3dd2af2002-09-30 21:11:55 +0000303 else
304 Out << C;
Chris Lattner6d492922002-08-19 21:32:41 +0000305 } else {
306 switch (C) {
307 case '\n': Out << "\\n"; break;
308 case '\t': Out << "\\t"; break;
309 case '\r': Out << "\\r"; break;
310 case '\v': Out << "\\v"; break;
311 case '\a': Out << "\\a"; break;
Nick Hildenbrandtc3dd2af2002-09-30 21:11:55 +0000312 case '\"': Out << "\\\""; break;
313 case '\'': Out << "\\\'"; break;
Chris Lattner6d492922002-08-19 21:32:41 +0000314 default:
315 Out << "\\x";
316 Out << ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
317 Out << ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
318 break;
319 }
320 }
321 }
322 Out << "\"";
323 } else {
324 Out << "{";
325 if (CPA->getNumOperands()) {
326 Out << " ";
327 printConstant(cast<Constant>(CPA->getOperand(0)));
328 for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
329 Out << ", ";
330 printConstant(cast<Constant>(CPA->getOperand(i)));
331 }
332 }
333 Out << " }";
334 }
335}
336
337
338// printConstant - The LLVM Constant to C Constant converter.
339void CWriter::printConstant(Constant *CPV) {
340 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
341 switch (CE->getOpcode()) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000342 case Instruction::Cast:
343 Out << "((";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000344 printType(Out, CPV->getType());
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000345 Out << ")";
346 printConstant(cast<Constant>(CPV->getOperand(0)));
347 Out << ")";
348 return;
349
350 case Instruction::GetElementPtr:
Nick Hildenbrandtd2eb3862002-10-03 20:47:24 +0000351 Out << "(&(";
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000352 printIndexingExpression(CPV->getOperand(0),
353 CPV->op_begin()+1, CPV->op_end());
Nick Hildenbrandtd2eb3862002-10-03 20:47:24 +0000354 Out << "))";
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000355 return;
356 case Instruction::Add:
357 Out << "(";
358 printConstant(cast<Constant>(CPV->getOperand(0)));
359 Out << " + ";
360 printConstant(cast<Constant>(CPV->getOperand(1)));
361 Out << ")";
362 return;
363 case Instruction::Sub:
364 Out << "(";
365 printConstant(cast<Constant>(CPV->getOperand(0)));
366 Out << " - ";
367 printConstant(cast<Constant>(CPV->getOperand(1)));
368 Out << ")";
369 return;
370
Chris Lattner6d492922002-08-19 21:32:41 +0000371 default:
372 std::cerr << "CWriter Error: Unhandled constant expression: "
373 << CE << "\n";
374 abort();
375 }
376 }
377
378 switch (CPV->getType()->getPrimitiveID()) {
379 case Type::BoolTyID:
380 Out << (CPV == ConstantBool::False ? "0" : "1"); break;
381 case Type::SByteTyID:
382 case Type::ShortTyID:
383 case Type::IntTyID:
384 Out << cast<ConstantSInt>(CPV)->getValue(); break;
385 case Type::LongTyID:
386 Out << cast<ConstantSInt>(CPV)->getValue() << "ll"; break;
387
388 case Type::UByteTyID:
389 case Type::UShortTyID:
390 Out << cast<ConstantUInt>(CPV)->getValue(); break;
391 case Type::UIntTyID:
392 Out << cast<ConstantUInt>(CPV)->getValue() << "u"; break;
393 case Type::ULongTyID:
394 Out << cast<ConstantUInt>(CPV)->getValue() << "ull"; break;
395
396 case Type::FloatTyID:
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000397 case Type::DoubleTyID: {
398 ConstantFP *FPC = cast<ConstantFP>(CPV);
Chris Lattneredd8ce12003-05-03 03:14:35 +0000399 std::map<const ConstantFP*, unsigned>::iterator I = FPConstantMap.find(FPC);
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000400 if (I != FPConstantMap.end()) {
401 // Because of FP precision problems we must load from a stack allocated
402 // value that holds the value in hex.
403 Out << "(*(" << (FPC->getType() == Type::FloatTy ? "float" : "double")
404 << "*)&FloatConstant" << I->second << ")";
405 } else {
406 Out << FPC->getValue();
407 }
408 break;
409 }
Chris Lattner6d492922002-08-19 21:32:41 +0000410
411 case Type::ArrayTyID:
412 printConstantArray(cast<ConstantArray>(CPV));
413 break;
414
415 case Type::StructTyID: {
416 Out << "{";
417 if (CPV->getNumOperands()) {
418 Out << " ";
419 printConstant(cast<Constant>(CPV->getOperand(0)));
420 for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) {
421 Out << ", ";
422 printConstant(cast<Constant>(CPV->getOperand(i)));
423 }
424 }
425 Out << " }";
426 break;
427 }
428
429 case Type::PointerTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000430 if (isa<ConstantPointerNull>(CPV)) {
Nick Hildenbrandtf0fca362002-10-28 19:54:06 +0000431 Out << "(NULL)";
Chris Lattner6d492922002-08-19 21:32:41 +0000432 break;
433 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
434 writeOperand(CPR->getValue());
435 break;
436 }
437 // FALL THROUGH
438 default:
439 std::cerr << "Unknown constant type: " << CPV << "\n";
440 abort();
441 }
442}
443
444void CWriter::writeOperandInternal(Value *Operand) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000445 if (Instruction *I = dyn_cast<Instruction>(Operand))
446 if (isInlinableInst(*I)) {
447 // Should we inline this instruction to build a tree?
448 Out << "(";
449 visit(*I);
450 Out << ")";
451 return;
452 }
453
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +0000454 if (Operand->hasName()) {
Chris Lattner6d492922002-08-19 21:32:41 +0000455 Out << getValueName(Operand);
456 } else if (Constant *CPV = dyn_cast<Constant>(Operand)) {
457 printConstant(CPV);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000458 } else {
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000459 int Slot = Table->getValSlot(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000460 assert(Slot >= 0 && "Malformed LLVM!");
461 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
462 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000463}
464
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000465void CWriter::writeOperand(Value *Operand) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000466 if (isa<GlobalVariable>(Operand))
467 Out << "(&"; // Global variables are references as their addresses by llvm
468
469 writeOperandInternal(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000470
471 if (isa<GlobalVariable>(Operand))
472 Out << ")";
473}
474
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000475// nameAllUsedStructureTypes - If there are structure types in the module that
476// are used but do not have names assigned to them in the symbol table yet then
477// we assign them names now.
478//
479bool CWriter::nameAllUsedStructureTypes(Module &M) {
480 // Get a set of types that are used by the program...
481 std::set<const Type *> UT = getAnalysis<FindUsedTypes>().getTypes();
482
483 // Loop over the module symbol table, removing types from UT that are already
484 // named.
485 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000486 SymbolTable &MST = M.getSymbolTable();
487 if (MST.find(Type::TypeTy) != MST.end())
488 for (SymbolTable::type_iterator I = MST.type_begin(Type::TypeTy),
489 E = MST.type_end(Type::TypeTy); I != E; ++I)
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000490 UT.erase(cast<Type>(I->second));
491
492 // UT now contains types that are not named. Loop over it, naming structure
493 // types.
494 //
495 bool Changed = false;
496 for (std::set<const Type *>::const_iterator I = UT.begin(), E = UT.end();
497 I != E; ++I)
498 if (const StructType *ST = dyn_cast<StructType>(*I)) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000499 ((Value*)ST)->setName("unnamed", &MST);
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000500 Changed = true;
501 }
502 return Changed;
503}
504
Chris Lattneredd8ce12003-05-03 03:14:35 +0000505static void generateAllocaDecl(std::ostream& Out) {
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000506 // On SunOS, we need to insert the alloca macro & proto for the builtin.
507 Out << "#ifdef sun\n"
508 << "extern void *__builtin_alloca(unsigned long);\n"
509 << "#define alloca(x) __builtin_alloca(x)\n"
510 << "#else\n"
511 << "#include <alloca.h>\n"
512 << "#endif\n\n";
513}
514
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000515void CWriter::printModule(Module *M) {
Chris Lattner78771c82002-05-17 04:55:35 +0000516 // Calculate which global values have names that will collide when we throw
517 // away type information.
Chris Lattner594b9fa2002-05-21 21:10:04 +0000518 { // Scope to delete the FoundNames set when we are done with it...
Chris Lattneredd8ce12003-05-03 03:14:35 +0000519 std::set<std::string> FoundNames;
Chris Lattner78771c82002-05-17 04:55:35 +0000520 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000521 if (I->hasName()) // If the global has a name...
522 if (FoundNames.count(I->getName())) // And the name is already used
523 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000524 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000525 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000526
527 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000528 if (I->hasName()) // If the global has a name...
529 if (FoundNames.count(I->getName())) // And the name is already used
530 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000531 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000532 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000533 }
534
Chris Lattner16c7bb22002-05-09 02:28:59 +0000535 // get declaration for alloca
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000536 Out << "/* Provide Declarations */\n";
537 generateAllocaDecl(Out);
Chris Lattner18ac3c82003-05-08 18:41:45 +0000538 Out << "#include <stdarg.h>\n";
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000539
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 Lattner18ac3c82003-05-08 18:41:45 +0000576 // Sometimes the bytecode likes to have multiple "declarations" for
Chris Lattneredd8ce12003-05-03 03:14:35 +0000577 // external functions
Chris Lattner18ac3c82003-05-08 18:41:45 +0000578 if ((I->hasInternalLinkage() || !MangledGlobals.count(I)) &&
579 !I->getIntrinsicID()) {
Nick Hildenbrandta1a64f82002-11-18 22:21:52 +0000580 printFunctionSignature(I, true);
581 Out << ";\n";
582 }
Chris Lattner4cda8352002-08-20 16:55:48 +0000583 }
Chris Lattnera4d4a852002-08-19 21:48:40 +0000584 }
585
Nick Hildenbrandt3cecdc52002-10-23 18:59:40 +0000586 // Print Malloc prototype if needed
587 if (needsMalloc){
588 Out << "\n/* Malloc to make sun happy */\n";
589 Out << "extern void * malloc(size_t);\n\n";
590 }
591
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +0000592 // Output the global variable declerations
593 if (!M->gempty()) {
594 Out << "\n\n/* Global Variable Declerations */\n";
595 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
596 if (!I->isExternal()) {
597 Out << "extern ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000598 printType(Out, I->getType()->getElementType(), getValueName(I));
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +0000599
600 Out << ";\n";
601 }
602 }
603
604
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000605 // Output the global variable definitions and contents...
Chris Lattnera4d4a852002-08-19 21:48:40 +0000606 if (!M->gempty()) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000607 Out << "\n\n/* Global Variable Definitions and Initialization */\n";
Chris Lattnere8e035b2002-10-16 20:08:47 +0000608 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
609 if (!I->isExternal()) {
610 if (I->hasInternalLinkage())
611 Out << "static ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000612 printType(Out, I->getType()->getElementType(), getValueName(I));
Chris Lattnera4d4a852002-08-19 21:48:40 +0000613
Chris Lattnera4d4a852002-08-19 21:48:40 +0000614 Out << " = " ;
615 writeOperand(I->getInitializer());
Chris Lattnere8e035b2002-10-16 20:08:47 +0000616 Out << ";\n";
Chris Lattnera4d4a852002-08-19 21:48:40 +0000617 }
Chris Lattnera4d4a852002-08-19 21:48:40 +0000618 }
619
Chris Lattner16c7bb22002-05-09 02:28:59 +0000620 // Output all of the functions...
Chris Lattnera4d4a852002-08-19 21:48:40 +0000621 if (!M->empty()) {
622 Out << "\n\n/* Function Bodies */\n";
623 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
624 printFunction(I);
625 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000626}
627
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000628
Chris Lattner2db41cd2002-09-20 15:12:13 +0000629/// printSymbolTable - Run through symbol table looking for type names. If a
630/// type name is found, emit it's declaration...
Chris Lattnera4c047e2002-09-20 14:56:54 +0000631///
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000632void CWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattner2db41cd2002-09-20 15:12:13 +0000633 // If there are no type names, exit early.
634 if (ST.find(Type::TypeTy) == ST.end())
635 return;
636
637 // We are only interested in the type plane of the symbol table...
638 SymbolTable::type_const_iterator I = ST.type_begin(Type::TypeTy);
639 SymbolTable::type_const_iterator End = ST.type_end(Type::TypeTy);
640
Chris Lattner58d04d42002-09-20 15:18:30 +0000641 // Print out forward declarations for structure types before anything else!
642 Out << "/* Structure forward decls */\n";
643 for (; I != End; ++I)
644 if (const Type *STy = dyn_cast<StructType>(I->second)) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000645 std::string Name = "struct l_" + makeNameProper(I->first);
Chris Lattner58d04d42002-09-20 15:18:30 +0000646 Out << Name << ";\n";
647 TypeNames.insert(std::make_pair(STy, Name));
648 }
649
650 Out << "\n";
651
652 // Now we can print out typedefs...
653 Out << "/* Typedefs */\n";
654 for (I = ST.type_begin(Type::TypeTy); I != End; ++I) {
655 const Type *Ty = cast<Type>(I->second);
Chris Lattneredd8ce12003-05-03 03:14:35 +0000656 std::string Name = "l_" + makeNameProper(I->first);
Chris Lattner58d04d42002-09-20 15:18:30 +0000657 Out << "typedef ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000658 printType(Out, Ty, Name);
Chris Lattner58d04d42002-09-20 15:18:30 +0000659 Out << ";\n";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000660 }
661
662 Out << "\n";
663
Chris Lattner2c601a72002-09-20 15:05:40 +0000664 // Keep track of which structures have been printed so far...
665 std::set<const StructType *> StructPrinted;
666
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000667 // Loop over all structures then push them into the stack so they are
668 // printed in the correct order.
Chris Lattner2db41cd2002-09-20 15:12:13 +0000669 //
Chris Lattner58d04d42002-09-20 15:18:30 +0000670 Out << "/* Structure contents */\n";
Chris Lattner2db41cd2002-09-20 15:12:13 +0000671 for (I = ST.type_begin(Type::TypeTy); I != End; ++I)
672 if (const StructType *STy = dyn_cast<StructType>(I->second))
673 printContainedStructs(STy, StructPrinted);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000674}
675
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000676// Push the struct onto the stack and recursively push all structs
677// this one depends on.
Chris Lattner2c601a72002-09-20 15:05:40 +0000678void CWriter::printContainedStructs(const Type *Ty,
679 std::set<const StructType*> &StructPrinted){
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000680 if (const StructType *STy = dyn_cast<StructType>(Ty)){
681 //Check to see if we have already printed this struct
Chris Lattner2c601a72002-09-20 15:05:40 +0000682 if (StructPrinted.count(STy) == 0) {
683 // Print all contained types first...
Chris Lattnera4c047e2002-09-20 14:56:54 +0000684 for (StructType::ElementTypes::const_iterator
685 I = STy->getElementTypes().begin(),
686 E = STy->getElementTypes().end(); I != E; ++I) {
Chris Lattner2c601a72002-09-20 15:05:40 +0000687 const Type *Ty1 = I->get();
Chris Lattnera4c047e2002-09-20 14:56:54 +0000688 if (isa<StructType>(Ty1) || isa<ArrayType>(Ty1))
Chris Lattner2c601a72002-09-20 15:05:40 +0000689 printContainedStructs(Ty1, StructPrinted);
Chris Lattnera4c047e2002-09-20 14:56:54 +0000690 }
691
Chris Lattner2c601a72002-09-20 15:05:40 +0000692 //Print structure type out..
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000693 StructPrinted.insert(STy);
Chris Lattneredd8ce12003-05-03 03:14:35 +0000694 std::string Name = TypeNames[STy];
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000695 printType(Out, STy, Name, true);
Chris Lattner58d04d42002-09-20 15:18:30 +0000696 Out << ";\n\n";
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000697 }
Chris Lattnera4c047e2002-09-20 14:56:54 +0000698
Chris Lattner2c601a72002-09-20 15:05:40 +0000699 // If it is an array, check contained types and continue
700 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)){
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000701 const Type *Ty1 = ATy->getElementType();
702 if (isa<StructType>(Ty1) || isa<ArrayType>(Ty1))
Chris Lattner2c601a72002-09-20 15:05:40 +0000703 printContainedStructs(Ty1, StructPrinted);
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000704 }
705}
706
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000707
Chris Lattner4cda8352002-08-20 16:55:48 +0000708void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
Nick Hildenbrandt3cecdc52002-10-23 18:59:40 +0000709 // If the program provides it's own malloc prototype we don't need
710 // to include the general one.
711 if (getValueName(F) == "malloc")
712 needsMalloc = false;
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000713 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000714 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000715 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000716
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000717 std::stringstream FunctionInards;
718
719 // Print out the name...
720 FunctionInards << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000721
Chris Lattner16c7bb22002-05-09 02:28:59 +0000722 if (!F->isExternal()) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000723 if (!F->aempty()) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000724 std::string ArgName;
Chris Lattner4cda8352002-08-20 16:55:48 +0000725 if (F->abegin()->hasName() || !Prototype)
726 ArgName = getValueName(F->abegin());
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000727 printType(FunctionInards, F->afront().getType(), ArgName);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000728 for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
729 I != E; ++I) {
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000730 FunctionInards << ", ";
Chris Lattner4cda8352002-08-20 16:55:48 +0000731 if (I->hasName() || !Prototype)
732 ArgName = getValueName(I);
733 else
734 ArgName = "";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000735 printType(FunctionInards, I->getType(), ArgName);
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000736 }
737 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000738 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000739 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000740 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000741 FT->getParamTypes().begin(),
742 E = FT->getParamTypes().end(); I != E; ++I) {
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000743 if (I != FT->getParamTypes().begin()) FunctionInards << ", ";
744 printType(FunctionInards, *I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000745 }
746 }
747
Chris Lattner1f8c4a12002-09-20 22:32:30 +0000748 // Finish printing arguments... if this is a vararg function, print the ...,
749 // unless there are no known types, in which case, we just emit ().
750 //
751 if (FT->isVarArg() && !FT->getParamTypes().empty()) {
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000752 if (FT->getParamTypes().size()) FunctionInards << ", ";
753 FunctionInards << "..."; // Output varargs portion of signature!
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000754 }
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000755 FunctionInards << ")";
756 // Print out the return type and the entire signature for that matter
757 printType(Out, F->getReturnType(), FunctionInards.str());
758
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000759}
760
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000761
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000762void CWriter::printFunction(Function *F) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000763 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000764
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000765 Table->incorporateFunction(F);
Chris Lattnerf34ee812002-05-09 03:12:34 +0000766
Chris Lattner4cda8352002-08-20 16:55:48 +0000767 printFunctionSignature(F, false);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000768 Out << " {\n";
769
Chris Lattner497e19a2002-05-09 20:39:03 +0000770 // print local variable information for the function
Chris Lattner7683a122002-05-09 20:33:35 +0000771 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000772 if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000773 Out << " ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000774 printType(Out, (*I)->getType(), getValueName(*I));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000775 Out << ";\n";
Chris Lattner84e66652003-05-03 07:11:00 +0000776
777 if (isa<PHINode>(*I)) { // Print out PHI node temporaries as well...
778 Out << " ";
779 printType(Out, (*I)->getType(), getValueName(*I)+"__PHI_TEMPORARY");
780 Out << ";\n";
781 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000782 }
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000783
784 Out << "\n";
785
786 // Scan the function for floating point constants. If any FP constant is used
787 // in the function, we want to redirect it here so that we do not depend on
788 // the precision of the printed form.
789 //
790 unsigned FPCounter = 0;
791 for (constant_iterator I = constant_begin(F), E = constant_end(F); I != E;++I)
792 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(*I))
793 if (FPConstantMap.find(FPC) == FPConstantMap.end()) {
794 double Val = FPC->getValue();
795
796 FPConstantMap[FPC] = FPCounter; // Number the FP constants
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000797
798 if (FPC->getType() == Type::DoubleTy)
799 Out << " const ConstantDoubleTy FloatConstant" << FPCounter++
800 << " = 0x" << std::hex << *(unsigned long long*)&Val << std::dec
801 << "; /* " << Val << " */\n";
Chris Lattner9c1338b2002-11-07 22:12:53 +0000802 else if (FPC->getType() == Type::FloatTy) {
803 float fVal = Val;
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000804 Out << " const ConstantFloatTy FloatConstant" << FPCounter++
Chris Lattner9c1338b2002-11-07 22:12:53 +0000805 << " = 0x" << std::hex << *(unsigned*)&fVal << std::dec
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000806 << "; /* " << Val << " */\n";
Chris Lattner9c1338b2002-11-07 22:12:53 +0000807 } else
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000808 assert(0 && "Unknown float type!");
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000809 }
810
811 Out << "\n";
Chris Lattner16c7bb22002-05-09 02:28:59 +0000812
813 // print the basic blocks
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000814 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
815 BasicBlock *Prev = BB->getPrev();
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000816
817 // Don't print the label for the basic block if there are no uses, or if the
818 // only terminator use is the precessor basic block's terminator. We have
819 // to scan the use list because PHI nodes use basic blocks too but do not
820 // require a label to be generated.
821 //
822 bool NeedsLabel = false;
823 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
824 UI != UE; ++UI)
825 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
Chris Lattnerf1acd962003-04-23 19:15:13 +0000826 if (TI != Prev->getTerminator() ||
827 isa<SwitchInst>(Prev->getTerminator())) {
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000828 NeedsLabel = true;
829 break;
830 }
831
832 if (NeedsLabel) Out << getValueName(BB) << ":\n";
833
834 // Output all of the instructions in the basic block...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000835 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II){
Chris Lattner84e66652003-05-03 07:11:00 +0000836 if (!isInlinableInst(*II)) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000837 if (II->getType() != Type::VoidTy)
838 outputLValue(II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000839 else
840 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000841 visit(*II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000842 Out << ";\n";
843 }
844 }
845
846 // Don't emit prefix or suffix for the terminator...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000847 visit(*BB->getTerminator());
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000848 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000849
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000850 Out << "}\n\n";
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000851 Table->purgeFunction();
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000852 FPConstantMap.clear();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000853}
854
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000855// Specific Instruction type classes... note that all of the casts are
856// neccesary because we use the instruction classes as opaque types...
857//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000858void CWriter::visitReturnInst(ReturnInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000859 // Don't output a void return if this is the last basic block in the function
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000860 if (I.getNumOperands() == 0 &&
861 &*--I.getParent()->getParent()->end() == I.getParent() &&
862 !I.getParent()->size() == 1) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000863 return;
Chris Lattner963b70b2002-05-21 18:05:19 +0000864 }
Chris Lattner44408262002-05-09 03:50:42 +0000865
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000866 Out << " return";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000867 if (I.getNumOperands()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000868 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000869 writeOperand(I.getOperand(0));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000870 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000871 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000872}
873
Chris Lattnera9f5e052003-04-22 20:19:52 +0000874void CWriter::visitSwitchInst(SwitchInst &SI) {
875 Out << " switch (";
876 writeOperand(SI.getOperand(0));
Chris Lattnerf1acd962003-04-23 19:15:13 +0000877 Out << ") {\n default:\n";
878 printBranchToBlock(SI.getParent(), SI.getDefaultDest(), 2);
Chris Lattnera9f5e052003-04-22 20:19:52 +0000879 Out << ";\n";
880 for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) {
881 Out << " case ";
882 writeOperand(SI.getOperand(i));
883 Out << ":\n";
884 BasicBlock *Succ = cast<BasicBlock>(SI.getOperand(i+1));
885 printBranchToBlock(SI.getParent(), Succ, 2);
886 if (Succ == SI.getParent()->getNext())
887 Out << " break;\n";
888 }
889 Out << " }\n";
890}
891
892
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000893static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
894 // If PHI nodes need copies, we need the copy code...
895 if (isa<PHINode>(To->front()) ||
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000896 From->getNext() != To) // Not directly successor, need goto
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000897 return true;
898
899 // Otherwise we don't need the code.
900 return false;
901}
902
903void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
904 unsigned Indent) {
905 for (BasicBlock::iterator I = Succ->begin();
Chris Lattner2ee82e02003-04-23 16:36:11 +0000906 PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000907 // now we have to do the printing
Chris Lattneredd8ce12003-05-03 03:14:35 +0000908 Out << std::string(Indent, ' ');
Chris Lattner84e66652003-05-03 07:11:00 +0000909 Out << " " << getValueName(I) << "__PHI_TEMPORARY = ";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000910 writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
911 Out << "; /* for PHI node */\n";
912 }
913
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000914 if (CurBB->getNext() != Succ) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000915 Out << std::string(Indent, ' ') << " goto ";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000916 writeOperand(Succ);
917 Out << ";\n";
918 }
919}
920
921// Brach instruction printing - Avoid printing out a brach to a basic block that
922// immediately succeeds the current one.
923//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000924void CWriter::visitBranchInst(BranchInst &I) {
925 if (I.isConditional()) {
926 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(0))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000927 Out << " if (";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000928 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000929 Out << ") {\n";
930
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000931 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000932
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000933 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(1))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000934 Out << " } else {\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000935 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000936 }
937 } else {
938 // First goto not neccesary, assume second one is...
939 Out << " if (!";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000940 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000941 Out << ") {\n";
942
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000943 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000944 }
945
946 Out << " }\n";
947 } else {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000948 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000949 }
950 Out << "\n";
951}
952
Chris Lattner84e66652003-05-03 07:11:00 +0000953// PHI nodes get copied into temporary values at the end of predecessor basic
954// blocks. We now need to copy these temporary values into the REAL value for
955// the PHI.
956void CWriter::visitPHINode(PHINode &I) {
957 writeOperand(&I);
958 Out << "__PHI_TEMPORARY";
959}
960
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000961
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000962void CWriter::visitBinaryOperator(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000963 // binary instructions, shift instructions, setCond instructions.
Chris Lattnerf5612b762003-04-23 19:09:22 +0000964 assert(!isa<PointerType>(I.getType()));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000965
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000966 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000967
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000968 switch (I.getOpcode()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000969 case Instruction::Add: Out << " + "; break;
970 case Instruction::Sub: Out << " - "; break;
971 case Instruction::Mul: Out << "*"; break;
972 case Instruction::Div: Out << "/"; break;
973 case Instruction::Rem: Out << "%"; break;
974 case Instruction::And: Out << " & "; break;
975 case Instruction::Or: Out << " | "; break;
976 case Instruction::Xor: Out << " ^ "; break;
977 case Instruction::SetEQ: Out << " == "; break;
978 case Instruction::SetNE: Out << " != "; break;
979 case Instruction::SetLE: Out << " <= "; break;
980 case Instruction::SetGE: Out << " >= "; break;
981 case Instruction::SetLT: Out << " < "; break;
982 case Instruction::SetGT: Out << " > "; break;
983 case Instruction::Shl : Out << " << "; break;
984 case Instruction::Shr : Out << " >> "; break;
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000985 default: std::cerr << "Invalid operator type!" << I; abort();
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000986 }
987
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000988 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000989}
990
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000991void CWriter::visitCastInst(CastInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000992 Out << "(";
Chris Lattneredd8ce12003-05-03 03:14:35 +0000993 printType(Out, I.getType(), "", /*ignoreName*/false, /*namedContext*/false);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000994 Out << ")";
Chris Lattnerf5612b762003-04-23 19:09:22 +0000995 if (isa<PointerType>(I.getType())&&I.getOperand(0)->getType()->isIntegral() ||
996 isa<PointerType>(I.getOperand(0)->getType())&&I.getType()->isIntegral()) {
997 // Avoid "cast to pointer from integer of different size" warnings
998 Out << "(long)";
999 }
1000
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001001 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001002}
1003
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001004void CWriter::visitCallInst(CallInst &I) {
Chris Lattner18ac3c82003-05-08 18:41:45 +00001005 // Handle intrinsic function calls first...
1006 if (Function *F = I.getCalledFunction())
1007 if (LLVMIntrinsic::ID ID = (LLVMIntrinsic::ID)F->getIntrinsicID()) {
1008 switch (ID) {
1009 default: assert(0 && "Unknown LLVM intrinsic!");
1010 case LLVMIntrinsic::va_start:
1011 Out << "va_start((va_list)*";
1012 writeOperand(I.getOperand(1));
1013 Out << ", ";
1014 // Output the last argument to the enclosing function...
1015 writeOperand(&I.getParent()->getParent()->aback());
1016 Out << ")";
1017 return;
1018
1019 case LLVMIntrinsic::va_end:
1020 Out << "va_end((va_list)*";
1021 writeOperand(I.getOperand(1));
1022 Out << ")";
1023 return;
1024 case LLVMIntrinsic::va_copy:
1025 Out << "va_copy((va_list)*";
1026 writeOperand(I.getOperand(1));
1027 Out << ", (va_list)";
1028 writeOperand(I.getOperand(2));
1029 Out << ")";
1030 return;
1031 }
1032 }
1033
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001034 const PointerType *PTy = cast<PointerType>(I.getCalledValue()->getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001035 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1036 const Type *RetTy = FTy->getReturnType();
1037
Chris Lattnerfabc8802002-08-26 20:50:09 +00001038 writeOperand(I.getOperand(0));
1039 Out << "(";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001040
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001041 if (I.getNumOperands() > 1) {
1042 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001043
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001044 for (unsigned op = 2, Eop = I.getNumOperands(); op != Eop; ++op) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001045 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001046 writeOperand(I.getOperand(op));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001047 }
1048 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001049 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001050}
1051
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001052void CWriter::visitMallocInst(MallocInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001053 Out << "(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001054 printType(Out, I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001055 Out << ")malloc(sizeof(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001056 printType(Out, I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001057 Out << ")";
1058
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001059 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001060 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001061 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001062 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001063 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001064}
1065
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001066void CWriter::visitAllocaInst(AllocaInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001067 Out << "(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001068 printType(Out, I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001069 Out << ") alloca(sizeof(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001070 printType(Out, I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001071 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001072 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001073 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001074 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001075 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001076 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001077}
1078
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001079void CWriter::visitFreeInst(FreeInst &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001080 Out << "free(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001081 writeOperand(I.getOperand(0));
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001082 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001083}
1084
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001085void CWriter::printIndexingExpression(Value *Ptr, User::op_iterator I,
1086 User::op_iterator E) {
1087 bool HasImplicitAddress = false;
1088 // If accessing a global value with no indexing, avoid *(&GV) syndrome
1089 if (GlobalValue *V = dyn_cast<GlobalValue>(Ptr)) {
1090 HasImplicitAddress = true;
1091 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Ptr)) {
1092 HasImplicitAddress = true;
1093 Ptr = CPR->getValue(); // Get to the global...
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001094 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001095
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001096 if (I == E) {
1097 if (!HasImplicitAddress)
1098 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001099
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001100 writeOperandInternal(Ptr);
1101 return;
1102 }
1103
1104 const Constant *CI = dyn_cast<Constant>(I->get());
1105 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
1106 Out << "(&";
1107
1108 writeOperandInternal(Ptr);
1109
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001110 if (HasImplicitAddress && (!CI || !CI->isNullValue())) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001111 Out << ")";
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001112 HasImplicitAddress = false; // HIA is only true if we haven't addressed yet
1113 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001114
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001115 assert(!HasImplicitAddress || (CI && CI->isNullValue()) &&
1116 "Can only have implicit address with direct accessing");
1117
1118 if (HasImplicitAddress) {
1119 ++I;
1120 } else if (CI && CI->isNullValue() && I+1 != E) {
1121 // Print out the -> operator if possible...
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001122 if ((*(I+1))->getType() == Type::UByteTy) {
1123 Out << (HasImplicitAddress ? "." : "->");
1124 Out << "field" << cast<ConstantUInt>(*(I+1))->getValue();
1125 I += 2;
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001126 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001127 }
Chris Lattner5dfe7672002-08-22 22:48:55 +00001128
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001129 for (; I != E; ++I)
Chris Lattner106ff452002-09-11 01:21:29 +00001130 if ((*I)->getType() == Type::LongTy) {
Vikram S. Adve9d6f13f2002-09-15 21:51:04 +00001131 Out << "[";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001132 writeOperand(*I);
Vikram S. Adve9d6f13f2002-09-15 21:51:04 +00001133 Out << "]";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001134 } else {
Chris Lattner59afbf32002-09-12 20:34:47 +00001135 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001136 }
1137}
1138
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001139void CWriter::visitLoadInst(LoadInst &I) {
Nick Hildenbrandtca626922002-10-02 21:14:33 +00001140 Out << "*";
Chris Lattner5dfe7672002-08-22 22:48:55 +00001141 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001142}
1143
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001144void CWriter::visitStoreInst(StoreInst &I) {
Nick Hildenbrandtca626922002-10-02 21:14:33 +00001145 Out << "*";
Chris Lattner5dfe7672002-08-22 22:48:55 +00001146 writeOperand(I.getPointerOperand());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001147 Out << " = ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001148 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001149}
1150
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001151void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
Nick Hildenbrandtca626922002-10-02 21:14:33 +00001152 Out << "&";
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001153 printIndexingExpression(I.getPointerOperand(), I.idx_begin(), I.idx_end());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001154}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001155
Chris Lattner18ac3c82003-05-08 18:41:45 +00001156void CWriter::visitVarArgInst(VarArgInst &I) {
1157 Out << "va_arg((va_list)*";
1158 writeOperand(I.getOperand(0));
1159 Out << ", ";
1160 printType(Out, I.getType(), "", /*ignoreName*/false, /*namedContext*/false);
1161 Out << ")";
1162}
1163
1164
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001165//===----------------------------------------------------------------------===//
1166// External Interface declaration
1167//===----------------------------------------------------------------------===//
1168
Chris Lattner0e9f93e2002-08-31 00:29:16 +00001169Pass *createWriteToCPass(std::ostream &o) { return new CWriter(o); }