blob: 577054e245ce6b48696b963a6d9d076334fdccf2 [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())
Chris Lattnerddfc03c2003-05-13 20:15:37 +0000221 FunctionInards << ", ...";
222 } else if (MTy->getParamTypes().empty()) {
223 FunctionInards << "void";
Chris Lattner83c57752002-08-19 22:17:53 +0000224 }
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000225 FunctionInards << ")";
Chris Lattneredd8ce12003-05-03 03:14:35 +0000226 std::string tstr = FunctionInards.str();
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000227 printType(Out, MTy->getReturnType(), tstr);
228 return Out;
Chris Lattner83c57752002-08-19 22:17:53 +0000229 }
230 case Type::StructTyID: {
231 const StructType *STy = cast<StructType>(Ty);
232 Out << NameSoFar + " {\n";
233 unsigned Idx = 0;
234 for (StructType::ElementTypes::const_iterator
235 I = STy->getElementTypes().begin(),
236 E = STy->getElementTypes().end(); I != E; ++I) {
237 Out << " ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000238 printType(Out, *I, "field" + utostr(Idx++));
Chris Lattner83c57752002-08-19 22:17:53 +0000239 Out << ";\n";
240 }
241 return Out << "}";
242 }
243
244 case Type::PointerTyID: {
245 const PointerType *PTy = cast<PointerType>(Ty);
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000246 std::string ptrName = "*" + NameSoFar;
247
248 // Do not need parens around "* NameSoFar" if NameSoFar consists only
249 // of zero or more '*' chars *and* this is not an unnamed pointer type
250 // such as the result type in a cast statement. Otherwise, enclose in ( ).
Nick Hildenbrandtc14ded42002-09-23 21:02:50 +0000251 if (ptrTypeNameNeedsParens(NameSoFar) || !namedContext ||
252 PTy->getElementType()->getPrimitiveID() == Type::ArrayTyID)
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000253 ptrName = "(" + ptrName + ")"; //
254
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000255 return printType(Out, PTy->getElementType(), ptrName);
256 }Out <<"--";
Chris Lattner83c57752002-08-19 22:17:53 +0000257
258 case Type::ArrayTyID: {
259 const ArrayType *ATy = cast<ArrayType>(Ty);
260 unsigned NumElements = ATy->getNumElements();
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000261 return printType(Out, ATy->getElementType(),
Chris Lattner83c57752002-08-19 22:17:53 +0000262 NameSoFar + "[" + utostr(NumElements) + "]");
263 }
Chris Lattner04b72c82002-10-16 00:08:22 +0000264
265 case Type::OpaqueTyID: {
266 static int Count = 0;
Chris Lattneredd8ce12003-05-03 03:14:35 +0000267 std::string TyName = "struct opaque_" + itostr(Count++);
Chris Lattner04b72c82002-10-16 00:08:22 +0000268 assert(TypeNames.find(Ty) == TypeNames.end());
269 TypeNames[Ty] = TyName;
270 return Out << TyName << " " << NameSoFar;
271 }
Chris Lattner83c57752002-08-19 22:17:53 +0000272 default:
273 assert(0 && "Unhandled case in getTypeProps!");
274 abort();
275 }
276
277 return Out;
278}
279
Chris Lattner6d492922002-08-19 21:32:41 +0000280void CWriter::printConstantArray(ConstantArray *CPA) {
281
282 // As a special case, print the array as a string if it is an array of
283 // ubytes or an array of sbytes with positive values.
284 //
285 const Type *ETy = CPA->getType()->getElementType();
286 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
287
288 // Make sure the last character is a null char, as automatically added by C
289 if (CPA->getNumOperands() == 0 ||
290 !cast<Constant>(*(CPA->op_end()-1))->isNullValue())
291 isString = false;
292
293 if (isString) {
294 Out << "\"";
295 // Do not include the last character, which we know is null
296 for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) {
297 unsigned char C = (ETy == Type::SByteTy) ?
298 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
299 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
300
301 if (isprint(C)) {
Nick Hildenbrandt088b4722002-11-06 21:40:23 +0000302 if (C == '"' || C == '\\')
303 Out << "\\" << C;
Nick Hildenbrandtc3dd2af2002-09-30 21:11:55 +0000304 else
305 Out << C;
Chris Lattner6d492922002-08-19 21:32:41 +0000306 } else {
307 switch (C) {
308 case '\n': Out << "\\n"; break;
309 case '\t': Out << "\\t"; break;
310 case '\r': Out << "\\r"; break;
311 case '\v': Out << "\\v"; break;
312 case '\a': Out << "\\a"; break;
Nick Hildenbrandtc3dd2af2002-09-30 21:11:55 +0000313 case '\"': Out << "\\\""; break;
314 case '\'': Out << "\\\'"; break;
Chris Lattner6d492922002-08-19 21:32:41 +0000315 default:
316 Out << "\\x";
317 Out << ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
318 Out << ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
319 break;
320 }
321 }
322 }
323 Out << "\"";
324 } else {
325 Out << "{";
326 if (CPA->getNumOperands()) {
327 Out << " ";
328 printConstant(cast<Constant>(CPA->getOperand(0)));
329 for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
330 Out << ", ";
331 printConstant(cast<Constant>(CPA->getOperand(i)));
332 }
333 }
334 Out << " }";
335 }
336}
337
338
339// printConstant - The LLVM Constant to C Constant converter.
340void CWriter::printConstant(Constant *CPV) {
341 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
342 switch (CE->getOpcode()) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000343 case Instruction::Cast:
344 Out << "((";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000345 printType(Out, CPV->getType());
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000346 Out << ")";
Chris Lattner84c0d5e2003-05-14 17:50:19 +0000347 printConstant(CE->getOperand(0));
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000348 Out << ")";
349 return;
350
351 case Instruction::GetElementPtr:
Nick Hildenbrandtd2eb3862002-10-03 20:47:24 +0000352 Out << "(&(";
Chris Lattner84c0d5e2003-05-14 17:50:19 +0000353 printIndexingExpression(CE->getOperand(0),
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000354 CPV->op_begin()+1, CPV->op_end());
Nick Hildenbrandtd2eb3862002-10-03 20:47:24 +0000355 Out << "))";
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000356 return;
357 case Instruction::Add:
358 Out << "(";
Chris Lattner84c0d5e2003-05-14 17:50:19 +0000359 printConstant(CE->getOperand(0));
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000360 Out << " + ";
Chris Lattner84c0d5e2003-05-14 17:50:19 +0000361 printConstant(CE->getOperand(1));
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000362 Out << ")";
363 return;
364 case Instruction::Sub:
365 Out << "(";
Chris Lattner84c0d5e2003-05-14 17:50:19 +0000366 printConstant(CE->getOperand(0));
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000367 Out << " - ";
Chris Lattner84c0d5e2003-05-14 17:50:19 +0000368 printConstant(CE->getOperand(1));
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000369 Out << ")";
370 return;
371
Chris Lattner6d492922002-08-19 21:32:41 +0000372 default:
373 std::cerr << "CWriter Error: Unhandled constant expression: "
374 << CE << "\n";
375 abort();
376 }
377 }
378
379 switch (CPV->getType()->getPrimitiveID()) {
380 case Type::BoolTyID:
381 Out << (CPV == ConstantBool::False ? "0" : "1"); break;
382 case Type::SByteTyID:
383 case Type::ShortTyID:
Chris Lattner6d492922002-08-19 21:32:41 +0000384 Out << cast<ConstantSInt>(CPV)->getValue(); break;
Chris Lattner45343ea2003-05-12 15:39:31 +0000385 case Type::IntTyID:
386 if ((int)cast<ConstantSInt>(CPV)->getValue() == (int)0x80000000)
387 Out << "((int)0x80000000)"; // Handle MININT specially to avoid warning
388 else
389 Out << cast<ConstantSInt>(CPV)->getValue();
390 break;
391
Chris Lattner6d492922002-08-19 21:32:41 +0000392 case Type::LongTyID:
393 Out << cast<ConstantSInt>(CPV)->getValue() << "ll"; break;
394
395 case Type::UByteTyID:
396 case Type::UShortTyID:
397 Out << cast<ConstantUInt>(CPV)->getValue(); break;
398 case Type::UIntTyID:
399 Out << cast<ConstantUInt>(CPV)->getValue() << "u"; break;
400 case Type::ULongTyID:
401 Out << cast<ConstantUInt>(CPV)->getValue() << "ull"; break;
402
403 case Type::FloatTyID:
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000404 case Type::DoubleTyID: {
405 ConstantFP *FPC = cast<ConstantFP>(CPV);
Chris Lattneredd8ce12003-05-03 03:14:35 +0000406 std::map<const ConstantFP*, unsigned>::iterator I = FPConstantMap.find(FPC);
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000407 if (I != FPConstantMap.end()) {
408 // Because of FP precision problems we must load from a stack allocated
409 // value that holds the value in hex.
410 Out << "(*(" << (FPC->getType() == Type::FloatTy ? "float" : "double")
411 << "*)&FloatConstant" << I->second << ")";
412 } else {
413 Out << FPC->getValue();
414 }
415 break;
416 }
Chris Lattner6d492922002-08-19 21:32:41 +0000417
418 case Type::ArrayTyID:
419 printConstantArray(cast<ConstantArray>(CPV));
420 break;
421
422 case Type::StructTyID: {
423 Out << "{";
424 if (CPV->getNumOperands()) {
425 Out << " ";
426 printConstant(cast<Constant>(CPV->getOperand(0)));
427 for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) {
428 Out << ", ";
429 printConstant(cast<Constant>(CPV->getOperand(i)));
430 }
431 }
432 Out << " }";
433 break;
434 }
435
436 case Type::PointerTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000437 if (isa<ConstantPointerNull>(CPV)) {
Nick Hildenbrandtf0fca362002-10-28 19:54:06 +0000438 Out << "(NULL)";
Chris Lattner6d492922002-08-19 21:32:41 +0000439 break;
440 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
441 writeOperand(CPR->getValue());
442 break;
443 }
444 // FALL THROUGH
445 default:
446 std::cerr << "Unknown constant type: " << CPV << "\n";
447 abort();
448 }
449}
450
451void CWriter::writeOperandInternal(Value *Operand) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000452 if (Instruction *I = dyn_cast<Instruction>(Operand))
453 if (isInlinableInst(*I)) {
454 // Should we inline this instruction to build a tree?
455 Out << "(";
456 visit(*I);
457 Out << ")";
458 return;
459 }
460
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +0000461 if (Operand->hasName()) {
Chris Lattner6d492922002-08-19 21:32:41 +0000462 Out << getValueName(Operand);
463 } else if (Constant *CPV = dyn_cast<Constant>(Operand)) {
464 printConstant(CPV);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000465 } else {
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000466 int Slot = Table->getValSlot(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000467 assert(Slot >= 0 && "Malformed LLVM!");
468 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
469 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000470}
471
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000472void CWriter::writeOperand(Value *Operand) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000473 if (isa<GlobalVariable>(Operand))
474 Out << "(&"; // Global variables are references as their addresses by llvm
475
476 writeOperandInternal(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000477
478 if (isa<GlobalVariable>(Operand))
479 Out << ")";
480}
481
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000482// nameAllUsedStructureTypes - If there are structure types in the module that
483// are used but do not have names assigned to them in the symbol table yet then
484// we assign them names now.
485//
486bool CWriter::nameAllUsedStructureTypes(Module &M) {
487 // Get a set of types that are used by the program...
488 std::set<const Type *> UT = getAnalysis<FindUsedTypes>().getTypes();
489
490 // Loop over the module symbol table, removing types from UT that are already
491 // named.
492 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000493 SymbolTable &MST = M.getSymbolTable();
494 if (MST.find(Type::TypeTy) != MST.end())
495 for (SymbolTable::type_iterator I = MST.type_begin(Type::TypeTy),
496 E = MST.type_end(Type::TypeTy); I != E; ++I)
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000497 UT.erase(cast<Type>(I->second));
498
499 // UT now contains types that are not named. Loop over it, naming structure
500 // types.
501 //
502 bool Changed = false;
503 for (std::set<const Type *>::const_iterator I = UT.begin(), E = UT.end();
504 I != E; ++I)
505 if (const StructType *ST = dyn_cast<StructType>(*I)) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000506 ((Value*)ST)->setName("unnamed", &MST);
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000507 Changed = true;
508 }
509 return Changed;
510}
511
Chris Lattneredd8ce12003-05-03 03:14:35 +0000512static void generateAllocaDecl(std::ostream& Out) {
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000513 // On SunOS, we need to insert the alloca macro & proto for the builtin.
514 Out << "#ifdef sun\n"
515 << "extern void *__builtin_alloca(unsigned long);\n"
516 << "#define alloca(x) __builtin_alloca(x)\n"
517 << "#else\n"
518 << "#include <alloca.h>\n"
519 << "#endif\n\n";
520}
521
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000522void CWriter::printModule(Module *M) {
Chris Lattner78771c82002-05-17 04:55:35 +0000523 // Calculate which global values have names that will collide when we throw
524 // away type information.
Chris Lattner594b9fa2002-05-21 21:10:04 +0000525 { // Scope to delete the FoundNames set when we are done with it...
Chris Lattneredd8ce12003-05-03 03:14:35 +0000526 std::set<std::string> FoundNames;
Chris Lattner78771c82002-05-17 04:55:35 +0000527 for (Module::iterator I = M->begin(), E = M->end(); 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 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000535 if (I->hasName()) // If the global has a name...
536 if (FoundNames.count(I->getName())) // And the name is already used
537 MangledGlobals.insert(I); // Mangle the name
Chris Lattner78771c82002-05-17 04:55:35 +0000538 else
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000539 FoundNames.insert(I->getName()); // Otherwise, keep track of name
Chris Lattner78771c82002-05-17 04:55:35 +0000540 }
541
Chris Lattner16c7bb22002-05-09 02:28:59 +0000542 // get declaration for alloca
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000543 Out << "/* Provide Declarations */\n";
544 generateAllocaDecl(Out);
Chris Lattner18ac3c82003-05-08 18:41:45 +0000545 Out << "#include <stdarg.h>\n";
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000546
547 // Provide a definition for null if one does not already exist,
548 // and for `bool' if not compiling with a C++ compiler.
549 Out << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000550 << "#ifndef __cplusplus\ntypedef unsigned char bool;\n#endif\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000551
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000552 << "\n\n/* Support for floating point constants */\n"
553 << "typedef unsigned long long ConstantDoubleTy;\n"
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000554 << "typedef unsigned int ConstantFloatTy;\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000555
Chris Lattnera4d4a852002-08-19 21:48:40 +0000556 << "\n\n/* Global Declarations */\n";
557
558 // First output all the declarations for the program, because C requires
559 // Functions & globals to be declared before they are used.
560 //
Chris Lattner16c7bb22002-05-09 02:28:59 +0000561
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000562 // Loop over the symbol table, emitting all named constants...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000563 printSymbolTable(M->getSymbolTable());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000564
Chris Lattnera4d4a852002-08-19 21:48:40 +0000565 // Global variable declarations...
566 if (!M->gempty()) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000567 Out << "\n/* External Global Variable Declarations */\n";
Chris Lattnera4d4a852002-08-19 21:48:40 +0000568 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000569 if (I->hasExternalLinkage()) {
570 Out << "extern ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000571 printType(Out, I->getType()->getElementType(), getValueName(I));
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000572 Out << ";\n";
573 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000574 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000575 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000576
Chris Lattnera4d4a852002-08-19 21:48:40 +0000577 // Function declarations
578 if (!M->empty()) {
579 Out << "\n/* Function Declarations */\n";
Nick Hildenbrandt3cecdc52002-10-23 18:59:40 +0000580 needsMalloc = true;
Chris Lattner4cda8352002-08-20 16:55:48 +0000581 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
Nick Hildenbrandta1a64f82002-11-18 22:21:52 +0000582 // If the function is external and the name collides don't print it.
Chris Lattner18ac3c82003-05-08 18:41:45 +0000583 // Sometimes the bytecode likes to have multiple "declarations" for
Chris Lattneredd8ce12003-05-03 03:14:35 +0000584 // external functions
Chris Lattner18ac3c82003-05-08 18:41:45 +0000585 if ((I->hasInternalLinkage() || !MangledGlobals.count(I)) &&
586 !I->getIntrinsicID()) {
Nick Hildenbrandta1a64f82002-11-18 22:21:52 +0000587 printFunctionSignature(I, true);
588 Out << ";\n";
589 }
Chris Lattner4cda8352002-08-20 16:55:48 +0000590 }
Chris Lattnera4d4a852002-08-19 21:48:40 +0000591 }
592
Nick Hildenbrandt3cecdc52002-10-23 18:59:40 +0000593 // Print Malloc prototype if needed
594 if (needsMalloc){
595 Out << "\n/* Malloc to make sun happy */\n";
596 Out << "extern void * malloc(size_t);\n\n";
597 }
598
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +0000599 // Output the global variable declerations
600 if (!M->gempty()) {
601 Out << "\n\n/* Global Variable Declerations */\n";
602 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
603 if (!I->isExternal()) {
604 Out << "extern ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000605 printType(Out, I->getType()->getElementType(), getValueName(I));
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +0000606
607 Out << ";\n";
608 }
609 }
610
611
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000612 // Output the global variable definitions and contents...
Chris Lattnera4d4a852002-08-19 21:48:40 +0000613 if (!M->gempty()) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000614 Out << "\n\n/* Global Variable Definitions and Initialization */\n";
Chris Lattnere8e035b2002-10-16 20:08:47 +0000615 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
616 if (!I->isExternal()) {
617 if (I->hasInternalLinkage())
618 Out << "static ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000619 printType(Out, I->getType()->getElementType(), getValueName(I));
Chris Lattnera4d4a852002-08-19 21:48:40 +0000620
Chris Lattnera4d4a852002-08-19 21:48:40 +0000621 Out << " = " ;
622 writeOperand(I->getInitializer());
Chris Lattnere8e035b2002-10-16 20:08:47 +0000623 Out << ";\n";
Chris Lattnera4d4a852002-08-19 21:48:40 +0000624 }
Chris Lattnera4d4a852002-08-19 21:48:40 +0000625 }
626
Chris Lattner16c7bb22002-05-09 02:28:59 +0000627 // Output all of the functions...
Chris Lattnera4d4a852002-08-19 21:48:40 +0000628 if (!M->empty()) {
629 Out << "\n\n/* Function Bodies */\n";
630 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
631 printFunction(I);
632 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000633}
634
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000635
Chris Lattner2db41cd2002-09-20 15:12:13 +0000636/// printSymbolTable - Run through symbol table looking for type names. If a
637/// type name is found, emit it's declaration...
Chris Lattnera4c047e2002-09-20 14:56:54 +0000638///
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000639void CWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattner2db41cd2002-09-20 15:12:13 +0000640 // If there are no type names, exit early.
641 if (ST.find(Type::TypeTy) == ST.end())
642 return;
643
644 // We are only interested in the type plane of the symbol table...
645 SymbolTable::type_const_iterator I = ST.type_begin(Type::TypeTy);
646 SymbolTable::type_const_iterator End = ST.type_end(Type::TypeTy);
647
Chris Lattner58d04d42002-09-20 15:18:30 +0000648 // Print out forward declarations for structure types before anything else!
649 Out << "/* Structure forward decls */\n";
650 for (; I != End; ++I)
651 if (const Type *STy = dyn_cast<StructType>(I->second)) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000652 std::string Name = "struct l_" + makeNameProper(I->first);
Chris Lattner58d04d42002-09-20 15:18:30 +0000653 Out << Name << ";\n";
654 TypeNames.insert(std::make_pair(STy, Name));
655 }
656
657 Out << "\n";
658
659 // Now we can print out typedefs...
660 Out << "/* Typedefs */\n";
661 for (I = ST.type_begin(Type::TypeTy); I != End; ++I) {
662 const Type *Ty = cast<Type>(I->second);
Chris Lattneredd8ce12003-05-03 03:14:35 +0000663 std::string Name = "l_" + makeNameProper(I->first);
Chris Lattner58d04d42002-09-20 15:18:30 +0000664 Out << "typedef ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000665 printType(Out, Ty, Name);
Chris Lattner58d04d42002-09-20 15:18:30 +0000666 Out << ";\n";
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000667 }
668
669 Out << "\n";
670
Chris Lattner2c601a72002-09-20 15:05:40 +0000671 // Keep track of which structures have been printed so far...
672 std::set<const StructType *> StructPrinted;
673
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000674 // Loop over all structures then push them into the stack so they are
675 // printed in the correct order.
Chris Lattner2db41cd2002-09-20 15:12:13 +0000676 //
Chris Lattner58d04d42002-09-20 15:18:30 +0000677 Out << "/* Structure contents */\n";
Chris Lattner2db41cd2002-09-20 15:12:13 +0000678 for (I = ST.type_begin(Type::TypeTy); I != End; ++I)
679 if (const StructType *STy = dyn_cast<StructType>(I->second))
680 printContainedStructs(STy, StructPrinted);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000681}
682
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000683// Push the struct onto the stack and recursively push all structs
684// this one depends on.
Chris Lattner2c601a72002-09-20 15:05:40 +0000685void CWriter::printContainedStructs(const Type *Ty,
686 std::set<const StructType*> &StructPrinted){
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000687 if (const StructType *STy = dyn_cast<StructType>(Ty)){
688 //Check to see if we have already printed this struct
Chris Lattner2c601a72002-09-20 15:05:40 +0000689 if (StructPrinted.count(STy) == 0) {
690 // Print all contained types first...
Chris Lattnera4c047e2002-09-20 14:56:54 +0000691 for (StructType::ElementTypes::const_iterator
692 I = STy->getElementTypes().begin(),
693 E = STy->getElementTypes().end(); I != E; ++I) {
Chris Lattner2c601a72002-09-20 15:05:40 +0000694 const Type *Ty1 = I->get();
Chris Lattnera4c047e2002-09-20 14:56:54 +0000695 if (isa<StructType>(Ty1) || isa<ArrayType>(Ty1))
Chris Lattner2c601a72002-09-20 15:05:40 +0000696 printContainedStructs(Ty1, StructPrinted);
Chris Lattnera4c047e2002-09-20 14:56:54 +0000697 }
698
Chris Lattner2c601a72002-09-20 15:05:40 +0000699 //Print structure type out..
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000700 StructPrinted.insert(STy);
Chris Lattneredd8ce12003-05-03 03:14:35 +0000701 std::string Name = TypeNames[STy];
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000702 printType(Out, STy, Name, true);
Chris Lattner58d04d42002-09-20 15:18:30 +0000703 Out << ";\n\n";
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000704 }
Chris Lattnera4c047e2002-09-20 14:56:54 +0000705
Chris Lattner2c601a72002-09-20 15:05:40 +0000706 // If it is an array, check contained types and continue
707 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)){
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000708 const Type *Ty1 = ATy->getElementType();
709 if (isa<StructType>(Ty1) || isa<ArrayType>(Ty1))
Chris Lattner2c601a72002-09-20 15:05:40 +0000710 printContainedStructs(Ty1, StructPrinted);
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000711 }
712}
713
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000714
Chris Lattner4cda8352002-08-20 16:55:48 +0000715void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
Nick Hildenbrandt3cecdc52002-10-23 18:59:40 +0000716 // If the program provides it's own malloc prototype we don't need
717 // to include the general one.
718 if (getValueName(F) == "malloc")
719 needsMalloc = false;
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000720 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000721 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000722 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000723
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000724 std::stringstream FunctionInards;
725
726 // Print out the name...
727 FunctionInards << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000728
Chris Lattner16c7bb22002-05-09 02:28:59 +0000729 if (!F->isExternal()) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000730 if (!F->aempty()) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000731 std::string ArgName;
Chris Lattner4cda8352002-08-20 16:55:48 +0000732 if (F->abegin()->hasName() || !Prototype)
733 ArgName = getValueName(F->abegin());
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000734 printType(FunctionInards, F->afront().getType(), ArgName);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000735 for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
736 I != E; ++I) {
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000737 FunctionInards << ", ";
Chris Lattner4cda8352002-08-20 16:55:48 +0000738 if (I->hasName() || !Prototype)
739 ArgName = getValueName(I);
740 else
741 ArgName = "";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000742 printType(FunctionInards, I->getType(), ArgName);
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000743 }
744 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000745 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000746 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000747 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000748 FT->getParamTypes().begin(),
749 E = FT->getParamTypes().end(); I != E; ++I) {
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000750 if (I != FT->getParamTypes().begin()) FunctionInards << ", ";
751 printType(FunctionInards, *I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000752 }
753 }
754
Chris Lattner1f8c4a12002-09-20 22:32:30 +0000755 // Finish printing arguments... if this is a vararg function, print the ...,
756 // unless there are no known types, in which case, we just emit ().
757 //
758 if (FT->isVarArg() && !FT->getParamTypes().empty()) {
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000759 if (FT->getParamTypes().size()) FunctionInards << ", ";
760 FunctionInards << "..."; // Output varargs portion of signature!
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000761 }
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000762 FunctionInards << ")";
763 // Print out the return type and the entire signature for that matter
764 printType(Out, F->getReturnType(), FunctionInards.str());
765
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000766}
767
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000768
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000769void CWriter::printFunction(Function *F) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000770 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000771
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000772 Table->incorporateFunction(F);
Chris Lattnerf34ee812002-05-09 03:12:34 +0000773
Chris Lattner4cda8352002-08-20 16:55:48 +0000774 printFunctionSignature(F, false);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000775 Out << " {\n";
776
Chris Lattner497e19a2002-05-09 20:39:03 +0000777 // print local variable information for the function
Chris Lattner7683a122002-05-09 20:33:35 +0000778 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000779 if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000780 Out << " ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000781 printType(Out, (*I)->getType(), getValueName(*I));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000782 Out << ";\n";
Chris Lattner84e66652003-05-03 07:11:00 +0000783
784 if (isa<PHINode>(*I)) { // Print out PHI node temporaries as well...
785 Out << " ";
786 printType(Out, (*I)->getType(), getValueName(*I)+"__PHI_TEMPORARY");
787 Out << ";\n";
788 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000789 }
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000790
791 Out << "\n";
792
793 // Scan the function for floating point constants. If any FP constant is used
794 // in the function, we want to redirect it here so that we do not depend on
795 // the precision of the printed form.
796 //
797 unsigned FPCounter = 0;
798 for (constant_iterator I = constant_begin(F), E = constant_end(F); I != E;++I)
799 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(*I))
800 if (FPConstantMap.find(FPC) == FPConstantMap.end()) {
801 double Val = FPC->getValue();
802
803 FPConstantMap[FPC] = FPCounter; // Number the FP constants
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000804
805 if (FPC->getType() == Type::DoubleTy)
806 Out << " const ConstantDoubleTy FloatConstant" << FPCounter++
807 << " = 0x" << std::hex << *(unsigned long long*)&Val << std::dec
808 << "; /* " << Val << " */\n";
Chris Lattner9c1338b2002-11-07 22:12:53 +0000809 else if (FPC->getType() == Type::FloatTy) {
810 float fVal = Val;
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000811 Out << " const ConstantFloatTy FloatConstant" << FPCounter++
Chris Lattner9c1338b2002-11-07 22:12:53 +0000812 << " = 0x" << std::hex << *(unsigned*)&fVal << std::dec
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000813 << "; /* " << Val << " */\n";
Chris Lattner9c1338b2002-11-07 22:12:53 +0000814 } else
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000815 assert(0 && "Unknown float type!");
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000816 }
817
818 Out << "\n";
Chris Lattner16c7bb22002-05-09 02:28:59 +0000819
820 // print the basic blocks
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000821 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
822 BasicBlock *Prev = BB->getPrev();
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000823
824 // Don't print the label for the basic block if there are no uses, or if the
825 // only terminator use is the precessor basic block's terminator. We have
826 // to scan the use list because PHI nodes use basic blocks too but do not
827 // require a label to be generated.
828 //
829 bool NeedsLabel = false;
830 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
831 UI != UE; ++UI)
832 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
Chris Lattnerf1acd962003-04-23 19:15:13 +0000833 if (TI != Prev->getTerminator() ||
834 isa<SwitchInst>(Prev->getTerminator())) {
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000835 NeedsLabel = true;
836 break;
837 }
838
839 if (NeedsLabel) Out << getValueName(BB) << ":\n";
840
841 // Output all of the instructions in the basic block...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000842 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II){
Chris Lattner84e66652003-05-03 07:11:00 +0000843 if (!isInlinableInst(*II)) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000844 if (II->getType() != Type::VoidTy)
845 outputLValue(II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000846 else
847 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000848 visit(*II);
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000849 Out << ";\n";
850 }
851 }
852
853 // Don't emit prefix or suffix for the terminator...
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000854 visit(*BB->getTerminator());
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000855 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000856
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000857 Out << "}\n\n";
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000858 Table->purgeFunction();
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000859 FPConstantMap.clear();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000860}
861
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000862// Specific Instruction type classes... note that all of the casts are
863// neccesary because we use the instruction classes as opaque types...
864//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000865void CWriter::visitReturnInst(ReturnInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000866 // Don't output a void return if this is the last basic block in the function
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000867 if (I.getNumOperands() == 0 &&
868 &*--I.getParent()->getParent()->end() == I.getParent() &&
869 !I.getParent()->size() == 1) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000870 return;
Chris Lattner963b70b2002-05-21 18:05:19 +0000871 }
Chris Lattner44408262002-05-09 03:50:42 +0000872
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000873 Out << " return";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000874 if (I.getNumOperands()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000875 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000876 writeOperand(I.getOperand(0));
Chris Lattner16c7bb22002-05-09 02:28:59 +0000877 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000878 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000879}
880
Chris Lattnera9f5e052003-04-22 20:19:52 +0000881void CWriter::visitSwitchInst(SwitchInst &SI) {
882 Out << " switch (";
883 writeOperand(SI.getOperand(0));
Chris Lattnerf1acd962003-04-23 19:15:13 +0000884 Out << ") {\n default:\n";
885 printBranchToBlock(SI.getParent(), SI.getDefaultDest(), 2);
Chris Lattnera9f5e052003-04-22 20:19:52 +0000886 Out << ";\n";
887 for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) {
888 Out << " case ";
889 writeOperand(SI.getOperand(i));
890 Out << ":\n";
891 BasicBlock *Succ = cast<BasicBlock>(SI.getOperand(i+1));
892 printBranchToBlock(SI.getParent(), Succ, 2);
893 if (Succ == SI.getParent()->getNext())
894 Out << " break;\n";
895 }
896 Out << " }\n";
897}
898
899
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000900static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
901 // If PHI nodes need copies, we need the copy code...
902 if (isa<PHINode>(To->front()) ||
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000903 From->getNext() != To) // Not directly successor, need goto
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000904 return true;
905
906 // Otherwise we don't need the code.
907 return false;
908}
909
910void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
911 unsigned Indent) {
912 for (BasicBlock::iterator I = Succ->begin();
Chris Lattner2ee82e02003-04-23 16:36:11 +0000913 PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000914 // now we have to do the printing
Chris Lattneredd8ce12003-05-03 03:14:35 +0000915 Out << std::string(Indent, ' ');
Chris Lattner84e66652003-05-03 07:11:00 +0000916 Out << " " << getValueName(I) << "__PHI_TEMPORARY = ";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000917 writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
918 Out << "; /* for PHI node */\n";
919 }
920
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000921 if (CurBB->getNext() != Succ) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000922 Out << std::string(Indent, ' ') << " goto ";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000923 writeOperand(Succ);
924 Out << ";\n";
925 }
926}
927
928// Brach instruction printing - Avoid printing out a brach to a basic block that
929// immediately succeeds the current one.
930//
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000931void CWriter::visitBranchInst(BranchInst &I) {
932 if (I.isConditional()) {
933 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(0))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000934 Out << " if (";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000935 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000936 Out << ") {\n";
937
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000938 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000939
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000940 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(1))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000941 Out << " } else {\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000942 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000943 }
944 } else {
945 // First goto not neccesary, assume second one is...
946 Out << " if (!";
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000947 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000948 Out << ") {\n";
949
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000950 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000951 }
952
953 Out << " }\n";
954 } else {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000955 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000956 }
957 Out << "\n";
958}
959
Chris Lattner84e66652003-05-03 07:11:00 +0000960// PHI nodes get copied into temporary values at the end of predecessor basic
961// blocks. We now need to copy these temporary values into the REAL value for
962// the PHI.
963void CWriter::visitPHINode(PHINode &I) {
964 writeOperand(&I);
965 Out << "__PHI_TEMPORARY";
966}
967
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000968
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000969void CWriter::visitBinaryOperator(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000970 // binary instructions, shift instructions, setCond instructions.
Chris Lattnerf5612b762003-04-23 19:09:22 +0000971 assert(!isa<PointerType>(I.getType()));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000972
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000973 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000974
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000975 switch (I.getOpcode()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000976 case Instruction::Add: Out << " + "; break;
977 case Instruction::Sub: Out << " - "; break;
978 case Instruction::Mul: Out << "*"; break;
979 case Instruction::Div: Out << "/"; break;
980 case Instruction::Rem: Out << "%"; break;
981 case Instruction::And: Out << " & "; break;
982 case Instruction::Or: Out << " | "; break;
983 case Instruction::Xor: Out << " ^ "; break;
984 case Instruction::SetEQ: Out << " == "; break;
985 case Instruction::SetNE: Out << " != "; break;
986 case Instruction::SetLE: Out << " <= "; break;
987 case Instruction::SetGE: Out << " >= "; break;
988 case Instruction::SetLT: Out << " < "; break;
989 case Instruction::SetGT: Out << " > "; break;
990 case Instruction::Shl : Out << " << "; break;
991 case Instruction::Shr : Out << " >> "; break;
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000992 default: std::cerr << "Invalid operator type!" << I; abort();
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000993 }
994
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000995 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000996}
997
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000998void CWriter::visitCastInst(CastInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000999 Out << "(";
Chris Lattneredd8ce12003-05-03 03:14:35 +00001000 printType(Out, I.getType(), "", /*ignoreName*/false, /*namedContext*/false);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001001 Out << ")";
Chris Lattnerf5612b762003-04-23 19:09:22 +00001002 if (isa<PointerType>(I.getType())&&I.getOperand(0)->getType()->isIntegral() ||
1003 isa<PointerType>(I.getOperand(0)->getType())&&I.getType()->isIntegral()) {
1004 // Avoid "cast to pointer from integer of different size" warnings
1005 Out << "(long)";
1006 }
1007
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001008 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001009}
1010
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001011void CWriter::visitCallInst(CallInst &I) {
Chris Lattner18ac3c82003-05-08 18:41:45 +00001012 // Handle intrinsic function calls first...
1013 if (Function *F = I.getCalledFunction())
1014 if (LLVMIntrinsic::ID ID = (LLVMIntrinsic::ID)F->getIntrinsicID()) {
1015 switch (ID) {
1016 default: assert(0 && "Unknown LLVM intrinsic!");
1017 case LLVMIntrinsic::va_start:
1018 Out << "va_start((va_list)*";
1019 writeOperand(I.getOperand(1));
1020 Out << ", ";
1021 // Output the last argument to the enclosing function...
1022 writeOperand(&I.getParent()->getParent()->aback());
1023 Out << ")";
1024 return;
1025
1026 case LLVMIntrinsic::va_end:
1027 Out << "va_end((va_list)*";
1028 writeOperand(I.getOperand(1));
1029 Out << ")";
1030 return;
1031 case LLVMIntrinsic::va_copy:
1032 Out << "va_copy((va_list)*";
1033 writeOperand(I.getOperand(1));
1034 Out << ", (va_list)";
1035 writeOperand(I.getOperand(2));
1036 Out << ")";
1037 return;
1038 }
1039 }
1040
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001041 const PointerType *PTy = cast<PointerType>(I.getCalledValue()->getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001042 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1043 const Type *RetTy = FTy->getReturnType();
1044
Chris Lattnerfabc8802002-08-26 20:50:09 +00001045 writeOperand(I.getOperand(0));
1046 Out << "(";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001047
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001048 if (I.getNumOperands() > 1) {
1049 writeOperand(I.getOperand(1));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001050
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001051 for (unsigned op = 2, Eop = I.getNumOperands(); op != Eop; ++op) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001052 Out << ", ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001053 writeOperand(I.getOperand(op));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001054 }
1055 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001056 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001057}
1058
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001059void CWriter::visitMallocInst(MallocInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001060 Out << "(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001061 printType(Out, I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001062 Out << ")malloc(sizeof(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001063 printType(Out, I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001064 Out << ")";
1065
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001066 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001067 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001068 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001069 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001070 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001071}
1072
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001073void CWriter::visitAllocaInst(AllocaInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001074 Out << "(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001075 printType(Out, I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001076 Out << ") alloca(sizeof(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001077 printType(Out, I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001078 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001079 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001080 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001081 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001082 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001083 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001084}
1085
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001086void CWriter::visitFreeInst(FreeInst &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001087 Out << "free(";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001088 writeOperand(I.getOperand(0));
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001089 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001090}
1091
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001092void CWriter::printIndexingExpression(Value *Ptr, User::op_iterator I,
1093 User::op_iterator E) {
1094 bool HasImplicitAddress = false;
1095 // If accessing a global value with no indexing, avoid *(&GV) syndrome
1096 if (GlobalValue *V = dyn_cast<GlobalValue>(Ptr)) {
1097 HasImplicitAddress = true;
1098 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Ptr)) {
1099 HasImplicitAddress = true;
1100 Ptr = CPR->getValue(); // Get to the global...
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001101 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001102
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001103 if (I == E) {
1104 if (!HasImplicitAddress)
1105 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001106
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001107 writeOperandInternal(Ptr);
1108 return;
1109 }
1110
1111 const Constant *CI = dyn_cast<Constant>(I->get());
1112 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
1113 Out << "(&";
1114
1115 writeOperandInternal(Ptr);
1116
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001117 if (HasImplicitAddress && (!CI || !CI->isNullValue())) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001118 Out << ")";
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001119 HasImplicitAddress = false; // HIA is only true if we haven't addressed yet
1120 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001121
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001122 assert(!HasImplicitAddress || (CI && CI->isNullValue()) &&
1123 "Can only have implicit address with direct accessing");
1124
1125 if (HasImplicitAddress) {
1126 ++I;
1127 } else if (CI && CI->isNullValue() && I+1 != E) {
1128 // Print out the -> operator if possible...
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001129 if ((*(I+1))->getType() == Type::UByteTy) {
1130 Out << (HasImplicitAddress ? "." : "->");
1131 Out << "field" << cast<ConstantUInt>(*(I+1))->getValue();
1132 I += 2;
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001133 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001134 }
Chris Lattner5dfe7672002-08-22 22:48:55 +00001135
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001136 for (; I != E; ++I)
Chris Lattner106ff452002-09-11 01:21:29 +00001137 if ((*I)->getType() == Type::LongTy) {
Vikram S. Adve9d6f13f2002-09-15 21:51:04 +00001138 Out << "[";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001139 writeOperand(*I);
Vikram S. Adve9d6f13f2002-09-15 21:51:04 +00001140 Out << "]";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001141 } else {
Chris Lattner59afbf32002-09-12 20:34:47 +00001142 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001143 }
1144}
1145
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001146void CWriter::visitLoadInst(LoadInst &I) {
Nick Hildenbrandtca626922002-10-02 21:14:33 +00001147 Out << "*";
Chris Lattner5dfe7672002-08-22 22:48:55 +00001148 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001149}
1150
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001151void CWriter::visitStoreInst(StoreInst &I) {
Nick Hildenbrandtca626922002-10-02 21:14:33 +00001152 Out << "*";
Chris Lattner5dfe7672002-08-22 22:48:55 +00001153 writeOperand(I.getPointerOperand());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001154 Out << " = ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001155 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001156}
1157
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001158void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
Nick Hildenbrandtca626922002-10-02 21:14:33 +00001159 Out << "&";
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001160 printIndexingExpression(I.getPointerOperand(), I.idx_begin(), I.idx_end());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001161}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001162
Chris Lattner18ac3c82003-05-08 18:41:45 +00001163void CWriter::visitVarArgInst(VarArgInst &I) {
1164 Out << "va_arg((va_list)*";
1165 writeOperand(I.getOperand(0));
1166 Out << ", ";
1167 printType(Out, I.getType(), "", /*ignoreName*/false, /*namedContext*/false);
1168 Out << ")";
1169}
1170
1171
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001172//===----------------------------------------------------------------------===//
1173// External Interface declaration
1174//===----------------------------------------------------------------------===//
1175
Chris Lattner0e9f93e2002-08-31 00:29:16 +00001176Pass *createWriteToCPass(std::ostream &o) { return new CWriter(o); }