blob: a4d5483f2a3b9795cf4663f88410f6a2e252c4c1 [file] [log] [blame]
Chris Lattner3af3ba82002-05-09 21:31:18 +00001//===-- Writer.cpp - Library for converting LLVM code to C ----------------===//
Misha Brukmand6a29a52005-04-20 16:05:03 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmand6a29a52005-04-20 16:05:03 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00009//
Chris Lattner897bf0d2004-02-13 06:18:21 +000010// This library converts LLVM code to C code, compilable by GCC and other C
11// compilers.
Chris Lattner16c7bb22002-05-09 02:28:59 +000012//
Chris Lattneredd8ce12003-05-03 03:14:35 +000013//===----------------------------------------------------------------------===//
Chris Lattner84e66652003-05-03 07:11:00 +000014
Chris Lattnerf31182a2004-02-13 23:18:48 +000015#include "CTargetMachine.h"
Chris Lattner0c54d892006-05-23 23:39:48 +000016#include "llvm/CallingConv.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000017#include "llvm/Constants.h"
Chris Lattner2f5f51a2002-05-09 03:28:37 +000018#include "llvm/DerivedTypes.h"
19#include "llvm/Module.h"
Chris Lattnera9f5e052003-04-22 20:19:52 +000020#include "llvm/Instructions.h"
Chris Lattner0e9f93e2002-08-31 00:29:16 +000021#include "llvm/Pass.h"
Chris Lattner94f4f8e2004-02-13 23:00:29 +000022#include "llvm/PassManager.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000023#include "llvm/SymbolTable.h"
Chris Lattner18ac3c82003-05-08 18:41:45 +000024#include "llvm/Intrinsics.h"
Jim Laskey580418e2006-03-23 18:08:29 +000025#include "llvm/IntrinsicInst.h"
Andrew Lenharth29c277f2006-11-27 23:50:49 +000026#include "llvm/InlineAsm.h"
Chris Lattnerd1cf1b42002-09-20 23:26:33 +000027#include "llvm/Analysis/ConstantsScanner.h"
Chris Lattnercb3ad012004-05-09 20:41:32 +000028#include "llvm/Analysis/FindUsedTypes.h"
29#include "llvm/Analysis/LoopInfo.h"
Chris Lattner30483732004-06-20 07:49:54 +000030#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattner94f4f8e2004-02-13 23:00:29 +000031#include "llvm/Transforms/Scalar.h"
Chris Lattnerd36c9702004-07-11 02:48:49 +000032#include "llvm/Target/TargetMachineRegistry.h"
Andrew Lenharthe0cf0752006-11-28 19:53:36 +000033#include "llvm/Target/TargetAsmInfo.h"
Chris Lattner23e90702003-11-25 20:49:55 +000034#include "llvm/Support/CallSite.h"
Chris Lattnera05e0ec2004-05-09 03:42:48 +000035#include "llvm/Support/CFG.h"
Chris Lattner23e90702003-11-25 20:49:55 +000036#include "llvm/Support/GetElementPtrTypeIterator.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000037#include "llvm/Support/InstVisitor.h"
Brian Gaeke49a178b2003-07-25 20:21:06 +000038#include "llvm/Support/Mangler.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000039#include "llvm/Support/MathExtras.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000040#include "llvm/ADT/StringExtras.h"
Chris Lattner67c2d182005-03-18 16:12:37 +000041#include "llvm/ADT/STLExtras.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000042#include "llvm/Support/MathExtras.h"
43#include "llvm/Config/config.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000044#include <algorithm>
Bill Wendling1a097e32006-12-07 23:41:45 +000045#include <sstream>
Chris Lattner897bf0d2004-02-13 06:18:21 +000046using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000047
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000048namespace {
Chris Lattnerd36c9702004-07-11 02:48:49 +000049 // Register the target.
Chris Lattner71d24aa2004-07-11 03:27:42 +000050 RegisterTarget<CTargetMachine> X("c", " C backend");
Chris Lattnerd36c9702004-07-11 02:48:49 +000051
Chris Lattnerf9a05322006-02-13 22:22:42 +000052 /// CBackendNameAllUsedStructsAndMergeFunctions - This pass inserts names for
53 /// any unnamed structure types that are used by the program, and merges
54 /// external functions with the same name.
Chris Lattner68758072004-05-09 06:20:51 +000055 ///
Chris Lattnerf9a05322006-02-13 22:22:42 +000056 class CBackendNameAllUsedStructsAndMergeFunctions : public ModulePass {
Chris Lattner68758072004-05-09 06:20:51 +000057 void getAnalysisUsage(AnalysisUsage &AU) const {
58 AU.addRequired<FindUsedTypes>();
59 }
60
61 virtual const char *getPassName() const {
62 return "C backend type canonicalizer";
63 }
64
Chris Lattnerb12914b2004-09-20 04:48:05 +000065 virtual bool runOnModule(Module &M);
Chris Lattner68758072004-05-09 06:20:51 +000066 };
Misha Brukmand6a29a52005-04-20 16:05:03 +000067
Chris Lattner68758072004-05-09 06:20:51 +000068 /// CWriter - This class is the main chunk of code that converts an LLVM
69 /// module to a C translation unit.
70 class CWriter : public FunctionPass, public InstVisitor<CWriter> {
Misha Brukmand6a29a52005-04-20 16:05:03 +000071 std::ostream &Out;
Chris Lattnerb71fd782006-11-15 18:00:10 +000072 IntrinsicLowering IL;
Brian Gaeked9fb37a2003-07-24 20:20:44 +000073 Mangler *Mang;
Chris Lattnercb3ad012004-05-09 20:41:32 +000074 LoopInfo *LI;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000075 const Module *TheModule;
Andrew Lenharthe0cf0752006-11-28 19:53:36 +000076 const TargetAsmInfo* TAsm;
Chris Lattneredd8ce12003-05-03 03:14:35 +000077 std::map<const Type *, std::string> TypeNames;
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +000078
Chris Lattneredd8ce12003-05-03 03:14:35 +000079 std::map<const ConstantFP *, unsigned> FPConstantMap;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000080 public:
Andrew Lenharthe0cf0752006-11-28 19:53:36 +000081 CWriter(std::ostream &o) : Out(o), TAsm(0) {}
Chris Lattner0e9f93e2002-08-31 00:29:16 +000082
Chris Lattner94f4f8e2004-02-13 23:00:29 +000083 virtual const char *getPassName() const { return "C backend"; }
Chris Lattner0e9f93e2002-08-31 00:29:16 +000084
Chris Lattnercb3ad012004-05-09 20:41:32 +000085 void getAnalysisUsage(AnalysisUsage &AU) const {
86 AU.addRequired<LoopInfo>();
87 AU.setPreservesAll();
88 }
89
Chris Lattner68758072004-05-09 06:20:51 +000090 virtual bool doInitialization(Module &M);
Chris Lattner0e9f93e2002-08-31 00:29:16 +000091
Chris Lattner68758072004-05-09 06:20:51 +000092 bool runOnFunction(Function &F) {
Chris Lattnercb3ad012004-05-09 20:41:32 +000093 LI = &getAnalysis<LoopInfo>();
94
Chris Lattner3150e2d2004-12-05 06:49:44 +000095 // Get rid of intrinsics we can't handle.
96 lowerIntrinsics(F);
97
Chris Lattner68758072004-05-09 06:20:51 +000098 // Output all floating point constants that cannot be printed accurately.
99 printFloatingPointConstants(F);
Chris Lattner3150e2d2004-12-05 06:49:44 +0000100
101 // Ensure that no local symbols conflict with global symbols.
102 F.renameLocalSymbols();
103
Chris Lattner68758072004-05-09 06:20:51 +0000104 printFunction(F);
105 FPConstantMap.clear();
106 return false;
107 }
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000108
Chris Lattner68758072004-05-09 06:20:51 +0000109 virtual bool doFinalization(Module &M) {
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000110 // Free memory...
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000111 delete Mang;
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000112 TypeNames.clear();
Chris Lattnercb3ad012004-05-09 20:41:32 +0000113 return false;
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000114 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000115
Chris Lattneredd8ce12003-05-03 03:14:35 +0000116 std::ostream &printType(std::ostream &Out, const Type *Ty,
117 const std::string &VariableName = "",
Chris Lattner7635ea42003-11-03 01:01:59 +0000118 bool IgnoreName = false);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000119
Chris Lattner0c54d892006-05-23 23:39:48 +0000120 void printStructReturnPointerFunctionType(std::ostream &Out,
121 const PointerType *Ty);
122
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000123 void writeOperand(Value *Operand);
Andrew Lenharth29c277f2006-11-27 23:50:49 +0000124 void writeOperandRaw(Value *Operand);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000125 void writeOperandInternal(Value *Operand);
Reid Spencer1628cec2006-10-26 06:15:43 +0000126 void writeOperandWithCast(Value* Operand, unsigned Opcode);
127 bool writeInstructionCast(const Instruction &I);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000128
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000129 private :
Andrew Lenharthe0cf0752006-11-28 19:53:36 +0000130 std::string InterpretASMConstraint(InlineAsm::ConstraintInfo& c);
131
Chris Lattnerc2421432004-05-09 04:30:20 +0000132 void lowerIntrinsics(Function &F);
Chris Lattner4ef51372004-02-14 00:31:10 +0000133
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000134 void printModule(Module *M);
Chris Lattner68758072004-05-09 06:20:51 +0000135 void printModuleTypes(const SymbolTable &ST);
Chris Lattner2c601a72002-09-20 15:05:40 +0000136 void printContainedStructs(const Type *Ty, std::set<const StructType *> &);
Chris Lattner68758072004-05-09 06:20:51 +0000137 void printFloatingPointConstants(Function &F);
Chris Lattner4cda8352002-08-20 16:55:48 +0000138 void printFunctionSignature(const Function *F, bool Prototype);
139
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000140 void printFunction(Function &);
Chris Lattnercb3ad012004-05-09 20:41:32 +0000141 void printBasicBlock(BasicBlock *BB);
142 void printLoop(Loop *L);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000143
Reid Spencer3da59db2006-11-27 01:05:10 +0000144 void printCast(unsigned opcode, const Type *SrcTy, const Type *DstTy);
Chris Lattner6d492922002-08-19 21:32:41 +0000145 void printConstant(Constant *CPV);
Reid Spencer1628cec2006-10-26 06:15:43 +0000146 void printConstantWithCast(Constant *CPV, unsigned Opcode);
147 bool printConstExprCast(const ConstantExpr *CE);
Chris Lattner6d492922002-08-19 21:32:41 +0000148 void printConstantArray(ConstantArray *CPA);
Robert Bocchinob78e8382006-01-20 20:43:57 +0000149 void printConstantPacked(ConstantPacked *CP);
Chris Lattner6d492922002-08-19 21:32:41 +0000150
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000151 // isInlinableInst - Attempt to inline instructions into their uses to build
152 // trees as much as possible. To do this, we have to consistently decide
153 // what is acceptable to inline, so that variable declarations don't get
154 // printed and an extra copy of the expr is not emitted.
155 //
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000156 static bool isInlinableInst(const Instruction &I) {
Chris Lattner433e25a2004-05-20 20:25:50 +0000157 // Always inline setcc instructions, even if they are shared by multiple
158 // expressions. GCC generates horrible code if we don't.
159 if (isa<SetCondInst>(I)) return true;
160
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000161 // Must be an expression, must be used exactly once. If it is dead, we
162 // emit it inline where it would go.
Chris Lattnerfd059242003-10-15 16:48:29 +0000163 if (I.getType() == Type::VoidTy || !I.hasOneUse() ||
Misha Brukmand6a29a52005-04-20 16:05:03 +0000164 isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I) ||
Andrew Lenharth558bc882005-06-18 18:34:52 +0000165 isa<LoadInst>(I) || isa<VAArgInst>(I))
Chris Lattnerd4e8d312003-07-23 20:45:31 +0000166 // Don't inline a load across a store or other bad things!
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000167 return false;
168
Andrew Lenharth29c277f2006-11-27 23:50:49 +0000169 // Must not be used in inline asm
170 if (I.hasOneUse() && isInlineAsm(*I.use_back())) return false;
171
Reid Spencer555a0b12006-12-11 20:39:15 +0000172 // Only inline instruction it if it's use is in the same BB as the inst.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000173 return I.getParent() == cast<Instruction>(I.use_back())->getParent();
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000174 }
175
Chris Lattnera8ab89e2003-06-17 04:39:14 +0000176 // isDirectAlloca - Define fixed sized allocas in the entry block as direct
177 // variables which are accessed with the & operator. This causes GCC to
178 // generate significantly better code than to emit alloca calls directly.
179 //
180 static const AllocaInst *isDirectAlloca(const Value *V) {
181 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
182 if (!AI) return false;
183 if (AI->isArrayAllocation())
184 return 0; // FIXME: we can also inline fixed size array allocas!
Chris Lattner02a3be02003-09-20 14:39:18 +0000185 if (AI->getParent() != &AI->getParent()->getParent()->getEntryBlock())
Chris Lattnera8ab89e2003-06-17 04:39:14 +0000186 return 0;
187 return AI;
188 }
Andrew Lenharth29c277f2006-11-27 23:50:49 +0000189
190 // isInlineAsm - Check if the instruction is a call to an inline asm chunk
191 static bool isInlineAsm(const Instruction& I) {
192 if (isa<CallInst>(&I) && isa<InlineAsm>(I.getOperand(0)))
193 return true;
194 return false;
195 }
196
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000197 // Instruction visitation functions
198 friend class InstVisitor<CWriter>;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000199
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000200 void visitReturnInst(ReturnInst &I);
201 void visitBranchInst(BranchInst &I);
Chris Lattnera9f5e052003-04-22 20:19:52 +0000202 void visitSwitchInst(SwitchInst &I);
Chris Lattnercb3ad012004-05-09 20:41:32 +0000203 void visitInvokeInst(InvokeInst &I) {
204 assert(0 && "Lowerinvoke pass didn't work!");
205 }
206
207 void visitUnwindInst(UnwindInst &I) {
208 assert(0 && "Lowerinvoke pass didn't work!");
209 }
Chris Lattnera9d12c02004-10-16 18:12:13 +0000210 void visitUnreachableInst(UnreachableInst &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000211
Chris Lattner84e66652003-05-03 07:11:00 +0000212 void visitPHINode(PHINode &I);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000213 void visitBinaryOperator(Instruction &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000214
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000215 void visitCastInst (CastInst &I);
Chris Lattner9f24a072004-03-12 05:52:14 +0000216 void visitSelectInst(SelectInst &I);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000217 void visitCallInst (CallInst &I);
Andrew Lenharth29c277f2006-11-27 23:50:49 +0000218 void visitInlineAsm(CallInst &I);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000219 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000220
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000221 void visitMallocInst(MallocInst &I);
222 void visitAllocaInst(AllocaInst &I);
223 void visitFreeInst (FreeInst &I);
224 void visitLoadInst (LoadInst &I);
225 void visitStoreInst (StoreInst &I);
226 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner4d45bd02003-10-18 05:57:43 +0000227 void visitVAArgInst (VAArgInst &I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000228
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000229 void visitInstruction(Instruction &I) {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000230 cerr << "C Writer does not know about " << I;
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000231 abort();
232 }
233
234 void outputLValue(Instruction *I) {
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000235 Out << " " << Mang->getValueName(I) << " = ";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000236 }
Chris Lattnercb3ad012004-05-09 20:41:32 +0000237
238 bool isGotoCodeNecessary(BasicBlock *From, BasicBlock *To);
John Criswell57bbfce2004-10-20 14:38:39 +0000239 void printPHICopiesForSuccessor(BasicBlock *CurBlock,
240 BasicBlock *Successor, unsigned Indent);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000241 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
242 unsigned Indent);
Chris Lattner23e90702003-11-25 20:49:55 +0000243 void printIndexingExpression(Value *Ptr, gep_type_iterator I,
244 gep_type_iterator E);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000245 };
Chris Lattner4ef51372004-02-14 00:31:10 +0000246}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000247
Chris Lattner68758072004-05-09 06:20:51 +0000248/// This method inserts names for any unnamed structure types that are used by
249/// the program, and removes names from structure types that are not used by the
250/// program.
251///
Chris Lattnerf9a05322006-02-13 22:22:42 +0000252bool CBackendNameAllUsedStructsAndMergeFunctions::runOnModule(Module &M) {
Chris Lattner68758072004-05-09 06:20:51 +0000253 // Get a set of types that are used by the program...
254 std::set<const Type *> UT = getAnalysis<FindUsedTypes>().getTypes();
Misha Brukmand6a29a52005-04-20 16:05:03 +0000255
Chris Lattner68758072004-05-09 06:20:51 +0000256 // Loop over the module symbol table, removing types from UT that are
Chris Lattnerdbf69f12005-03-08 16:19:59 +0000257 // already named, and removing names for types that are not used.
Chris Lattner68758072004-05-09 06:20:51 +0000258 //
259 SymbolTable &MST = M.getSymbolTable();
Reid Spencer9231ac82004-05-25 08:53:40 +0000260 for (SymbolTable::type_iterator TI = MST.type_begin(), TE = MST.type_end();
261 TI != TE; ) {
262 SymbolTable::type_iterator I = TI++;
Chris Lattnerdbf69f12005-03-08 16:19:59 +0000263
264 // If this is not used, remove it from the symbol table.
265 std::set<const Type *>::iterator UTI = UT.find(I->second);
266 if (UTI == UT.end())
267 MST.remove(I);
268 else
269 UT.erase(UTI); // Only keep one name for this type.
Reid Spencer9231ac82004-05-25 08:53:40 +0000270 }
Chris Lattner68758072004-05-09 06:20:51 +0000271
272 // UT now contains types that are not named. Loop over it, naming
273 // structure types.
274 //
275 bool Changed = false;
Chris Lattner9d1af972004-05-28 05:47:27 +0000276 unsigned RenameCounter = 0;
Chris Lattner68758072004-05-09 06:20:51 +0000277 for (std::set<const Type *>::const_iterator I = UT.begin(), E = UT.end();
278 I != E; ++I)
279 if (const StructType *ST = dyn_cast<StructType>(*I)) {
Chris Lattner9d1af972004-05-28 05:47:27 +0000280 while (M.addTypeName("unnamed"+utostr(RenameCounter), ST))
281 ++RenameCounter;
Chris Lattner68758072004-05-09 06:20:51 +0000282 Changed = true;
283 }
Chris Lattnerf9a05322006-02-13 22:22:42 +0000284
285
286 // Loop over all external functions and globals. If we have two with
287 // identical names, merge them.
288 // FIXME: This code should disappear when we don't allow values with the same
289 // names when they have different types!
290 std::map<std::string, GlobalValue*> ExtSymbols;
291 for (Module::iterator I = M.begin(), E = M.end(); I != E;) {
292 Function *GV = I++;
293 if (GV->isExternal() && GV->hasName()) {
294 std::pair<std::map<std::string, GlobalValue*>::iterator, bool> X
295 = ExtSymbols.insert(std::make_pair(GV->getName(), GV));
296 if (!X.second) {
297 // Found a conflict, replace this global with the previous one.
298 GlobalValue *OldGV = X.first->second;
Reid Spencer4da49122006-12-12 05:05:00 +0000299 GV->replaceAllUsesWith(ConstantExpr::getBitCast(OldGV, GV->getType()));
Chris Lattnerf9a05322006-02-13 22:22:42 +0000300 GV->eraseFromParent();
301 Changed = true;
302 }
303 }
304 }
305 // Do the same for globals.
306 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
307 I != E;) {
308 GlobalVariable *GV = I++;
309 if (GV->isExternal() && GV->hasName()) {
310 std::pair<std::map<std::string, GlobalValue*>::iterator, bool> X
311 = ExtSymbols.insert(std::make_pair(GV->getName(), GV));
312 if (!X.second) {
313 // Found a conflict, replace this global with the previous one.
314 GlobalValue *OldGV = X.first->second;
Reid Spencer4da49122006-12-12 05:05:00 +0000315 GV->replaceAllUsesWith(ConstantExpr::getBitCast(OldGV, GV->getType()));
Chris Lattnerf9a05322006-02-13 22:22:42 +0000316 GV->eraseFromParent();
317 Changed = true;
318 }
319 }
320 }
321
Chris Lattner68758072004-05-09 06:20:51 +0000322 return Changed;
323}
324
Chris Lattner0c54d892006-05-23 23:39:48 +0000325/// printStructReturnPointerFunctionType - This is like printType for a struct
326/// return type, except, instead of printing the type as void (*)(Struct*, ...)
327/// print it as "Struct (*)(...)", for struct return functions.
328void CWriter::printStructReturnPointerFunctionType(std::ostream &Out,
329 const PointerType *TheTy) {
330 const FunctionType *FTy = cast<FunctionType>(TheTy->getElementType());
331 std::stringstream FunctionInnards;
332 FunctionInnards << " (*) (";
333 bool PrintedType = false;
334
335 FunctionType::param_iterator I = FTy->param_begin(), E = FTy->param_end();
336 const Type *RetTy = cast<PointerType>(I->get())->getElementType();
337 for (++I; I != E; ++I) {
338 if (PrintedType)
339 FunctionInnards << ", ";
340 printType(FunctionInnards, *I, "");
341 PrintedType = true;
342 }
343 if (FTy->isVarArg()) {
344 if (PrintedType)
345 FunctionInnards << ", ...";
346 } else if (!PrintedType) {
347 FunctionInnards << "void";
348 }
349 FunctionInnards << ')';
350 std::string tstr = FunctionInnards.str();
351 printType(Out, RetTy, tstr);
352}
353
Chris Lattner68758072004-05-09 06:20:51 +0000354
Chris Lattner83c57752002-08-19 22:17:53 +0000355// Pass the Type* and the variable name and this prints out the variable
356// declaration.
357//
Chris Lattneredd8ce12003-05-03 03:14:35 +0000358std::ostream &CWriter::printType(std::ostream &Out, const Type *Ty,
359 const std::string &NameSoFar,
Chris Lattner7635ea42003-11-03 01:01:59 +0000360 bool IgnoreName) {
Chris Lattner83c57752002-08-19 22:17:53 +0000361 if (Ty->isPrimitiveType())
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000362 switch (Ty->getTypeID()) {
Chris Lattner83c57752002-08-19 22:17:53 +0000363 case Type::VoidTyID: return Out << "void " << NameSoFar;
364 case Type::BoolTyID: return Out << "bool " << NameSoFar;
365 case Type::UByteTyID: return Out << "unsigned char " << NameSoFar;
366 case Type::SByteTyID: return Out << "signed char " << NameSoFar;
367 case Type::UShortTyID: return Out << "unsigned short " << NameSoFar;
368 case Type::ShortTyID: return Out << "short " << NameSoFar;
369 case Type::UIntTyID: return Out << "unsigned " << NameSoFar;
370 case Type::IntTyID: return Out << "int " << NameSoFar;
371 case Type::ULongTyID: return Out << "unsigned long long " << NameSoFar;
372 case Type::LongTyID: return Out << "signed long long " << NameSoFar;
373 case Type::FloatTyID: return Out << "float " << NameSoFar;
374 case Type::DoubleTyID: return Out << "double " << NameSoFar;
375 default :
Bill Wendlingf5da1332006-12-07 22:21:48 +0000376 cerr << "Unknown primitive type: " << *Ty << "\n";
Chris Lattner83c57752002-08-19 22:17:53 +0000377 abort();
378 }
Misha Brukmand6a29a52005-04-20 16:05:03 +0000379
Chris Lattner83c57752002-08-19 22:17:53 +0000380 // Check to see if the type is named.
Chris Lattner04b72c82002-10-16 00:08:22 +0000381 if (!IgnoreName || isa<OpaqueType>(Ty)) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000382 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000383 if (I != TypeNames.end()) return Out << I->second << ' ' << NameSoFar;
Chris Lattner04b72c82002-10-16 00:08:22 +0000384 }
Chris Lattner83c57752002-08-19 22:17:53 +0000385
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000386 switch (Ty->getTypeID()) {
Chris Lattner83c57752002-08-19 22:17:53 +0000387 case Type::FunctionTyID: {
Chris Lattner0c54d892006-05-23 23:39:48 +0000388 const FunctionType *FTy = cast<FunctionType>(Ty);
Misha Brukmand6a29a52005-04-20 16:05:03 +0000389 std::stringstream FunctionInnards;
Joel Stanley54f60322003-06-25 04:52:09 +0000390 FunctionInnards << " (" << NameSoFar << ") (";
Chris Lattner0c54d892006-05-23 23:39:48 +0000391 for (FunctionType::param_iterator I = FTy->param_begin(),
392 E = FTy->param_end(); I != E; ++I) {
393 if (I != FTy->param_begin())
Joel Stanley54f60322003-06-25 04:52:09 +0000394 FunctionInnards << ", ";
395 printType(FunctionInnards, *I, "");
Chris Lattner83c57752002-08-19 22:17:53 +0000396 }
Chris Lattner0c54d892006-05-23 23:39:48 +0000397 if (FTy->isVarArg()) {
398 if (FTy->getNumParams())
Reid Spencer9231ac82004-05-25 08:53:40 +0000399 FunctionInnards << ", ...";
Chris Lattner0c54d892006-05-23 23:39:48 +0000400 } else if (!FTy->getNumParams()) {
Joel Stanley54f60322003-06-25 04:52:09 +0000401 FunctionInnards << "void";
Chris Lattner83c57752002-08-19 22:17:53 +0000402 }
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000403 FunctionInnards << ')';
Joel Stanley54f60322003-06-25 04:52:09 +0000404 std::string tstr = FunctionInnards.str();
Chris Lattner0c54d892006-05-23 23:39:48 +0000405 printType(Out, FTy->getReturnType(), tstr);
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000406 return Out;
Chris Lattner83c57752002-08-19 22:17:53 +0000407 }
408 case Type::StructTyID: {
409 const StructType *STy = cast<StructType>(Ty);
410 Out << NameSoFar + " {\n";
411 unsigned Idx = 0;
Chris Lattnerd21cd802004-02-09 04:37:31 +0000412 for (StructType::element_iterator I = STy->element_begin(),
413 E = STy->element_end(); I != E; ++I) {
Chris Lattner83c57752002-08-19 22:17:53 +0000414 Out << " ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000415 printType(Out, *I, "field" + utostr(Idx++));
Chris Lattner83c57752002-08-19 22:17:53 +0000416 Out << ";\n";
417 }
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000418 return Out << '}';
Misha Brukmand6a29a52005-04-20 16:05:03 +0000419 }
Chris Lattner83c57752002-08-19 22:17:53 +0000420
421 case Type::PointerTyID: {
422 const PointerType *PTy = cast<PointerType>(Ty);
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000423 std::string ptrName = "*" + NameSoFar;
424
Robert Bocchinob78e8382006-01-20 20:43:57 +0000425 if (isa<ArrayType>(PTy->getElementType()) ||
426 isa<PackedType>(PTy->getElementType()))
Chris Lattner8917d362003-11-03 04:31:54 +0000427 ptrName = "(" + ptrName + ")";
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000428
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000429 return printType(Out, PTy->getElementType(), ptrName);
Misha Brukman8f9f9a22003-07-21 16:34:35 +0000430 }
Chris Lattner83c57752002-08-19 22:17:53 +0000431
432 case Type::ArrayTyID: {
433 const ArrayType *ATy = cast<ArrayType>(Ty);
434 unsigned NumElements = ATy->getNumElements();
Chris Lattner3e3b6f72004-12-15 23:13:15 +0000435 if (NumElements == 0) NumElements = 1;
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000436 return printType(Out, ATy->getElementType(),
Chris Lattner83c57752002-08-19 22:17:53 +0000437 NameSoFar + "[" + utostr(NumElements) + "]");
438 }
Chris Lattner04b72c82002-10-16 00:08:22 +0000439
Robert Bocchinob78e8382006-01-20 20:43:57 +0000440 case Type::PackedTyID: {
441 const PackedType *PTy = cast<PackedType>(Ty);
442 unsigned NumElements = PTy->getNumElements();
443 if (NumElements == 0) NumElements = 1;
444 return printType(Out, PTy->getElementType(),
445 NameSoFar + "[" + utostr(NumElements) + "]");
446 }
447
Chris Lattner04b72c82002-10-16 00:08:22 +0000448 case Type::OpaqueTyID: {
449 static int Count = 0;
Chris Lattneredd8ce12003-05-03 03:14:35 +0000450 std::string TyName = "struct opaque_" + itostr(Count++);
Chris Lattner04b72c82002-10-16 00:08:22 +0000451 assert(TypeNames.find(Ty) == TypeNames.end());
452 TypeNames[Ty] = TyName;
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000453 return Out << TyName << ' ' << NameSoFar;
Chris Lattner04b72c82002-10-16 00:08:22 +0000454 }
Chris Lattner83c57752002-08-19 22:17:53 +0000455 default:
456 assert(0 && "Unhandled case in getTypeProps!");
457 abort();
458 }
459
460 return Out;
461}
462
Chris Lattner6d492922002-08-19 21:32:41 +0000463void CWriter::printConstantArray(ConstantArray *CPA) {
464
465 // As a special case, print the array as a string if it is an array of
466 // ubytes or an array of sbytes with positive values.
Misha Brukmand6a29a52005-04-20 16:05:03 +0000467 //
Chris Lattner6d492922002-08-19 21:32:41 +0000468 const Type *ETy = CPA->getType()->getElementType();
469 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
470
471 // Make sure the last character is a null char, as automatically added by C
Chris Lattner02da6c02003-06-16 12:09:09 +0000472 if (isString && (CPA->getNumOperands() == 0 ||
473 !cast<Constant>(*(CPA->op_end()-1))->isNullValue()))
Chris Lattner6d492922002-08-19 21:32:41 +0000474 isString = false;
Misha Brukmand6a29a52005-04-20 16:05:03 +0000475
Chris Lattner6d492922002-08-19 21:32:41 +0000476 if (isString) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000477 Out << '\"';
Chris Lattner02da6c02003-06-16 12:09:09 +0000478 // Keep track of whether the last number was a hexadecimal escape
479 bool LastWasHex = false;
480
Chris Lattner6d492922002-08-19 21:32:41 +0000481 // Do not include the last character, which we know is null
482 for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000483 unsigned char C = cast<ConstantInt>(CPA->getOperand(i))->getZExtValue();
Misha Brukmand6a29a52005-04-20 16:05:03 +0000484
Chris Lattner02da6c02003-06-16 12:09:09 +0000485 // Print it out literally if it is a printable character. The only thing
486 // to be careful about is when the last letter output was a hex escape
487 // code, in which case we have to be careful not to print out hex digits
Chris Lattnerda920902003-06-16 12:21:19 +0000488 // explicitly (the C compiler thinks it is a continuation of the previous
489 // character, sheesh...)
Chris Lattner02da6c02003-06-16 12:09:09 +0000490 //
491 if (isprint(C) && (!LastWasHex || !isxdigit(C))) {
492 LastWasHex = false;
Nick Hildenbrandt088b4722002-11-06 21:40:23 +0000493 if (C == '"' || C == '\\')
494 Out << "\\" << C;
Nick Hildenbrandtc3dd2af2002-09-30 21:11:55 +0000495 else
496 Out << C;
Chris Lattner6d492922002-08-19 21:32:41 +0000497 } else {
Chris Lattner02da6c02003-06-16 12:09:09 +0000498 LastWasHex = false;
Chris Lattner6d492922002-08-19 21:32:41 +0000499 switch (C) {
500 case '\n': Out << "\\n"; break;
501 case '\t': Out << "\\t"; break;
502 case '\r': Out << "\\r"; break;
503 case '\v': Out << "\\v"; break;
504 case '\a': Out << "\\a"; break;
Nick Hildenbrandtc3dd2af2002-09-30 21:11:55 +0000505 case '\"': Out << "\\\""; break;
Misha Brukmand6a29a52005-04-20 16:05:03 +0000506 case '\'': Out << "\\\'"; break;
Chris Lattner6d492922002-08-19 21:32:41 +0000507 default:
508 Out << "\\x";
Chris Lattner02da6c02003-06-16 12:09:09 +0000509 Out << (char)(( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'));
510 Out << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
511 LastWasHex = true;
Chris Lattner6d492922002-08-19 21:32:41 +0000512 break;
513 }
514 }
515 }
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000516 Out << '\"';
Chris Lattner6d492922002-08-19 21:32:41 +0000517 } else {
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000518 Out << '{';
Chris Lattner6d492922002-08-19 21:32:41 +0000519 if (CPA->getNumOperands()) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000520 Out << ' ';
Chris Lattner6d492922002-08-19 21:32:41 +0000521 printConstant(cast<Constant>(CPA->getOperand(0)));
522 for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
523 Out << ", ";
524 printConstant(cast<Constant>(CPA->getOperand(i)));
525 }
526 }
527 Out << " }";
528 }
529}
530
Robert Bocchinob78e8382006-01-20 20:43:57 +0000531void CWriter::printConstantPacked(ConstantPacked *CP) {
532 Out << '{';
533 if (CP->getNumOperands()) {
534 Out << ' ';
535 printConstant(cast<Constant>(CP->getOperand(0)));
536 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
537 Out << ", ";
538 printConstant(cast<Constant>(CP->getOperand(i)));
539 }
540 }
541 Out << " }";
542}
543
Chris Lattnerdd76aca2003-10-05 00:40:51 +0000544// isFPCSafeToPrint - Returns true if we may assume that CFP may be written out
545// textually as a double (rather than as a reference to a stack-allocated
546// variable). We decide this by converting CFP to a string and back into a
547// double, and then checking whether the conversion results in a bit-equal
548// double to the original value of CFP. This depends on us and the target C
549// compiler agreeing on the conversion process (which is pretty likely since we
550// only deal in IEEE FP).
551//
Chris Lattnercb3ad012004-05-09 20:41:32 +0000552static bool isFPCSafeToPrint(const ConstantFP *CFP) {
Reid Spencerbcf81242006-11-05 19:26:37 +0000553#if HAVE_PRINTF_A && ENABLE_CBE_PRINTF_A
Chris Lattner5ef6ffd2003-10-12 08:12:58 +0000554 char Buffer[100];
555 sprintf(Buffer, "%a", CFP->getValue());
556
557 if (!strncmp(Buffer, "0x", 2) ||
558 !strncmp(Buffer, "-0x", 3) ||
559 !strncmp(Buffer, "+0x", 3))
560 return atof(Buffer) == CFP->getValue();
561 return false;
562#else
Brian Gaekeb471a232003-06-17 23:55:35 +0000563 std::string StrVal = ftostr(CFP->getValue());
Chris Lattner9860e772003-10-12 04:36:29 +0000564
565 while (StrVal[0] == ' ')
566 StrVal.erase(StrVal.begin());
567
Chris Lattnerdd76aca2003-10-05 00:40:51 +0000568 // Check to make sure that the stringized number is not some string like "Inf"
569 // or NaN. Check that the string matches the "[-+]?[0-9]" regex.
Brian Gaekeb471a232003-06-17 23:55:35 +0000570 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
571 ((StrVal[0] == '-' || StrVal[0] == '+') &&
572 (StrVal[1] >= '0' && StrVal[1] <= '9')))
573 // Reparse stringized version!
Chris Lattnerdd76aca2003-10-05 00:40:51 +0000574 return atof(StrVal.c_str()) == CFP->getValue();
Brian Gaekeb471a232003-06-17 23:55:35 +0000575 return false;
Chris Lattner5ef6ffd2003-10-12 08:12:58 +0000576#endif
Brian Gaekeb471a232003-06-17 23:55:35 +0000577}
Chris Lattner6d492922002-08-19 21:32:41 +0000578
Reid Spencer3da59db2006-11-27 01:05:10 +0000579/// Print out the casting for a cast operation. This does the double casting
580/// necessary for conversion to the destination type, if necessary.
581/// @returns true if a closing paren is necessary
582/// @brief Print a cast
583void CWriter::printCast(unsigned opc, const Type *SrcTy, const Type *DstTy) {
584 Out << '(';
585 printType(Out, DstTy);
586 Out << ')';
587 switch (opc) {
588 case Instruction::UIToFP:
589 case Instruction::ZExt:
590 if (SrcTy->isSigned()) {
591 Out << '(';
592 printType(Out, SrcTy->getUnsignedVersion());
593 Out << ')';
594 }
595 break;
596 case Instruction::SIToFP:
597 case Instruction::SExt:
598 if (SrcTy->isUnsigned()) {
599 Out << '(';
600 printType(Out, SrcTy->getSignedVersion());
601 Out << ')';
602 }
603 break;
604 case Instruction::IntToPtr:
605 case Instruction::PtrToInt:
606 // Avoid "cast to pointer from integer of different size" warnings
607 Out << "(unsigned long)";
608 break;
609 case Instruction::Trunc:
610 case Instruction::BitCast:
611 case Instruction::FPExt:
612 case Instruction::FPTrunc:
613 case Instruction::FPToSI:
614 case Instruction::FPToUI:
615 default:
616 break;
617 }
618}
619
Chris Lattner6d492922002-08-19 21:32:41 +0000620// printConstant - The LLVM Constant to C Constant converter.
621void CWriter::printConstant(Constant *CPV) {
622 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
623 switch (CE->getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000624 case Instruction::Trunc:
625 case Instruction::ZExt:
626 case Instruction::SExt:
627 case Instruction::FPTrunc:
628 case Instruction::FPExt:
629 case Instruction::UIToFP:
630 case Instruction::SIToFP:
631 case Instruction::FPToUI:
632 case Instruction::FPToSI:
633 case Instruction::PtrToInt:
634 case Instruction::IntToPtr:
635 case Instruction::BitCast:
636 Out << "(";
637 printCast(CE->getOpcode(), CE->getOperand(0)->getType(), CE->getType());
638 if (CE->getOpcode() == Instruction::SExt &&
639 CE->getOperand(0)->getType() == Type::BoolTy) {
640 // Make sure we really sext from bool here by subtracting from 0
641 Out << "0-";
642 }
Chris Lattner84c0d5e2003-05-14 17:50:19 +0000643 printConstant(CE->getOperand(0));
Reid Spencer639cf7d2006-11-27 18:51:06 +0000644 if (CE->getType() == Type::BoolTy &&
645 (CE->getOpcode() == Instruction::Trunc ||
646 CE->getOpcode() == Instruction::FPToUI ||
647 CE->getOpcode() == Instruction::FPToSI ||
648 CE->getOpcode() == Instruction::PtrToInt)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000649 // Make sure we really truncate to bool here by anding with 1
650 Out << "&1u";
651 }
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000652 Out << ')';
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000653 return;
654
655 case Instruction::GetElementPtr:
Nick Hildenbrandtd2eb3862002-10-03 20:47:24 +0000656 Out << "(&(";
Chris Lattner23e90702003-11-25 20:49:55 +0000657 printIndexingExpression(CE->getOperand(0), gep_type_begin(CPV),
658 gep_type_end(CPV));
Nick Hildenbrandtd2eb3862002-10-03 20:47:24 +0000659 Out << "))";
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000660 return;
Chris Lattner6683dbf2004-04-01 05:28:26 +0000661 case Instruction::Select:
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000662 Out << '(';
Chris Lattner6683dbf2004-04-01 05:28:26 +0000663 printConstant(CE->getOperand(0));
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000664 Out << '?';
Chris Lattner6683dbf2004-04-01 05:28:26 +0000665 printConstant(CE->getOperand(1));
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000666 Out << ':';
Chris Lattner6683dbf2004-04-01 05:28:26 +0000667 printConstant(CE->getOperand(2));
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000668 Out << ')';
Chris Lattner6683dbf2004-04-01 05:28:26 +0000669 return;
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000670 case Instruction::Add:
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000671 case Instruction::Sub:
Chris Lattner29255922003-08-14 19:19:53 +0000672 case Instruction::Mul:
Reid Spencer1628cec2006-10-26 06:15:43 +0000673 case Instruction::SDiv:
674 case Instruction::UDiv:
675 case Instruction::FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +0000676 case Instruction::URem:
677 case Instruction::SRem:
678 case Instruction::FRem:
Chris Lattnerf376e5e2004-12-29 04:00:09 +0000679 case Instruction::And:
680 case Instruction::Or:
681 case Instruction::Xor:
Chris Lattner29255922003-08-14 19:19:53 +0000682 case Instruction::SetEQ:
683 case Instruction::SetNE:
684 case Instruction::SetLT:
685 case Instruction::SetLE:
686 case Instruction::SetGT:
687 case Instruction::SetGE:
Brian Gaeke0415b6c2003-11-22 05:02:56 +0000688 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +0000689 case Instruction::LShr:
690 case Instruction::AShr:
Reid Spencer72ddc212006-10-26 06:17:40 +0000691 {
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000692 Out << '(';
Reid Spencer1628cec2006-10-26 06:15:43 +0000693 bool NeedsClosingParens = printConstExprCast(CE);
694 printConstantWithCast(CE->getOperand(0), CE->getOpcode());
Chris Lattner29255922003-08-14 19:19:53 +0000695 switch (CE->getOpcode()) {
696 case Instruction::Add: Out << " + "; break;
697 case Instruction::Sub: Out << " - "; break;
698 case Instruction::Mul: Out << " * "; break;
Reid Spencer0a783f72006-11-02 01:53:59 +0000699 case Instruction::URem:
700 case Instruction::SRem:
701 case Instruction::FRem: Out << " % "; break;
Reid Spencer1628cec2006-10-26 06:15:43 +0000702 case Instruction::UDiv:
703 case Instruction::SDiv:
704 case Instruction::FDiv: Out << " / "; break;
Chris Lattnerf376e5e2004-12-29 04:00:09 +0000705 case Instruction::And: Out << " & "; break;
706 case Instruction::Or: Out << " | "; break;
707 case Instruction::Xor: Out << " ^ "; break;
Chris Lattner29255922003-08-14 19:19:53 +0000708 case Instruction::SetEQ: Out << " == "; break;
709 case Instruction::SetNE: Out << " != "; break;
710 case Instruction::SetLT: Out << " < "; break;
711 case Instruction::SetLE: Out << " <= "; break;
712 case Instruction::SetGT: Out << " > "; break;
713 case Instruction::SetGE: Out << " >= "; break;
Brian Gaeke0415b6c2003-11-22 05:02:56 +0000714 case Instruction::Shl: Out << " << "; break;
Reid Spencer3822ff52006-11-08 06:47:33 +0000715 case Instruction::LShr:
716 case Instruction::AShr: Out << " >> "; break;
Chris Lattner29255922003-08-14 19:19:53 +0000717 default: assert(0 && "Illegal opcode here!");
718 }
Reid Spencer1628cec2006-10-26 06:15:43 +0000719 printConstantWithCast(CE->getOperand(1), CE->getOpcode());
720 if (NeedsClosingParens)
721 Out << "))";
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000722 Out << ')';
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000723 return;
Reid Spencer72ddc212006-10-26 06:17:40 +0000724 }
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000725
Chris Lattner6d492922002-08-19 21:32:41 +0000726 default:
Bill Wendlingf5da1332006-12-07 22:21:48 +0000727 cerr << "CWriter Error: Unhandled constant expression: "
728 << *CE << "\n";
Chris Lattner6d492922002-08-19 21:32:41 +0000729 abort();
730 }
Chris Lattnera9d12c02004-10-16 18:12:13 +0000731 } else if (isa<UndefValue>(CPV) && CPV->getType()->isFirstClassType()) {
Chris Lattner665825e2004-10-17 17:48:59 +0000732 Out << "((";
733 printType(Out, CPV->getType());
734 Out << ")/*UNDEF*/0)";
Chris Lattnera9d12c02004-10-16 18:12:13 +0000735 return;
Chris Lattner6d492922002-08-19 21:32:41 +0000736 }
737
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000738 switch (CPV->getType()->getTypeID()) {
Chris Lattner6d492922002-08-19 21:32:41 +0000739 case Type::BoolTyID:
Chris Lattner33ce7772006-09-28 23:19:29 +0000740 Out << (cast<ConstantBool>(CPV)->getValue() ? '1' : '0');
741 break;
Chris Lattner6d492922002-08-19 21:32:41 +0000742 case Type::SByteTyID:
743 case Type::ShortTyID:
Reid Spencerb83eb642006-10-20 07:07:24 +0000744 Out << cast<ConstantInt>(CPV)->getSExtValue();
Chris Lattner33ce7772006-09-28 23:19:29 +0000745 break;
Chris Lattner45343ea2003-05-12 15:39:31 +0000746 case Type::IntTyID:
Reid Spencerb83eb642006-10-20 07:07:24 +0000747 if ((int)cast<ConstantInt>(CPV)->getSExtValue() == (int)0x80000000)
Chris Lattner40e7c352004-11-30 21:33:58 +0000748 Out << "((int)0x80000000U)"; // Handle MININT specially to avoid warning
Chris Lattner45343ea2003-05-12 15:39:31 +0000749 else
Reid Spencerb83eb642006-10-20 07:07:24 +0000750 Out << cast<ConstantInt>(CPV)->getSExtValue();
Chris Lattner45343ea2003-05-12 15:39:31 +0000751 break;
752
Chris Lattner6d492922002-08-19 21:32:41 +0000753 case Type::LongTyID:
Reid Spencerd10ecf72006-12-06 21:27:07 +0000754 if (cast<ConstantInt>(CPV)->isMinValue(true))
Chris Lattner40e7c352004-11-30 21:33:58 +0000755 Out << "(/*INT64_MIN*/(-9223372036854775807LL)-1)";
756 else
Reid Spencerb83eb642006-10-20 07:07:24 +0000757 Out << cast<ConstantInt>(CPV)->getSExtValue() << "ll";
Chris Lattner33ce7772006-09-28 23:19:29 +0000758 break;
Chris Lattner6d492922002-08-19 21:32:41 +0000759
760 case Type::UByteTyID:
761 case Type::UShortTyID:
Reid Spencerb83eb642006-10-20 07:07:24 +0000762 Out << cast<ConstantInt>(CPV)->getZExtValue();
Chris Lattner33ce7772006-09-28 23:19:29 +0000763 break;
Chris Lattner6d492922002-08-19 21:32:41 +0000764 case Type::UIntTyID:
Reid Spencerb83eb642006-10-20 07:07:24 +0000765 Out << cast<ConstantInt>(CPV)->getZExtValue() << 'u';
Chris Lattner33ce7772006-09-28 23:19:29 +0000766 break;
Chris Lattner6d492922002-08-19 21:32:41 +0000767 case Type::ULongTyID:
Reid Spencerb83eb642006-10-20 07:07:24 +0000768 Out << cast<ConstantInt>(CPV)->getZExtValue() << "ull";
Chris Lattner33ce7772006-09-28 23:19:29 +0000769 break;
Chris Lattner6d492922002-08-19 21:32:41 +0000770
771 case Type::FloatTyID:
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000772 case Type::DoubleTyID: {
773 ConstantFP *FPC = cast<ConstantFP>(CPV);
Chris Lattneredd8ce12003-05-03 03:14:35 +0000774 std::map<const ConstantFP*, unsigned>::iterator I = FPConstantMap.find(FPC);
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000775 if (I != FPConstantMap.end()) {
776 // Because of FP precision problems we must load from a stack allocated
777 // value that holds the value in hex.
778 Out << "(*(" << (FPC->getType() == Type::FloatTy ? "float" : "double")
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000779 << "*)&FPConstant" << I->second << ')';
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000780 } else {
Brian Gaeke043c0bb2004-07-21 03:15:26 +0000781 if (IsNAN(FPC->getValue())) {
782 // The value is NaN
Misha Brukmand6a29a52005-04-20 16:05:03 +0000783
Brian Gaeke8a702e82004-08-25 19:00:42 +0000784 // The prefix for a quiet NaN is 0x7FF8. For a signalling NaN,
785 // it's 0x7ff4.
786 const unsigned long QuietNaN = 0x7ff8UL;
Reid Spencer3ed469c2006-11-02 20:25:50 +0000787 //const unsigned long SignalNaN = 0x7ff4UL;
Brian Gaeke8a702e82004-08-25 19:00:42 +0000788
789 // We need to grab the first part of the FP #
Brian Gaeke8a702e82004-08-25 19:00:42 +0000790 char Buffer[100];
791
Jim Laskeycb6682f2005-08-17 19:34:49 +0000792 uint64_t ll = DoubleToBits(FPC->getValue());
Reid Spencer2bc320d2006-05-31 22:26:11 +0000793 sprintf(Buffer, "0x%llx", static_cast<long long>(ll));
Brian Gaeke8a702e82004-08-25 19:00:42 +0000794
795 std::string Num(&Buffer[0], &Buffer[6]);
796 unsigned long Val = strtoul(Num.c_str(), 0, 16);
797
Brian Gaeke043c0bb2004-07-21 03:15:26 +0000798 if (FPC->getType() == Type::FloatTy)
Brian Gaeke8a702e82004-08-25 19:00:42 +0000799 Out << "LLVM_NAN" << (Val == QuietNaN ? "" : "S") << "F(\""
800 << Buffer << "\") /*nan*/ ";
Brian Gaeke043c0bb2004-07-21 03:15:26 +0000801 else
Brian Gaeke8a702e82004-08-25 19:00:42 +0000802 Out << "LLVM_NAN" << (Val == QuietNaN ? "" : "S") << "(\""
803 << Buffer << "\") /*nan*/ ";
Brian Gaeke043c0bb2004-07-21 03:15:26 +0000804 } else if (IsInf(FPC->getValue())) {
805 // The value is Inf
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000806 if (FPC->getValue() < 0) Out << '-';
Brian Gaeke8a702e82004-08-25 19:00:42 +0000807 Out << "LLVM_INF" << (FPC->getType() == Type::FloatTy ? "F" : "")
808 << " /*inf*/ ";
Brian Gaeke043c0bb2004-07-21 03:15:26 +0000809 } else {
Brian Gaeke07b52b32004-08-25 19:37:26 +0000810 std::string Num;
Reid Spencerbcf81242006-11-05 19:26:37 +0000811#if HAVE_PRINTF_A && ENABLE_CBE_PRINTF_A
Brian Gaeke07b52b32004-08-25 19:37:26 +0000812 // Print out the constant as a floating point number.
813 char Buffer[100];
814 sprintf(Buffer, "%a", FPC->getValue());
815 Num = Buffer;
816#else
817 Num = ftostr(FPC->getValue());
818#endif
Brian Gaeke043c0bb2004-07-21 03:15:26 +0000819 Out << Num;
820 }
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000821 }
822 break;
823 }
Chris Lattner6d492922002-08-19 21:32:41 +0000824
825 case Type::ArrayTyID:
Chris Lattnera9d12c02004-10-16 18:12:13 +0000826 if (isa<ConstantAggregateZero>(CPV) || isa<UndefValue>(CPV)) {
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000827 const ArrayType *AT = cast<ArrayType>(CPV->getType());
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000828 Out << '{';
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000829 if (AT->getNumElements()) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000830 Out << ' ';
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000831 Constant *CZ = Constant::getNullValue(AT->getElementType());
832 printConstant(CZ);
833 for (unsigned i = 1, e = AT->getNumElements(); i != e; ++i) {
834 Out << ", ";
835 printConstant(CZ);
836 }
837 }
838 Out << " }";
839 } else {
840 printConstantArray(cast<ConstantArray>(CPV));
841 }
Chris Lattner6d492922002-08-19 21:32:41 +0000842 break;
843
Robert Bocchinob78e8382006-01-20 20:43:57 +0000844 case Type::PackedTyID:
845 if (isa<ConstantAggregateZero>(CPV) || isa<UndefValue>(CPV)) {
846 const PackedType *AT = cast<PackedType>(CPV->getType());
847 Out << '{';
848 if (AT->getNumElements()) {
849 Out << ' ';
850 Constant *CZ = Constant::getNullValue(AT->getElementType());
851 printConstant(CZ);
852 for (unsigned i = 1, e = AT->getNumElements(); i != e; ++i) {
853 Out << ", ";
854 printConstant(CZ);
855 }
856 }
857 Out << " }";
858 } else {
859 printConstantPacked(cast<ConstantPacked>(CPV));
860 }
861 break;
862
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000863 case Type::StructTyID:
Chris Lattnera9d12c02004-10-16 18:12:13 +0000864 if (isa<ConstantAggregateZero>(CPV) || isa<UndefValue>(CPV)) {
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000865 const StructType *ST = cast<StructType>(CPV->getType());
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000866 Out << '{';
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000867 if (ST->getNumElements()) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000868 Out << ' ';
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000869 printConstant(Constant::getNullValue(ST->getElementType(0)));
870 for (unsigned i = 1, e = ST->getNumElements(); i != e; ++i) {
871 Out << ", ";
872 printConstant(Constant::getNullValue(ST->getElementType(i)));
873 }
Chris Lattner6d492922002-08-19 21:32:41 +0000874 }
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000875 Out << " }";
876 } else {
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000877 Out << '{';
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000878 if (CPV->getNumOperands()) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000879 Out << ' ';
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000880 printConstant(cast<Constant>(CPV->getOperand(0)));
881 for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) {
882 Out << ", ";
883 printConstant(cast<Constant>(CPV->getOperand(i)));
884 }
885 }
886 Out << " }";
Chris Lattner6d492922002-08-19 21:32:41 +0000887 }
Chris Lattner6d492922002-08-19 21:32:41 +0000888 break;
Chris Lattner6d492922002-08-19 21:32:41 +0000889
890 case Type::PointerTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000891 if (isa<ConstantPointerNull>(CPV)) {
Chris Lattnercf135cb2003-06-02 03:10:53 +0000892 Out << "((";
893 printType(Out, CPV->getType());
894 Out << ")/*NULL*/0)";
Chris Lattner6d492922002-08-19 21:32:41 +0000895 break;
Reid Spencer518310c2004-07-18 00:44:37 +0000896 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(CPV)) {
897 writeOperand(GV);
Chris Lattner6d492922002-08-19 21:32:41 +0000898 break;
899 }
900 // FALL THROUGH
901 default:
Bill Wendlingf5da1332006-12-07 22:21:48 +0000902 cerr << "Unknown constant type: " << *CPV << "\n";
Chris Lattner6d492922002-08-19 21:32:41 +0000903 abort();
904 }
905}
906
Reid Spencer1628cec2006-10-26 06:15:43 +0000907// Some constant expressions need to be casted back to the original types
908// because their operands were casted to the expected type. This function takes
909// care of detecting that case and printing the cast for the ConstantExpr.
910bool CWriter::printConstExprCast(const ConstantExpr* CE) {
Reid Spencer3822ff52006-11-08 06:47:33 +0000911 bool NeedsExplicitCast = false;
Reid Spencer3da59db2006-11-27 01:05:10 +0000912 const Type *Ty = CE->getOperand(0)->getType();
Reid Spencer1628cec2006-10-26 06:15:43 +0000913 switch (CE->getOpcode()) {
Reid Spencer3822ff52006-11-08 06:47:33 +0000914 case Instruction::LShr:
Reid Spencer0a783f72006-11-02 01:53:59 +0000915 case Instruction::URem:
Reid Spencer3da59db2006-11-27 01:05:10 +0000916 case Instruction::UDiv:
917 NeedsExplicitCast = Ty->isSigned(); break;
Reid Spencer3822ff52006-11-08 06:47:33 +0000918 case Instruction::AShr:
Reid Spencer0a783f72006-11-02 01:53:59 +0000919 case Instruction::SRem:
Reid Spencer3da59db2006-11-27 01:05:10 +0000920 case Instruction::SDiv:
921 NeedsExplicitCast = Ty->isUnsigned(); break;
922 case Instruction::ZExt:
923 case Instruction::SExt:
924 case Instruction::Trunc:
925 case Instruction::FPTrunc:
926 case Instruction::FPExt:
927 case Instruction::UIToFP:
928 case Instruction::SIToFP:
929 case Instruction::FPToUI:
930 case Instruction::FPToSI:
931 case Instruction::PtrToInt:
932 case Instruction::IntToPtr:
933 case Instruction::BitCast:
934 Ty = CE->getType();
935 NeedsExplicitCast = true;
936 break;
Reid Spencer1628cec2006-10-26 06:15:43 +0000937 default: break;
938 }
Reid Spencer3822ff52006-11-08 06:47:33 +0000939 if (NeedsExplicitCast) {
Reid Spencer1628cec2006-10-26 06:15:43 +0000940 Out << "((";
941 printType(Out, Ty);
942 Out << ")(";
943 }
Reid Spencer3822ff52006-11-08 06:47:33 +0000944 return NeedsExplicitCast;
Reid Spencer1628cec2006-10-26 06:15:43 +0000945}
946
947// Print a constant assuming that it is the operand for a given Opcode. The
948// opcodes that care about sign need to cast their operands to the expected
949// type before the operation proceeds. This function does the casting.
950void CWriter::printConstantWithCast(Constant* CPV, unsigned Opcode) {
951
952 // Extract the operand's type, we'll need it.
953 const Type* OpTy = CPV->getType();
954
955 // Indicate whether to do the cast or not.
956 bool shouldCast = false;
957
958 // Based on the Opcode for which this Constant is being written, determine
959 // the new type to which the operand should be casted by setting the value
Reid Spencer3da59db2006-11-27 01:05:10 +0000960 // of OpTy. If we change OpTy, also set shouldCast to true so it gets
961 // casted below.
Reid Spencer1628cec2006-10-26 06:15:43 +0000962 switch (Opcode) {
963 default:
964 // for most instructions, it doesn't matter
965 break;
Reid Spencer3822ff52006-11-08 06:47:33 +0000966 case Instruction::LShr:
Reid Spencer1628cec2006-10-26 06:15:43 +0000967 case Instruction::UDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +0000968 case Instruction::URem:
969 // For UDiv/URem get correct type
Reid Spencer1628cec2006-10-26 06:15:43 +0000970 if (OpTy->isSigned()) {
971 OpTy = OpTy->getUnsignedVersion();
972 shouldCast = true;
973 }
974 break;
Reid Spencer3822ff52006-11-08 06:47:33 +0000975 case Instruction::AShr:
Reid Spencer1628cec2006-10-26 06:15:43 +0000976 case Instruction::SDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +0000977 case Instruction::SRem:
978 // For SDiv/SRem get correct type
Reid Spencer1628cec2006-10-26 06:15:43 +0000979 if (OpTy->isUnsigned()) {
980 OpTy = OpTy->getSignedVersion();
981 shouldCast = true;
982 }
983 break;
984 }
985
Reid Spencer3da59db2006-11-27 01:05:10 +0000986 // Write out the casted constant if we should, otherwise just write the
Reid Spencer1628cec2006-10-26 06:15:43 +0000987 // operand.
988 if (shouldCast) {
989 Out << "((";
990 printType(Out, OpTy);
991 Out << ")";
992 printConstant(CPV);
993 Out << ")";
994 } else
995 writeOperand(CPV);
996
997}
998
Chris Lattner6d492922002-08-19 21:32:41 +0000999void CWriter::writeOperandInternal(Value *Operand) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001000 if (Instruction *I = dyn_cast<Instruction>(Operand))
Chris Lattnera8ab89e2003-06-17 04:39:14 +00001001 if (isInlinableInst(*I) && !isDirectAlloca(I)) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001002 // Should we inline this instruction to build a tree?
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001003 Out << '(';
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001004 visit(*I);
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001005 Out << ')';
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001006 return;
1007 }
Misha Brukmand6a29a52005-04-20 16:05:03 +00001008
Reid Spencer518310c2004-07-18 00:44:37 +00001009 Constant* CPV = dyn_cast<Constant>(Operand);
1010 if (CPV && !isa<GlobalValue>(CPV)) {
Misha Brukmand6a29a52005-04-20 16:05:03 +00001011 printConstant(CPV);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001012 } else {
Brian Gaeked9fb37a2003-07-24 20:20:44 +00001013 Out << Mang->getValueName(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001014 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001015}
1016
Andrew Lenharth29c277f2006-11-27 23:50:49 +00001017void CWriter::writeOperandRaw(Value *Operand) {
1018 Constant* CPV = dyn_cast<Constant>(Operand);
1019 if (CPV && !isa<GlobalValue>(CPV)) {
1020 printConstant(CPV);
1021 } else {
1022 Out << Mang->getValueName(Operand);
1023 }
1024}
1025
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001026void CWriter::writeOperand(Value *Operand) {
Chris Lattnera8ab89e2003-06-17 04:39:14 +00001027 if (isa<GlobalVariable>(Operand) || isDirectAlloca(Operand))
Reid Spencer3da59db2006-11-27 01:05:10 +00001028 Out << "(&"; // Global variables are referenced as their addresses by llvm
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001029
1030 writeOperandInternal(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001031
Chris Lattnera8ab89e2003-06-17 04:39:14 +00001032 if (isa<GlobalVariable>(Operand) || isDirectAlloca(Operand))
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001033 Out << ')';
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001034}
1035
Reid Spencer1628cec2006-10-26 06:15:43 +00001036// Some instructions need to have their result value casted back to the
1037// original types because their operands were casted to the expected type.
1038// This function takes care of detecting that case and printing the cast
1039// for the Instruction.
1040bool CWriter::writeInstructionCast(const Instruction &I) {
Reid Spencer3822ff52006-11-08 06:47:33 +00001041 bool NeedsExplicitCast = false;
Reid Spencer3da59db2006-11-27 01:05:10 +00001042 const Type *Ty = I.getOperand(0)->getType();
Reid Spencer1628cec2006-10-26 06:15:43 +00001043 switch (I.getOpcode()) {
Reid Spencer3822ff52006-11-08 06:47:33 +00001044 case Instruction::LShr:
Reid Spencer0a783f72006-11-02 01:53:59 +00001045 case Instruction::URem:
Reid Spencer3da59db2006-11-27 01:05:10 +00001046 case Instruction::UDiv:
1047 NeedsExplicitCast = Ty->isSigned(); break;
Reid Spencer3822ff52006-11-08 06:47:33 +00001048 case Instruction::AShr:
Reid Spencer0a783f72006-11-02 01:53:59 +00001049 case Instruction::SRem:
Reid Spencer3da59db2006-11-27 01:05:10 +00001050 case Instruction::SDiv:
1051 NeedsExplicitCast = Ty->isUnsigned(); break;
1052 case Instruction::ZExt:
1053 case Instruction::SExt:
1054 case Instruction::Trunc:
1055 case Instruction::FPTrunc:
1056 case Instruction::FPExt:
1057 case Instruction::UIToFP:
1058 case Instruction::SIToFP:
1059 case Instruction::FPToUI:
1060 case Instruction::FPToSI:
1061 case Instruction::PtrToInt:
1062 case Instruction::IntToPtr:
1063 case Instruction::BitCast:
1064 Ty = I.getType();
1065 NeedsExplicitCast = true;
1066 break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001067 default: break;
1068 }
Reid Spencer3822ff52006-11-08 06:47:33 +00001069 if (NeedsExplicitCast) {
Reid Spencer1628cec2006-10-26 06:15:43 +00001070 Out << "((";
1071 printType(Out, Ty);
1072 Out << ")(";
1073 }
Reid Spencer3822ff52006-11-08 06:47:33 +00001074 return NeedsExplicitCast;
Reid Spencer1628cec2006-10-26 06:15:43 +00001075}
1076
1077// Write the operand with a cast to another type based on the Opcode being used.
1078// This will be used in cases where an instruction has specific type
1079// requirements (usually signedness) for its operands.
1080void CWriter::writeOperandWithCast(Value* Operand, unsigned Opcode) {
1081
1082 // Extract the operand's type, we'll need it.
1083 const Type* OpTy = Operand->getType();
1084
1085 // Indicate whether to do the cast or not.
1086 bool shouldCast = false;
1087
1088 // Based on the Opcode for which this Operand is being written, determine
1089 // the new type to which the operand should be casted by setting the value
1090 // of OpTy. If we change OpTy, also set shouldCast to true.
1091 switch (Opcode) {
1092 default:
1093 // for most instructions, it doesn't matter
1094 break;
Reid Spencer3822ff52006-11-08 06:47:33 +00001095 case Instruction::LShr:
Reid Spencer1628cec2006-10-26 06:15:43 +00001096 case Instruction::UDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +00001097 case Instruction::URem:
Reid Spencer1628cec2006-10-26 06:15:43 +00001098 // For UDiv to have unsigned operands
1099 if (OpTy->isSigned()) {
1100 OpTy = OpTy->getUnsignedVersion();
1101 shouldCast = true;
1102 }
1103 break;
Reid Spencer3822ff52006-11-08 06:47:33 +00001104 case Instruction::AShr:
Reid Spencer1628cec2006-10-26 06:15:43 +00001105 case Instruction::SDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +00001106 case Instruction::SRem:
Reid Spencer1628cec2006-10-26 06:15:43 +00001107 if (OpTy->isUnsigned()) {
1108 OpTy = OpTy->getSignedVersion();
1109 shouldCast = true;
1110 }
1111 break;
1112 }
1113
1114 // Write out the casted operand if we should, otherwise just write the
1115 // operand.
1116 if (shouldCast) {
1117 Out << "((";
1118 printType(Out, OpTy);
1119 Out << ")";
1120 writeOperand(Operand);
1121 Out << ")";
1122 } else
1123 writeOperand(Operand);
1124
1125}
1126
Chris Lattner41488192003-06-28 17:15:12 +00001127// generateCompilerSpecificCode - This is where we add conditional compilation
1128// directives to cater to specific compilers as need be.
1129//
Chris Lattnerc59c1182003-11-16 22:06:14 +00001130static void generateCompilerSpecificCode(std::ostream& Out) {
Chris Lattner9a571ba2006-03-07 22:58:23 +00001131 // Alloca is hard to get, and we don't want to include stdlib.h here.
Chris Lattner41488192003-06-28 17:15:12 +00001132 Out << "/* get a declaration for alloca */\n"
Chris Lattnerd9477602006-06-02 18:54:01 +00001133 << "#if defined(__CYGWIN__) || defined(__MINGW32__)\n"
Reid Spencera8411a62005-01-23 04:32:47 +00001134 << "extern void *_alloca(unsigned long);\n"
1135 << "#define alloca(x) _alloca(x)\n"
1136 << "#elif defined(__APPLE__)\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +00001137 << "extern void *__builtin_alloca(unsigned long);\n"
1138 << "#define alloca(x) __builtin_alloca(x)\n"
Anton Korobeynikovd27a2582006-12-10 23:12:42 +00001139 << "#define longjmp _longjmp\n"
1140 << "#define setjmp _setjmp\n"
Brian Gaekece630052004-12-10 05:44:45 +00001141 << "#elif defined(__sun__)\n"
1142 << "#if defined(__sparcv9)\n"
1143 << "extern void *__builtin_alloca(unsigned long);\n"
1144 << "#else\n"
1145 << "extern void *__builtin_alloca(unsigned int);\n"
1146 << "#endif\n"
1147 << "#define alloca(x) __builtin_alloca(x)\n"
Jeff Cohen3c280bf2006-04-17 17:55:41 +00001148 << "#elif defined(__FreeBSD__) || defined(__OpenBSD__)\n"
Chris Lattner60e66742004-10-06 04:21:52 +00001149 << "#define alloca(x) __builtin_alloca(x)\n"
Jeff Cohend01f65a2005-01-06 04:21:49 +00001150 << "#elif !defined(_MSC_VER)\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +00001151 << "#include <alloca.h>\n"
1152 << "#endif\n\n";
Chris Lattner41488192003-06-28 17:15:12 +00001153
1154 // We output GCC specific attributes to preserve 'linkonce'ness on globals.
1155 // If we aren't being compiled with GCC, just drop these attributes.
Chris Lattner9bda5f52003-06-28 17:53:05 +00001156 Out << "#ifndef __GNUC__ /* Can only support \"linkonce\" vars with GCC */\n"
Chris Lattner41488192003-06-28 17:15:12 +00001157 << "#define __attribute__(X)\n"
Brian Gaeke27f7a712003-12-11 00:24:36 +00001158 << "#endif\n\n";
1159
Brian Gaeke27f7a712003-12-11 00:24:36 +00001160 // On Mac OS X, "external weak" is spelled "__attribute__((weak_import))".
1161 Out << "#if defined(__GNUC__) && defined(__APPLE_CC__)\n"
1162 << "#define __EXTERNAL_WEAK__ __attribute__((weak_import))\n"
1163 << "#elif defined(__GNUC__)\n"
1164 << "#define __EXTERNAL_WEAK__ __attribute__((weak))\n"
1165 << "#else\n"
1166 << "#define __EXTERNAL_WEAK__\n"
1167 << "#endif\n\n";
Brian Gaeke27f7a712003-12-11 00:24:36 +00001168
1169 // For now, turn off the weak linkage attribute on Mac OS X. (See above.)
1170 Out << "#if defined(__GNUC__) && defined(__APPLE_CC__)\n"
1171 << "#define __ATTRIBUTE_WEAK__\n"
1172 << "#elif defined(__GNUC__)\n"
1173 << "#define __ATTRIBUTE_WEAK__ __attribute__((weak))\n"
1174 << "#else\n"
1175 << "#define __ATTRIBUTE_WEAK__\n"
1176 << "#endif\n\n";
Brian Gaeke043c0bb2004-07-21 03:15:26 +00001177
1178 // Define NaN and Inf as GCC builtins if using GCC, as 0 otherwise
1179 // From the GCC documentation:
Misha Brukmand6a29a52005-04-20 16:05:03 +00001180 //
Brian Gaeke043c0bb2004-07-21 03:15:26 +00001181 // double __builtin_nan (const char *str)
1182 //
1183 // This is an implementation of the ISO C99 function nan.
1184 //
1185 // Since ISO C99 defines this function in terms of strtod, which we do
1186 // not implement, a description of the parsing is in order. The string is
1187 // parsed as by strtol; that is, the base is recognized by leading 0 or
1188 // 0x prefixes. The number parsed is placed in the significand such that
1189 // the least significant bit of the number is at the least significant
1190 // bit of the significand. The number is truncated to fit the significand
1191 // field provided. The significand is forced to be a quiet NaN.
1192 //
1193 // This function, if given a string literal, is evaluated early enough
1194 // that it is considered a compile-time constant.
1195 //
1196 // float __builtin_nanf (const char *str)
1197 //
1198 // Similar to __builtin_nan, except the return type is float.
1199 //
1200 // double __builtin_inf (void)
1201 //
1202 // Similar to __builtin_huge_val, except a warning is generated if the
1203 // target floating-point format does not support infinities. This
1204 // function is suitable for implementing the ISO C99 macro INFINITY.
1205 //
1206 // float __builtin_inff (void)
1207 //
1208 // Similar to __builtin_inf, except the return type is float.
1209 Out << "#ifdef __GNUC__\n"
Brian Gaeke8a702e82004-08-25 19:00:42 +00001210 << "#define LLVM_NAN(NanStr) __builtin_nan(NanStr) /* Double */\n"
1211 << "#define LLVM_NANF(NanStr) __builtin_nanf(NanStr) /* Float */\n"
1212 << "#define LLVM_NANS(NanStr) __builtin_nans(NanStr) /* Double */\n"
1213 << "#define LLVM_NANSF(NanStr) __builtin_nansf(NanStr) /* Float */\n"
1214 << "#define LLVM_INF __builtin_inf() /* Double */\n"
1215 << "#define LLVM_INFF __builtin_inff() /* Float */\n"
Chris Lattnerf9a05322006-02-13 22:22:42 +00001216 << "#define LLVM_PREFETCH(addr,rw,locality) "
1217 "__builtin_prefetch(addr,rw,locality)\n"
Chris Lattner9a571ba2006-03-07 22:58:23 +00001218 << "#define __ATTRIBUTE_CTOR__ __attribute__((constructor))\n"
1219 << "#define __ATTRIBUTE_DTOR__ __attribute__((destructor))\n"
Chris Lattnerdacbe7b2006-07-28 20:58:47 +00001220 << "#define LLVM_ASM __asm__\n"
Brian Gaeke043c0bb2004-07-21 03:15:26 +00001221 << "#else\n"
Brian Gaeke8a702e82004-08-25 19:00:42 +00001222 << "#define LLVM_NAN(NanStr) ((double)0.0) /* Double */\n"
1223 << "#define LLVM_NANF(NanStr) 0.0F /* Float */\n"
1224 << "#define LLVM_NANS(NanStr) ((double)0.0) /* Double */\n"
1225 << "#define LLVM_NANSF(NanStr) 0.0F /* Float */\n"
1226 << "#define LLVM_INF ((double)0.0) /* Double */\n"
1227 << "#define LLVM_INFF 0.0F /* Float */\n"
Chris Lattner50a8a172005-05-06 06:58:42 +00001228 << "#define LLVM_PREFETCH(addr,rw,locality) /* PREFETCH */\n"
Chris Lattner9a571ba2006-03-07 22:58:23 +00001229 << "#define __ATTRIBUTE_CTOR__\n"
1230 << "#define __ATTRIBUTE_DTOR__\n"
Chris Lattnerdacbe7b2006-07-28 20:58:47 +00001231 << "#define LLVM_ASM(X)\n"
Chris Lattner50a8a172005-05-06 06:58:42 +00001232 << "#endif\n\n";
1233
1234 // Output target-specific code that should be inserted into main.
1235 Out << "#define CODE_FOR_MAIN() /* Any target-specific code for main()*/\n";
1236 // On X86, set the FP control word to 64-bits of precision instead of 80 bits.
1237 Out << "#if defined(__GNUC__) && !defined(__llvm__)\n"
Evan Chenge3db4da2006-06-20 22:11:12 +00001238 << "#if defined(i386) || defined(__i386__) || defined(__i386) || "
1239 << "defined(__x86_64__)\n"
Chris Lattner50a8a172005-05-06 06:58:42 +00001240 << "#undef CODE_FOR_MAIN\n"
1241 << "#define CODE_FOR_MAIN() \\\n"
1242 << " {short F;__asm__ (\"fnstcw %0\" : \"=m\" (*&F)); \\\n"
1243 << " F=(F&~0x300)|0x200;__asm__(\"fldcw %0\"::\"m\"(*&F));}\n"
1244 << "#endif\n#endif\n";
1245
Joel Stanleyecd78ac2003-02-12 20:45:00 +00001246}
1247
Chris Lattner9a571ba2006-03-07 22:58:23 +00001248/// FindStaticTors - Given a static ctor/dtor list, unpack its contents into
1249/// the StaticTors set.
1250static void FindStaticTors(GlobalVariable *GV, std::set<Function*> &StaticTors){
1251 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
1252 if (!InitList) return;
1253
1254 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
1255 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
1256 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
1257
1258 if (CS->getOperand(1)->isNullValue())
1259 return; // Found a null terminator, exit printing.
1260 Constant *FP = CS->getOperand(1);
1261 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
Reid Spencer3da59db2006-11-27 01:05:10 +00001262 if (CE->isCast())
Chris Lattner9a571ba2006-03-07 22:58:23 +00001263 FP = CE->getOperand(0);
1264 if (Function *F = dyn_cast<Function>(FP))
1265 StaticTors.insert(F);
1266 }
1267}
1268
1269enum SpecialGlobalClass {
1270 NotSpecial = 0,
1271 GlobalCtors, GlobalDtors,
1272 NotPrinted
1273};
1274
1275/// getGlobalVariableClass - If this is a global that is specially recognized
1276/// by LLVM, return a code that indicates how we should handle it.
1277static SpecialGlobalClass getGlobalVariableClass(const GlobalVariable *GV) {
1278 // If this is a global ctors/dtors list, handle it now.
1279 if (GV->hasAppendingLinkage() && GV->use_empty()) {
1280 if (GV->getName() == "llvm.global_ctors")
1281 return GlobalCtors;
1282 else if (GV->getName() == "llvm.global_dtors")
1283 return GlobalDtors;
1284 }
1285
1286 // Otherwise, it it is other metadata, don't print it. This catches things
1287 // like debug information.
1288 if (GV->getSection() == "llvm.metadata")
1289 return NotPrinted;
1290
1291 return NotSpecial;
1292}
1293
1294
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001295bool CWriter::doInitialization(Module &M) {
1296 // Initialize
1297 TheModule = &M;
Chris Lattnerc2421432004-05-09 04:30:20 +00001298
1299 IL.AddPrototypes(M);
Misha Brukmand6a29a52005-04-20 16:05:03 +00001300
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001301 // Ensure that all structure types have names...
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001302 Mang = new Mangler(M);
Chris Lattnerba9c6432005-11-10 21:39:29 +00001303 Mang->markCharUnacceptable('.');
Chris Lattnerc59c1182003-11-16 22:06:14 +00001304
Chris Lattner9a571ba2006-03-07 22:58:23 +00001305 // Keep track of which functions are static ctors/dtors so they can have
1306 // an attribute added to their prototypes.
1307 std::set<Function*> StaticCtors, StaticDtors;
1308 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
1309 I != E; ++I) {
1310 switch (getGlobalVariableClass(I)) {
1311 default: break;
1312 case GlobalCtors:
1313 FindStaticTors(I, StaticCtors);
1314 break;
1315 case GlobalDtors:
1316 FindStaticTors(I, StaticDtors);
1317 break;
1318 }
1319 }
1320
Chris Lattner16c7bb22002-05-09 02:28:59 +00001321 // get declaration for alloca
Joel Stanleyecd78ac2003-02-12 20:45:00 +00001322 Out << "/* Provide Declarations */\n";
Chris Lattnerc59c1182003-11-16 22:06:14 +00001323 Out << "#include <stdarg.h>\n"; // Varargs support
1324 Out << "#include <setjmp.h>\n"; // Unwind support
Chris Lattner41488192003-06-28 17:15:12 +00001325 generateCompilerSpecificCode(Out);
Chris Lattnerc59c1182003-11-16 22:06:14 +00001326
Chris Lattnercf135cb2003-06-02 03:10:53 +00001327 // Provide a definition for `bool' if not compiling with a C++ compiler.
1328 Out << "\n"
Vikram S. Adve969c4ad2002-08-25 20:00:08 +00001329 << "#ifndef __cplusplus\ntypedef unsigned char bool;\n#endif\n"
Misha Brukmand6a29a52005-04-20 16:05:03 +00001330
Chris Lattnerd1cf1b42002-09-20 23:26:33 +00001331 << "\n\n/* Support for floating point constants */\n"
1332 << "typedef unsigned long long ConstantDoubleTy;\n"
Chris Lattnereb6f8c72002-11-07 19:43:59 +00001333 << "typedef unsigned int ConstantFloatTy;\n"
Misha Brukmand6a29a52005-04-20 16:05:03 +00001334
Chris Lattnera4d4a852002-08-19 21:48:40 +00001335 << "\n\n/* Global Declarations */\n";
1336
1337 // First output all the declarations for the program, because C requires
1338 // Functions & globals to be declared before they are used.
1339 //
Chris Lattner16c7bb22002-05-09 02:28:59 +00001340
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001341 // Loop over the symbol table, emitting all named constants...
Chris Lattner68758072004-05-09 06:20:51 +00001342 printModuleTypes(M.getSymbolTable());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001343
Chris Lattnera4d4a852002-08-19 21:48:40 +00001344 // Global variable declarations...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001345 if (!M.global_empty()) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +00001346 Out << "\n/* External Global Variable Declarations */\n";
Chris Lattnerf9a05322006-02-13 22:22:42 +00001347 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
1348 I != E; ++I) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +00001349 if (I->hasExternalLinkage()) {
1350 Out << "extern ";
Brian Gaeked9fb37a2003-07-24 20:20:44 +00001351 printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
Vikram S. Adve913bfbc2002-09-17 11:50:38 +00001352 Out << ";\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001353 } else if (I->hasDLLImportLinkage()) {
1354 Out << "__declspec(dllimport) ";
1355 printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
1356 Out << ";\n";
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +00001357 } else if (I->hasExternalWeakLinkage()) {
1358 Out << "extern ";
1359 printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
1360 Out << " __EXTERNAL_WEAK__ ;\n";
1361 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +00001362 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +00001363 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001364
Chris Lattnera4d4a852002-08-19 21:48:40 +00001365 // Function declarations
Chris Lattner9a571ba2006-03-07 22:58:23 +00001366 Out << "\n/* Function Declarations */\n";
Chris Lattner57da2522005-08-23 20:22:50 +00001367 Out << "double fmod(double, double);\n"; // Support for FP rem
1368 Out << "float fmodf(float, float);\n";
1369
Chris Lattner9a571ba2006-03-07 22:58:23 +00001370 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1371 // Don't print declarations for intrinsic functions.
Reid Spencer2cb46e12006-10-22 09:58:21 +00001372 if (!I->getIntrinsicID() && I->getName() != "setjmp" &&
1373 I->getName() != "longjmp" && I->getName() != "_setjmp") {
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +00001374 if (I->hasExternalWeakLinkage())
1375 Out << "extern ";
Chris Lattner9a571ba2006-03-07 22:58:23 +00001376 printFunctionSignature(I, true);
1377 if (I->hasWeakLinkage() || I->hasLinkOnceLinkage())
1378 Out << " __ATTRIBUTE_WEAK__";
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +00001379 if (I->hasExternalWeakLinkage())
1380 Out << " __EXTERNAL_WEAK__";
Chris Lattner9a571ba2006-03-07 22:58:23 +00001381 if (StaticCtors.count(I))
1382 Out << " __ATTRIBUTE_CTOR__";
1383 if (StaticDtors.count(I))
1384 Out << " __ATTRIBUTE_DTOR__";
Chris Lattnerdacbe7b2006-07-28 20:58:47 +00001385
1386 if (I->hasName() && I->getName()[0] == 1)
1387 Out << " LLVM_ASM(\"" << I->getName().c_str()+1 << "\")";
1388
Chris Lattner9a571ba2006-03-07 22:58:23 +00001389 Out << ";\n";
Chris Lattner4cda8352002-08-20 16:55:48 +00001390 }
Chris Lattnera4d4a852002-08-19 21:48:40 +00001391 }
1392
Joel Stanley54f60322003-06-25 04:52:09 +00001393 // Output the global variable declarations
Chris Lattnere4d5c442005-03-15 04:54:21 +00001394 if (!M.global_empty()) {
Joel Stanley54f60322003-06-25 04:52:09 +00001395 Out << "\n\n/* Global Variable Declarations */\n";
Chris Lattnerf9a05322006-02-13 22:22:42 +00001396 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
1397 I != E; ++I)
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +00001398 if (!I->isExternal()) {
Chris Lattner9a571ba2006-03-07 22:58:23 +00001399 // Ignore special globals, such as debug info.
1400 if (getGlobalVariableClass(I))
1401 continue;
1402
Chris Lattnercc16a8e2004-12-03 17:19:10 +00001403 if (I->hasInternalLinkage())
1404 Out << "static ";
1405 else
1406 Out << "extern ";
Brian Gaeked9fb37a2003-07-24 20:20:44 +00001407 printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
Chris Lattner2580d4f2003-11-03 17:32:38 +00001408
1409 if (I->hasLinkOnceLinkage())
1410 Out << " __attribute__((common))";
1411 else if (I->hasWeakLinkage())
Brian Gaeke27f7a712003-12-11 00:24:36 +00001412 Out << " __ATTRIBUTE_WEAK__";
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +00001413 else if (I->hasExternalWeakLinkage())
1414 Out << " __EXTERNAL_WEAK__";
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +00001415 Out << ";\n";
1416 }
1417 }
1418
Vikram S. Adve913bfbc2002-09-17 11:50:38 +00001419 // Output the global variable definitions and contents...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001420 if (!M.global_empty()) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +00001421 Out << "\n\n/* Global Variable Definitions and Initialization */\n";
Chris Lattnerf9a05322006-02-13 22:22:42 +00001422 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
1423 I != E; ++I)
Chris Lattnere8e035b2002-10-16 20:08:47 +00001424 if (!I->isExternal()) {
Chris Lattner9a571ba2006-03-07 22:58:23 +00001425 // Ignore special globals, such as debug info.
1426 if (getGlobalVariableClass(I))
1427 continue;
1428
Chris Lattnere8e035b2002-10-16 20:08:47 +00001429 if (I->hasInternalLinkage())
1430 Out << "static ";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001431 else if (I->hasDLLImportLinkage())
1432 Out << "__declspec(dllimport) ";
1433 else if (I->hasDLLExportLinkage())
1434 Out << "__declspec(dllexport) ";
1435
Brian Gaeked9fb37a2003-07-24 20:20:44 +00001436 printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
Chris Lattner76d9f1b2003-06-28 17:08:36 +00001437 if (I->hasLinkOnceLinkage())
1438 Out << " __attribute__((common))";
Chris Lattner72ac148d2003-10-16 18:29:00 +00001439 else if (I->hasWeakLinkage())
Brian Gaeke27f7a712003-12-11 00:24:36 +00001440 Out << " __ATTRIBUTE_WEAK__";
Chris Lattner5ea326a2003-11-03 17:35:00 +00001441
1442 // If the initializer is not null, emit the initializer. If it is null,
1443 // we try to avoid emitting large amounts of zeros. The problem with
1444 // this, however, occurs when the variable has weak linkage. In this
1445 // case, the assembler will complain about the variable being both weak
1446 // and common, so we disable this optimization.
Chris Lattnerf358c5a2004-02-20 05:49:22 +00001447 if (!I->getInitializer()->isNullValue()) {
Chris Lattner940b08d2003-06-06 07:10:24 +00001448 Out << " = " ;
1449 writeOperand(I->getInitializer());
Chris Lattnerf358c5a2004-02-20 05:49:22 +00001450 } else if (I->hasWeakLinkage()) {
1451 // We have to specify an initializer, but it doesn't have to be
1452 // complete. If the value is an aggregate, print out { 0 }, and let
1453 // the compiler figure out the rest of the zeros.
1454 Out << " = " ;
1455 if (isa<StructType>(I->getInitializer()->getType()) ||
Robert Bocchinob78e8382006-01-20 20:43:57 +00001456 isa<ArrayType>(I->getInitializer()->getType()) ||
1457 isa<PackedType>(I->getInitializer()->getType())) {
Chris Lattnerf358c5a2004-02-20 05:49:22 +00001458 Out << "{ 0 }";
1459 } else {
1460 // Just print it out normally.
1461 writeOperand(I->getInitializer());
1462 }
Chris Lattner940b08d2003-06-06 07:10:24 +00001463 }
Chris Lattnere8e035b2002-10-16 20:08:47 +00001464 Out << ";\n";
Chris Lattnera4d4a852002-08-19 21:48:40 +00001465 }
Chris Lattnera4d4a852002-08-19 21:48:40 +00001466 }
1467
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001468 if (!M.empty())
Chris Lattnera4d4a852002-08-19 21:48:40 +00001469 Out << "\n\n/* Function Bodies */\n";
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001470 return false;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001471}
1472
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001473
Chris Lattner9860e772003-10-12 04:36:29 +00001474/// Output all floating point constants that cannot be printed accurately...
Chris Lattner68758072004-05-09 06:20:51 +00001475void CWriter::printFloatingPointConstants(Function &F) {
Chris Lattner9860e772003-10-12 04:36:29 +00001476 // Scan the module for floating point constants. If any FP constant is used
1477 // in the function, we want to redirect it here so that we do not depend on
1478 // the precision of the printed form, unless the printed form preserves
1479 // precision.
1480 //
Chris Lattner68758072004-05-09 06:20:51 +00001481 static unsigned FPCounter = 0;
1482 for (constant_iterator I = constant_begin(&F), E = constant_end(&F);
1483 I != E; ++I)
1484 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(*I))
1485 if (!isFPCSafeToPrint(FPC) && // Do not put in FPConstantMap if safe.
1486 !FPConstantMap.count(FPC)) {
1487 double Val = FPC->getValue();
Misha Brukmand6a29a52005-04-20 16:05:03 +00001488
Chris Lattner68758072004-05-09 06:20:51 +00001489 FPConstantMap[FPC] = FPCounter; // Number the FP constants
Misha Brukmand6a29a52005-04-20 16:05:03 +00001490
Chris Lattner68758072004-05-09 06:20:51 +00001491 if (FPC->getType() == Type::DoubleTy) {
Chris Lattner68758072004-05-09 06:20:51 +00001492 Out << "static const ConstantDoubleTy FPConstant" << FPCounter++
Jim Laskeycb6682f2005-08-17 19:34:49 +00001493 << " = 0x" << std::hex << DoubleToBits(Val) << std::dec
Chris Lattner68758072004-05-09 06:20:51 +00001494 << "ULL; /* " << Val << " */\n";
1495 } else if (FPC->getType() == Type::FloatTy) {
Chris Lattner68758072004-05-09 06:20:51 +00001496 Out << "static const ConstantFloatTy FPConstant" << FPCounter++
Jim Laskeycb6682f2005-08-17 19:34:49 +00001497 << " = 0x" << std::hex << FloatToBits(Val) << std::dec
Chris Lattner68758072004-05-09 06:20:51 +00001498 << "U; /* " << Val << " */\n";
1499 } else
1500 assert(0 && "Unknown float type!");
1501 }
Misha Brukmand6a29a52005-04-20 16:05:03 +00001502
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001503 Out << '\n';
Chris Lattner68758072004-05-09 06:20:51 +00001504}
Chris Lattner9860e772003-10-12 04:36:29 +00001505
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001506
Chris Lattner2db41cd2002-09-20 15:12:13 +00001507/// printSymbolTable - Run through symbol table looking for type names. If a
Robert Bocchinob78e8382006-01-20 20:43:57 +00001508/// type name is found, emit its declaration...
Chris Lattnera4c047e2002-09-20 14:56:54 +00001509///
Chris Lattner68758072004-05-09 06:20:51 +00001510void CWriter::printModuleTypes(const SymbolTable &ST) {
Reid Spencer555a0b12006-12-11 20:39:15 +00001511 Out << "/* Helper union for bitcasts */\n";
1512 Out << "typedef union {\n";
Reid Spencer22b36fb2006-12-12 00:11:08 +00001513 Out << " unsigned int UInt;\n";
1514 Out << " signed int SInt;\n";
1515 Out << " unsigned long long ULong;\n";
1516 Out << " signed long long SLong;\n";
1517 Out << " float Float;\n";
1518 Out << " double Double;\n";
Reid Spencer555a0b12006-12-11 20:39:15 +00001519 Out << "} llvmBitCastUnion;\n";
1520
Chris Lattnerb15fde22005-03-06 02:28:23 +00001521 // We are only interested in the type plane of the symbol table.
Reid Spencer9231ac82004-05-25 08:53:40 +00001522 SymbolTable::type_const_iterator I = ST.type_begin();
1523 SymbolTable::type_const_iterator End = ST.type_end();
Chris Lattnerb15fde22005-03-06 02:28:23 +00001524
1525 // If there are no type names, exit early.
1526 if (I == End) return;
Misha Brukmand6a29a52005-04-20 16:05:03 +00001527
Chris Lattner58d04d42002-09-20 15:18:30 +00001528 // Print out forward declarations for structure types before anything else!
1529 Out << "/* Structure forward decls */\n";
1530 for (; I != End; ++I)
Chris Lattner68758072004-05-09 06:20:51 +00001531 if (const Type *STy = dyn_cast<StructType>(I->second)) {
Chris Lattner36c975c2005-11-10 18:53:25 +00001532 std::string Name = "struct l_" + Mang->makeNameProper(I->first);
Chris Lattner68758072004-05-09 06:20:51 +00001533 Out << Name << ";\n";
1534 TypeNames.insert(std::make_pair(STy, Name));
1535 }
Chris Lattner58d04d42002-09-20 15:18:30 +00001536
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001537 Out << '\n';
Chris Lattner58d04d42002-09-20 15:18:30 +00001538
1539 // Now we can print out typedefs...
1540 Out << "/* Typedefs */\n";
Reid Spencer9231ac82004-05-25 08:53:40 +00001541 for (I = ST.type_begin(); I != End; ++I) {
Chris Lattner68758072004-05-09 06:20:51 +00001542 const Type *Ty = cast<Type>(I->second);
Chris Lattner36c975c2005-11-10 18:53:25 +00001543 std::string Name = "l_" + Mang->makeNameProper(I->first);
Chris Lattner68758072004-05-09 06:20:51 +00001544 Out << "typedef ";
1545 printType(Out, Ty, Name);
1546 Out << ";\n";
1547 }
Misha Brukmand6a29a52005-04-20 16:05:03 +00001548
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001549 Out << '\n';
Chris Lattner3ef6dc72002-05-09 14:40:11 +00001550
Chris Lattner2c601a72002-09-20 15:05:40 +00001551 // Keep track of which structures have been printed so far...
1552 std::set<const StructType *> StructPrinted;
1553
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +00001554 // Loop over all structures then push them into the stack so they are
1555 // printed in the correct order.
Chris Lattner2db41cd2002-09-20 15:12:13 +00001556 //
Chris Lattner58d04d42002-09-20 15:18:30 +00001557 Out << "/* Structure contents */\n";
Reid Spencer9231ac82004-05-25 08:53:40 +00001558 for (I = ST.type_begin(); I != End; ++I)
Chris Lattner2db41cd2002-09-20 15:12:13 +00001559 if (const StructType *STy = dyn_cast<StructType>(I->second))
Chris Lattnerfa395ec2003-11-02 01:29:27 +00001560 // Only print out used types!
Chris Lattner68758072004-05-09 06:20:51 +00001561 printContainedStructs(STy, StructPrinted);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001562}
1563
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +00001564// Push the struct onto the stack and recursively push all structs
1565// this one depends on.
Robert Bocchinob78e8382006-01-20 20:43:57 +00001566//
1567// TODO: Make this work properly with packed types
1568//
Chris Lattner2c601a72002-09-20 15:05:40 +00001569void CWriter::printContainedStructs(const Type *Ty,
1570 std::set<const StructType*> &StructPrinted){
Chris Lattner14d9b202006-01-20 18:57:03 +00001571 // Don't walk through pointers.
1572 if (isa<PointerType>(Ty) || Ty->isPrimitiveType()) return;
1573
1574 // Print all contained types first.
1575 for (Type::subtype_iterator I = Ty->subtype_begin(),
1576 E = Ty->subtype_end(); I != E; ++I)
1577 printContainedStructs(*I, StructPrinted);
1578
Misha Brukmanac25de32003-07-09 17:33:50 +00001579 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattner14d9b202006-01-20 18:57:03 +00001580 // Check to see if we have already printed this struct.
1581 if (StructPrinted.insert(STy).second) {
1582 // Print structure type out.
Misha Brukmand6a29a52005-04-20 16:05:03 +00001583 std::string Name = TypeNames[STy];
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001584 printType(Out, STy, Name, true);
Chris Lattner58d04d42002-09-20 15:18:30 +00001585 Out << ";\n\n";
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +00001586 }
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +00001587 }
1588}
1589
Chris Lattner4cda8352002-08-20 16:55:48 +00001590void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
Chris Lattner0c54d892006-05-23 23:39:48 +00001591 /// isCStructReturn - Should this function actually return a struct by-value?
1592 bool isCStructReturn = F->getCallingConv() == CallingConv::CSRet;
1593
Joel Stanley54f60322003-06-25 04:52:09 +00001594 if (F->hasInternalLinkage()) Out << "static ";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001595 if (F->hasDLLImportLinkage()) Out << "__declspec(dllimport) ";
1596 if (F->hasDLLExportLinkage()) Out << "__declspec(dllexport) ";
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001597 switch (F->getCallingConv()) {
1598 case CallingConv::X86_StdCall:
1599 Out << "__stdcall ";
1600 break;
1601 case CallingConv::X86_FastCall:
1602 Out << "__fastcall ";
1603 break;
1604 }
1605
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001606 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +00001607 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Misha Brukmand6a29a52005-04-20 16:05:03 +00001608
1609 std::stringstream FunctionInnards;
1610
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001611 // Print out the name...
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001612 FunctionInnards << Mang->getValueName(F) << '(';
Misha Brukmand6a29a52005-04-20 16:05:03 +00001613
Chris Lattner0c54d892006-05-23 23:39:48 +00001614 bool PrintedArg = false;
Chris Lattner16c7bb22002-05-09 02:28:59 +00001615 if (!F->isExternal()) {
Chris Lattnere4d5c442005-03-15 04:54:21 +00001616 if (!F->arg_empty()) {
Chris Lattner0c54d892006-05-23 23:39:48 +00001617 Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1618
1619 // If this is a struct-return function, don't print the hidden
1620 // struct-return argument.
1621 if (isCStructReturn) {
1622 assert(I != E && "Invalid struct return function!");
1623 ++I;
1624 }
1625
Chris Lattneredd8ce12003-05-03 03:14:35 +00001626 std::string ArgName;
Chris Lattner0c54d892006-05-23 23:39:48 +00001627 for (; I != E; ++I) {
1628 if (PrintedArg) FunctionInnards << ", ";
Chris Lattner4cda8352002-08-20 16:55:48 +00001629 if (I->hasName() || !Prototype)
Brian Gaeked9fb37a2003-07-24 20:20:44 +00001630 ArgName = Mang->getValueName(I);
Misha Brukmand6a29a52005-04-20 16:05:03 +00001631 else
Chris Lattner4cda8352002-08-20 16:55:48 +00001632 ArgName = "";
Joel Stanley54f60322003-06-25 04:52:09 +00001633 printType(FunctionInnards, I->getType(), ArgName);
Chris Lattner0c54d892006-05-23 23:39:48 +00001634 PrintedArg = true;
Chris Lattnerdeed7a52002-05-09 15:18:52 +00001635 }
1636 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001637 } else {
Chris Lattner0c54d892006-05-23 23:39:48 +00001638 // Loop over the arguments, printing them.
1639 FunctionType::param_iterator I = FT->param_begin(), E = FT->param_end();
1640
1641 // If this is a struct-return function, don't print the hidden
1642 // struct-return argument.
1643 if (isCStructReturn) {
1644 assert(I != E && "Invalid struct return function!");
1645 ++I;
1646 }
1647
1648 for (; I != E; ++I) {
1649 if (PrintedArg) FunctionInnards << ", ";
Joel Stanley54f60322003-06-25 04:52:09 +00001650 printType(FunctionInnards, *I);
Chris Lattner0c54d892006-05-23 23:39:48 +00001651 PrintedArg = true;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001652 }
1653 }
1654
Chris Lattner1f8c4a12002-09-20 22:32:30 +00001655 // Finish printing arguments... if this is a vararg function, print the ...,
1656 // unless there are no known types, in which case, we just emit ().
1657 //
Chris Lattner0c54d892006-05-23 23:39:48 +00001658 if (FT->isVarArg() && PrintedArg) {
1659 if (PrintedArg) FunctionInnards << ", ";
Joel Stanley54f60322003-06-25 04:52:09 +00001660 FunctionInnards << "..."; // Output varargs portion of signature!
Chris Lattner0c54d892006-05-23 23:39:48 +00001661 } else if (!FT->isVarArg() && !PrintedArg) {
Chris Lattner4f7e1732003-11-26 00:09:17 +00001662 FunctionInnards << "void"; // ret() -> ret(void) in C.
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001663 }
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001664 FunctionInnards << ')';
Chris Lattner0c54d892006-05-23 23:39:48 +00001665
1666 // Get the return tpe for the function.
1667 const Type *RetTy;
1668 if (!isCStructReturn)
1669 RetTy = F->getReturnType();
1670 else {
1671 // If this is a struct-return function, print the struct-return type.
1672 RetTy = cast<PointerType>(FT->getParamType(0))->getElementType();
1673 }
1674
1675 // Print out the return type and the signature built above.
1676 printType(Out, RetTy, FunctionInnards.str());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001677}
1678
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001679void CWriter::printFunction(Function &F) {
1680 printFunctionSignature(&F, false);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +00001681 Out << " {\n";
Chris Lattner0c54d892006-05-23 23:39:48 +00001682
1683 // If this is a struct return function, handle the result with magic.
1684 if (F.getCallingConv() == CallingConv::CSRet) {
1685 const Type *StructTy =
1686 cast<PointerType>(F.arg_begin()->getType())->getElementType();
1687 Out << " ";
1688 printType(Out, StructTy, "StructReturn");
1689 Out << "; /* Struct return temporary */\n";
Chris Lattner8c3c4bf2002-05-09 15:49:41 +00001690
Chris Lattner0c54d892006-05-23 23:39:48 +00001691 Out << " ";
1692 printType(Out, F.arg_begin()->getType(), Mang->getValueName(F.arg_begin()));
1693 Out << " = &StructReturn;\n";
1694 }
1695
1696 bool PrintedVar = false;
1697
Chris Lattner497e19a2002-05-09 20:39:03 +00001698 // print local variable information for the function
Reid Spencer941cfda2006-12-17 18:50:51 +00001699 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) {
Chris Lattner6ffe5512004-04-27 15:13:33 +00001700 if (const AllocaInst *AI = isDirectAlloca(&*I)) {
Chris Lattnera8ab89e2003-06-17 04:39:14 +00001701 Out << " ";
Brian Gaeked9fb37a2003-07-24 20:20:44 +00001702 printType(Out, AI->getAllocatedType(), Mang->getValueName(AI));
Misha Brukman41ce39c2005-01-31 06:19:57 +00001703 Out << "; /* Address-exposed local */\n";
Chris Lattner0c54d892006-05-23 23:39:48 +00001704 PrintedVar = true;
Chris Lattner6ffe5512004-04-27 15:13:33 +00001705 } else if (I->getType() != Type::VoidTy && !isInlinableInst(*I)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +00001706 Out << " ";
Chris Lattner6ffe5512004-04-27 15:13:33 +00001707 printType(Out, I->getType(), Mang->getValueName(&*I));
Chris Lattner16c7bb22002-05-09 02:28:59 +00001708 Out << ";\n";
Misha Brukmand6a29a52005-04-20 16:05:03 +00001709
Chris Lattner84e66652003-05-03 07:11:00 +00001710 if (isa<PHINode>(*I)) { // Print out PHI node temporaries as well...
1711 Out << " ";
Chris Lattner6ffe5512004-04-27 15:13:33 +00001712 printType(Out, I->getType(),
1713 Mang->getValueName(&*I)+"__PHI_TEMPORARY");
Chris Lattner84e66652003-05-03 07:11:00 +00001714 Out << ";\n";
1715 }
Chris Lattner0c54d892006-05-23 23:39:48 +00001716 PrintedVar = true;
Reid Spencer941cfda2006-12-17 18:50:51 +00001717 }
1718 // We need a temporary for the BitCast to use so it can pluck a value out
1719 // of a uniont to do the BitCast. This is separate from the need for a
1720 // variable to hold the result of the BitCast.
1721 if (isa<BitCastInst>(*I) &&
1722 ((I->getType()->isFloatingPoint() &&
1723 I->getOperand(0)->getType()->isInteger()) ||
1724 (I->getType()->isInteger() &&
1725 I->getOperand(0)->getType()->isFloatingPoint()))) {
Reid Spencer555a0b12006-12-11 20:39:15 +00001726 Out << " llvmBitCastUnion " << Mang->getValueName(&*I)
1727 << "__BITCAST_TEMPORARY;\n";
Reid Spencer941cfda2006-12-17 18:50:51 +00001728 PrintedVar = true;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001729 }
Reid Spencer941cfda2006-12-17 18:50:51 +00001730 }
Chris Lattnerd1cf1b42002-09-20 23:26:33 +00001731
Chris Lattner0c54d892006-05-23 23:39:48 +00001732 if (PrintedVar)
1733 Out << '\n';
Chris Lattnerd1cf1b42002-09-20 23:26:33 +00001734
Chris Lattner395fd592004-12-13 21:52:52 +00001735 if (F.hasExternalLinkage() && F.getName() == "main")
Chris Lattner50a8a172005-05-06 06:58:42 +00001736 Out << " CODE_FOR_MAIN();\n";
Chris Lattner395fd592004-12-13 21:52:52 +00001737
Chris Lattner16c7bb22002-05-09 02:28:59 +00001738 // print the basic blocks
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001739 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattnercb3ad012004-05-09 20:41:32 +00001740 if (Loop *L = LI->getLoopFor(BB)) {
1741 if (L->getHeader() == BB && L->getParentLoop() == 0)
1742 printLoop(L);
1743 } else {
1744 printBasicBlock(BB);
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001745 }
Chris Lattner8c3c4bf2002-05-09 15:49:41 +00001746 }
Misha Brukmand6a29a52005-04-20 16:05:03 +00001747
Chris Lattner8c3c4bf2002-05-09 15:49:41 +00001748 Out << "}\n\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001749}
1750
Chris Lattnercb3ad012004-05-09 20:41:32 +00001751void CWriter::printLoop(Loop *L) {
1752 Out << " do { /* Syntactic loop '" << L->getHeader()->getName()
1753 << "' to make GCC happy */\n";
1754 for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i) {
1755 BasicBlock *BB = L->getBlocks()[i];
1756 Loop *BBLoop = LI->getLoopFor(BB);
1757 if (BBLoop == L)
1758 printBasicBlock(BB);
1759 else if (BB == BBLoop->getHeader() && BBLoop->getParentLoop() == L)
Misha Brukmand6a29a52005-04-20 16:05:03 +00001760 printLoop(BBLoop);
Chris Lattnercb3ad012004-05-09 20:41:32 +00001761 }
1762 Out << " } while (1); /* end of syntactic loop '"
1763 << L->getHeader()->getName() << "' */\n";
1764}
1765
1766void CWriter::printBasicBlock(BasicBlock *BB) {
1767
1768 // Don't print the label for the basic block if there are no uses, or if
1769 // the only terminator use is the predecessor basic block's terminator.
1770 // We have to scan the use list because PHI nodes use basic blocks too but
1771 // do not require a label to be generated.
1772 //
1773 bool NeedsLabel = false;
1774 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
1775 if (isGotoCodeNecessary(*PI, BB)) {
1776 NeedsLabel = true;
1777 break;
1778 }
Misha Brukmand6a29a52005-04-20 16:05:03 +00001779
Chris Lattnercb3ad012004-05-09 20:41:32 +00001780 if (NeedsLabel) Out << Mang->getValueName(BB) << ":\n";
Misha Brukmand6a29a52005-04-20 16:05:03 +00001781
Chris Lattnercb3ad012004-05-09 20:41:32 +00001782 // Output all of the instructions in the basic block...
1783 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E;
1784 ++II) {
1785 if (!isInlinableInst(*II) && !isDirectAlloca(II)) {
Andrew Lenharth29c277f2006-11-27 23:50:49 +00001786 if (II->getType() != Type::VoidTy && !isInlineAsm(*II))
Chris Lattnercb3ad012004-05-09 20:41:32 +00001787 outputLValue(II);
1788 else
1789 Out << " ";
1790 visit(*II);
1791 Out << ";\n";
1792 }
1793 }
Misha Brukmand6a29a52005-04-20 16:05:03 +00001794
Chris Lattnercb3ad012004-05-09 20:41:32 +00001795 // Don't emit prefix or suffix for the terminator...
1796 visit(*BB->getTerminator());
1797}
1798
1799
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001800// Specific Instruction type classes... note that all of the casts are
Misha Brukman5560c9d2003-08-18 14:43:39 +00001801// necessary because we use the instruction classes as opaque types...
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001802//
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001803void CWriter::visitReturnInst(ReturnInst &I) {
Chris Lattner0c54d892006-05-23 23:39:48 +00001804 // If this is a struct return function, return the temporary struct.
1805 if (I.getParent()->getParent()->getCallingConv() == CallingConv::CSRet) {
1806 Out << " return StructReturn;\n";
1807 return;
1808 }
1809
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001810 // Don't output a void return if this is the last basic block in the function
Misha Brukmand6a29a52005-04-20 16:05:03 +00001811 if (I.getNumOperands() == 0 &&
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001812 &*--I.getParent()->getParent()->end() == I.getParent() &&
1813 !I.getParent()->size() == 1) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001814 return;
Chris Lattner963b70b2002-05-21 18:05:19 +00001815 }
Chris Lattner44408262002-05-09 03:50:42 +00001816
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001817 Out << " return";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001818 if (I.getNumOperands()) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001819 Out << ' ';
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001820 writeOperand(I.getOperand(0));
Chris Lattner16c7bb22002-05-09 02:28:59 +00001821 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001822 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001823}
1824
Chris Lattnera9f5e052003-04-22 20:19:52 +00001825void CWriter::visitSwitchInst(SwitchInst &SI) {
Chris Lattnera05e0ec2004-05-09 03:42:48 +00001826
Chris Lattnera9f5e052003-04-22 20:19:52 +00001827 Out << " switch (";
1828 writeOperand(SI.getOperand(0));
Chris Lattnerf1acd962003-04-23 19:15:13 +00001829 Out << ") {\n default:\n";
John Criswell30cc2272004-10-25 18:30:09 +00001830 printPHICopiesForSuccessor (SI.getParent(), SI.getDefaultDest(), 2);
Chris Lattnerf1acd962003-04-23 19:15:13 +00001831 printBranchToBlock(SI.getParent(), SI.getDefaultDest(), 2);
Chris Lattnera9f5e052003-04-22 20:19:52 +00001832 Out << ";\n";
1833 for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) {
1834 Out << " case ";
1835 writeOperand(SI.getOperand(i));
1836 Out << ":\n";
1837 BasicBlock *Succ = cast<BasicBlock>(SI.getOperand(i+1));
John Criswell30cc2272004-10-25 18:30:09 +00001838 printPHICopiesForSuccessor (SI.getParent(), Succ, 2);
Chris Lattnera9f5e052003-04-22 20:19:52 +00001839 printBranchToBlock(SI.getParent(), Succ, 2);
Chris Lattnerefd02c72005-03-19 17:35:11 +00001840 if (Function::iterator(Succ) == next(Function::iterator(SI.getParent())))
Chris Lattnera9f5e052003-04-22 20:19:52 +00001841 Out << " break;\n";
1842 }
1843 Out << " }\n";
1844}
1845
Chris Lattnera9d12c02004-10-16 18:12:13 +00001846void CWriter::visitUnreachableInst(UnreachableInst &I) {
Chris Lattner963869e2004-10-17 23:49:11 +00001847 Out << " /*UNREACHABLE*/;\n";
Chris Lattnera9d12c02004-10-16 18:12:13 +00001848}
1849
Chris Lattnercb3ad012004-05-09 20:41:32 +00001850bool CWriter::isGotoCodeNecessary(BasicBlock *From, BasicBlock *To) {
1851 /// FIXME: This should be reenabled, but loop reordering safe!!
1852 return true;
1853
Chris Lattner67c2d182005-03-18 16:12:37 +00001854 if (next(Function::iterator(From)) != Function::iterator(To))
1855 return true; // Not the direct successor, we need a goto.
Chris Lattnercb3ad012004-05-09 20:41:32 +00001856
1857 //isa<SwitchInst>(From->getTerminator())
Chris Lattner9bda5f52003-06-28 17:53:05 +00001858
Chris Lattnercb3ad012004-05-09 20:41:32 +00001859 if (LI->getLoopFor(From) != LI->getLoopFor(To))
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001860 return true;
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001861 return false;
1862}
1863
John Criswell57bbfce2004-10-20 14:38:39 +00001864void CWriter::printPHICopiesForSuccessor (BasicBlock *CurBlock,
Misha Brukmand6a29a52005-04-20 16:05:03 +00001865 BasicBlock *Successor,
John Criswell57bbfce2004-10-20 14:38:39 +00001866 unsigned Indent) {
1867 for (BasicBlock::iterator I = Successor->begin(); isa<PHINode>(I); ++I) {
1868 PHINode *PN = cast<PHINode>(I);
1869 // Now we have to do the printing.
1870 Value *IV = PN->getIncomingValueForBlock(CurBlock);
1871 if (!isa<UndefValue>(IV)) {
1872 Out << std::string(Indent, ' ');
1873 Out << " " << Mang->getValueName(I) << "__PHI_TEMPORARY = ";
1874 writeOperand(IV);
1875 Out << "; /* for PHI node */\n";
1876 }
1877 }
1878}
1879
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001880void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
Chris Lattner9bda5f52003-06-28 17:53:05 +00001881 unsigned Indent) {
Chris Lattnercb3ad012004-05-09 20:41:32 +00001882 if (isGotoCodeNecessary(CurBB, Succ)) {
Chris Lattneredd8ce12003-05-03 03:14:35 +00001883 Out << std::string(Indent, ' ') << " goto ";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001884 writeOperand(Succ);
1885 Out << ";\n";
1886 }
1887}
1888
Misha Brukman37f92e22003-09-11 22:34:13 +00001889// Branch instruction printing - Avoid printing out a branch to a basic block
1890// that immediately succeeds the current one.
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001891//
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001892void CWriter::visitBranchInst(BranchInst &I) {
Chris Lattnera05e0ec2004-05-09 03:42:48 +00001893
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001894 if (I.isConditional()) {
Chris Lattner5ef6ffd2003-10-12 08:12:58 +00001895 if (isGotoCodeNecessary(I.getParent(), I.getSuccessor(0))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001896 Out << " if (";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001897 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001898 Out << ") {\n";
Misha Brukmand6a29a52005-04-20 16:05:03 +00001899
John Criswell57bbfce2004-10-20 14:38:39 +00001900 printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(0), 2);
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001901 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
Misha Brukmand6a29a52005-04-20 16:05:03 +00001902
Chris Lattner5ef6ffd2003-10-12 08:12:58 +00001903 if (isGotoCodeNecessary(I.getParent(), I.getSuccessor(1))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001904 Out << " } else {\n";
John Criswell57bbfce2004-10-20 14:38:39 +00001905 printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(1), 2);
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001906 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001907 }
1908 } else {
Misha Brukman5560c9d2003-08-18 14:43:39 +00001909 // First goto not necessary, assume second one is...
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001910 Out << " if (!";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001911 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001912 Out << ") {\n";
1913
John Criswell57bbfce2004-10-20 14:38:39 +00001914 printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(1), 2);
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001915 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001916 }
1917
1918 Out << " }\n";
1919 } else {
John Criswell57bbfce2004-10-20 14:38:39 +00001920 printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(0), 0);
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001921 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001922 }
1923 Out << "\n";
1924}
1925
Chris Lattner84e66652003-05-03 07:11:00 +00001926// PHI nodes get copied into temporary values at the end of predecessor basic
1927// blocks. We now need to copy these temporary values into the REAL value for
1928// the PHI.
1929void CWriter::visitPHINode(PHINode &I) {
1930 writeOperand(&I);
1931 Out << "__PHI_TEMPORARY";
1932}
1933
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001934
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001935void CWriter::visitBinaryOperator(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001936 // binary instructions, shift instructions, setCond instructions.
Chris Lattnerf5612b762003-04-23 19:09:22 +00001937 assert(!isa<PointerType>(I.getType()));
Brian Gaeke031a1122003-06-23 20:00:51 +00001938
1939 // We must cast the results of binary operations which might be promoted.
1940 bool needsCast = false;
1941 if ((I.getType() == Type::UByteTy) || (I.getType() == Type::SByteTy)
Brian Gaekee99f4cf2003-06-25 03:05:33 +00001942 || (I.getType() == Type::UShortTy) || (I.getType() == Type::ShortTy)
1943 || (I.getType() == Type::FloatTy)) {
Brian Gaeke031a1122003-06-23 20:00:51 +00001944 needsCast = true;
1945 Out << "((";
Chris Lattner7635ea42003-11-03 01:01:59 +00001946 printType(Out, I.getType());
Brian Gaeke031a1122003-06-23 20:00:51 +00001947 Out << ")(";
1948 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001949
Chris Lattner6d9b69f2005-03-03 21:12:04 +00001950 // If this is a negation operation, print it out as such. For FP, we don't
1951 // want to print "-0.0 - X".
1952 if (BinaryOperator::isNeg(&I)) {
John Criswellce4e1e42005-07-14 19:41:16 +00001953 Out << "-(";
Chris Lattner6d9b69f2005-03-03 21:12:04 +00001954 writeOperand(BinaryOperator::getNegArgument(cast<BinaryOperator>(&I)));
John Criswellce4e1e42005-07-14 19:41:16 +00001955 Out << ")";
Reid Spencer0a783f72006-11-02 01:53:59 +00001956 } else if (I.getOpcode() == Instruction::FRem) {
Chris Lattner57da2522005-08-23 20:22:50 +00001957 // Output a call to fmod/fmodf instead of emitting a%b
1958 if (I.getType() == Type::FloatTy)
1959 Out << "fmodf(";
1960 else
1961 Out << "fmod(";
1962 writeOperand(I.getOperand(0));
1963 Out << ", ";
1964 writeOperand(I.getOperand(1));
1965 Out << ")";
Chris Lattner6d9b69f2005-03-03 21:12:04 +00001966 } else {
Reid Spencer1628cec2006-10-26 06:15:43 +00001967
1968 // Write out the cast of the instruction's value back to the proper type
1969 // if necessary.
1970 bool NeedsClosingParens = writeInstructionCast(I);
1971
1972 // Certain instructions require the operand to be forced to a specific type
1973 // so we use writeOperandWithCast here instead of writeOperand. Similarly
1974 // below for operand 1
1975 writeOperandWithCast(I.getOperand(0), I.getOpcode());
Chris Lattner6d9b69f2005-03-03 21:12:04 +00001976
1977 switch (I.getOpcode()) {
1978 case Instruction::Add: Out << " + "; break;
1979 case Instruction::Sub: Out << " - "; break;
1980 case Instruction::Mul: Out << '*'; break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001981 case Instruction::URem:
1982 case Instruction::SRem:
1983 case Instruction::FRem: Out << '%'; break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001984 case Instruction::UDiv:
1985 case Instruction::SDiv:
1986 case Instruction::FDiv: Out << '/'; break;
Chris Lattner6d9b69f2005-03-03 21:12:04 +00001987 case Instruction::And: Out << " & "; break;
1988 case Instruction::Or: Out << " | "; break;
1989 case Instruction::Xor: Out << " ^ "; break;
1990 case Instruction::SetEQ: Out << " == "; break;
1991 case Instruction::SetNE: Out << " != "; break;
1992 case Instruction::SetLE: Out << " <= "; break;
1993 case Instruction::SetGE: Out << " >= "; break;
1994 case Instruction::SetLT: Out << " < "; break;
1995 case Instruction::SetGT: Out << " > "; break;
1996 case Instruction::Shl : Out << " << "; break;
Reid Spencer3822ff52006-11-08 06:47:33 +00001997 case Instruction::LShr:
1998 case Instruction::AShr: Out << " >> "; break;
Bill Wendlingf5da1332006-12-07 22:21:48 +00001999 default: cerr << "Invalid operator type!" << I; abort();
Chris Lattner6d9b69f2005-03-03 21:12:04 +00002000 }
2001
Reid Spencer1628cec2006-10-26 06:15:43 +00002002 writeOperandWithCast(I.getOperand(1), I.getOpcode());
2003 if (NeedsClosingParens)
2004 Out << "))";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002005 }
2006
Brian Gaeke031a1122003-06-23 20:00:51 +00002007 if (needsCast) {
2008 Out << "))";
2009 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002010}
2011
Reid Spencer555a0b12006-12-11 20:39:15 +00002012static const char * getFloatBitCastField(const Type *Ty) {
2013 switch (Ty->getTypeID()) {
2014 default: assert(0 && "Invalid Type");
2015 case Type::FloatTyID: return "Float";
2016 case Type::UIntTyID: return "UInt";
2017 case Type::IntTyID: return "SInt";
2018 case Type::DoubleTyID:return "Double";
2019 case Type::ULongTyID: return "ULong";
2020 case Type::LongTyID: return "SLong";
2021 }
2022}
2023
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002024void CWriter::visitCastInst(CastInst &I) {
Reid Spencer3da59db2006-11-27 01:05:10 +00002025 const Type *DstTy = I.getType();
2026 const Type *SrcTy = I.getOperand(0)->getType();
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002027 Out << '(';
Reid Spencer555a0b12006-12-11 20:39:15 +00002028 if (isa<BitCastInst>(I) &&
2029 ((I.getType()->isFloatingPoint() &&
2030 I.getOperand(0)->getType()->isInteger()) ||
2031 (I.getType()->isInteger() &&
2032 I.getOperand(0)->getType()->isFloatingPoint()))) {
Reid Spencer941cfda2006-12-17 18:50:51 +00002033
Reid Spencer555a0b12006-12-11 20:39:15 +00002034 // These int<->float and long<->double casts need to be handled specially
2035 Out << Mang->getValueName(&I) << "__BITCAST_TEMPORARY."
2036 << getFloatBitCastField(I.getOperand(0)->getType()) << " = ";
2037 writeOperand(I.getOperand(0));
2038 Out << ", " << Mang->getValueName(&I) << "__BITCAST_TEMPORARY."
2039 << getFloatBitCastField(I.getType());
2040 } else {
2041 printCast(I.getOpcode(), SrcTy, DstTy);
2042 if (I.getOpcode() == Instruction::SExt && SrcTy == Type::BoolTy) {
2043 // Make sure we really get a sext from bool by subtracing the bool from 0
2044 Out << "0-";
2045 }
2046 writeOperand(I.getOperand(0));
2047 if (DstTy == Type::BoolTy &&
2048 (I.getOpcode() == Instruction::Trunc ||
2049 I.getOpcode() == Instruction::FPToUI ||
2050 I.getOpcode() == Instruction::FPToSI ||
2051 I.getOpcode() == Instruction::PtrToInt)) {
2052 // Make sure we really get a trunc to bool by anding the operand with 1
2053 Out << "&1u";
2054 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002055 }
2056 Out << ')';
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002057}
2058
Chris Lattner9f24a072004-03-12 05:52:14 +00002059void CWriter::visitSelectInst(SelectInst &I) {
2060 Out << "((";
2061 writeOperand(I.getCondition());
2062 Out << ") ? (";
2063 writeOperand(I.getTrueValue());
2064 Out << ") : (";
2065 writeOperand(I.getFalseValue());
Misha Brukmand6a29a52005-04-20 16:05:03 +00002066 Out << "))";
Chris Lattner9f24a072004-03-12 05:52:14 +00002067}
2068
2069
Chris Lattnerc2421432004-05-09 04:30:20 +00002070void CWriter::lowerIntrinsics(Function &F) {
2071 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
2072 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
2073 if (CallInst *CI = dyn_cast<CallInst>(I++))
2074 if (Function *F = CI->getCalledFunction())
2075 switch (F->getIntrinsicID()) {
2076 case Intrinsic::not_intrinsic:
2077 case Intrinsic::vastart:
2078 case Intrinsic::vacopy:
2079 case Intrinsic::vaend:
2080 case Intrinsic::returnaddress:
2081 case Intrinsic::frameaddress:
2082 case Intrinsic::setjmp:
2083 case Intrinsic::longjmp:
Chris Lattner33e9d292005-02-28 19:29:46 +00002084 case Intrinsic::prefetch:
Jim Laskey7075d6f2006-03-08 19:31:15 +00002085 case Intrinsic::dbg_stoppoint:
Chris Lattner1e142892006-09-09 06:17:12 +00002086 case Intrinsic::powi_f32:
2087 case Intrinsic::powi_f64:
Chris Lattnerc2421432004-05-09 04:30:20 +00002088 // We directly implement these intrinsics
2089 break;
2090 default:
Chris Lattner87242152006-03-13 23:09:05 +00002091 // If this is an intrinsic that directly corresponds to a GCC
2092 // builtin, we handle it.
2093 const char *BuiltinName = "";
2094#define GET_GCC_BUILTIN_NAME
2095#include "llvm/Intrinsics.gen"
2096#undef GET_GCC_BUILTIN_NAME
2097 // If we handle it, don't lower it.
2098 if (BuiltinName[0]) break;
2099
Chris Lattnerc2421432004-05-09 04:30:20 +00002100 // All other intrinsic calls we must lower.
Chris Lattner67c2d182005-03-18 16:12:37 +00002101 Instruction *Before = 0;
Misha Brukmand6a29a52005-04-20 16:05:03 +00002102 if (CI != &BB->front())
Chris Lattner67c2d182005-03-18 16:12:37 +00002103 Before = prior(BasicBlock::iterator(CI));
Misha Brukmand6a29a52005-04-20 16:05:03 +00002104
Chris Lattnerc2421432004-05-09 04:30:20 +00002105 IL.LowerIntrinsicCall(CI);
2106 if (Before) { // Move iterator to instruction after call
2107 I = Before; ++I;
2108 } else {
2109 I = BB->begin();
Chris Lattner4ef51372004-02-14 00:31:10 +00002110 }
Chris Lattner87242152006-03-13 23:09:05 +00002111 break;
Chris Lattnerc2421432004-05-09 04:30:20 +00002112 }
Chris Lattner4ef51372004-02-14 00:31:10 +00002113}
2114
2115
2116
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002117void CWriter::visitCallInst(CallInst &I) {
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002118 //check if we have inline asm
2119 if (isInlineAsm(I)) {
2120 visitInlineAsm(I);
2121 return;
2122 }
2123
Chris Lattner87242152006-03-13 23:09:05 +00002124 bool WroteCallee = false;
2125
Chris Lattner18ac3c82003-05-08 18:41:45 +00002126 // Handle intrinsic function calls first...
2127 if (Function *F = I.getCalledFunction())
Brian Gaeked0fde302003-11-11 22:41:34 +00002128 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
Chris Lattner18ac3c82003-05-08 18:41:45 +00002129 switch (ID) {
Chris Lattner87242152006-03-13 23:09:05 +00002130 default: {
2131 // If this is an intrinsic that directly corresponds to a GCC
2132 // builtin, we emit it here.
2133 const char *BuiltinName = "";
2134#define GET_GCC_BUILTIN_NAME
2135#include "llvm/Intrinsics.gen"
2136#undef GET_GCC_BUILTIN_NAME
2137 assert(BuiltinName[0] && "Unknown LLVM intrinsic!");
2138
2139 Out << BuiltinName;
2140 WroteCallee = true;
2141 break;
2142 }
Misha Brukmand6a29a52005-04-20 16:05:03 +00002143 case Intrinsic::vastart:
Chris Lattner4d45bd02003-10-18 05:57:43 +00002144 Out << "0; ";
Misha Brukmand6a29a52005-04-20 16:05:03 +00002145
Andrew Lenharth558bc882005-06-18 18:34:52 +00002146 Out << "va_start(*(va_list*)";
2147 writeOperand(I.getOperand(1));
2148 Out << ", ";
Chris Lattner18ac3c82003-05-08 18:41:45 +00002149 // Output the last argument to the enclosing function...
Chris Lattnere4d5c442005-03-15 04:54:21 +00002150 if (I.getParent()->getParent()->arg_empty()) {
Bill Wendlingf5da1332006-12-07 22:21:48 +00002151 cerr << "The C backend does not currently support zero "
2152 << "argument varargs functions, such as '"
2153 << I.getParent()->getParent()->getName() << "'!\n";
Chris Lattner4065ef92003-10-23 18:39:22 +00002154 abort();
2155 }
Chris Lattnerc5e7df12005-03-15 04:59:17 +00002156 writeOperand(--I.getParent()->getParent()->arg_end());
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002157 Out << ')';
Chris Lattner18ac3c82003-05-08 18:41:45 +00002158 return;
Chris Lattner317201d2004-03-13 00:24:00 +00002159 case Intrinsic::vaend:
Chris Lattnerf8e6f4f2004-08-10 00:19:16 +00002160 if (!isa<ConstantPointerNull>(I.getOperand(1))) {
Andrew Lenharth558bc882005-06-18 18:34:52 +00002161 Out << "0; va_end(*(va_list*)";
Chris Lattnerf8e6f4f2004-08-10 00:19:16 +00002162 writeOperand(I.getOperand(1));
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002163 Out << ')';
Chris Lattnerf8e6f4f2004-08-10 00:19:16 +00002164 } else {
2165 Out << "va_end(*(va_list*)0)";
2166 }
Chris Lattner18ac3c82003-05-08 18:41:45 +00002167 return;
Chris Lattner317201d2004-03-13 00:24:00 +00002168 case Intrinsic::vacopy:
Andrew Lenharth558bc882005-06-18 18:34:52 +00002169 Out << "0; ";
2170 Out << "va_copy(*(va_list*)";
Chris Lattner18ac3c82003-05-08 18:41:45 +00002171 writeOperand(I.getOperand(1));
Andrew Lenharth213e5572005-06-22 21:04:42 +00002172 Out << ", *(va_list*)";
Andrew Lenharth558bc882005-06-18 18:34:52 +00002173 writeOperand(I.getOperand(2));
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002174 Out << ')';
Chris Lattner18ac3c82003-05-08 18:41:45 +00002175 return;
Chris Lattnerfe1554a2004-02-14 02:55:36 +00002176 case Intrinsic::returnaddress:
2177 Out << "__builtin_return_address(";
2178 writeOperand(I.getOperand(1));
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002179 Out << ')';
Chris Lattnerfe1554a2004-02-14 02:55:36 +00002180 return;
2181 case Intrinsic::frameaddress:
2182 Out << "__builtin_frame_address(";
2183 writeOperand(I.getOperand(1));
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002184 Out << ')';
Chris Lattnerfe1554a2004-02-14 02:55:36 +00002185 return;
Chris Lattner1e142892006-09-09 06:17:12 +00002186 case Intrinsic::powi_f32:
2187 case Intrinsic::powi_f64:
2188 Out << "__builtin_powi(";
2189 writeOperand(I.getOperand(1));
2190 Out << ", ";
2191 writeOperand(I.getOperand(2));
2192 Out << ')';
2193 return;
Chris Lattnere42cde22004-02-15 22:51:47 +00002194 case Intrinsic::setjmp:
2195 Out << "setjmp(*(jmp_buf*)";
2196 writeOperand(I.getOperand(1));
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002197 Out << ')';
Chris Lattnere42cde22004-02-15 22:51:47 +00002198 return;
2199 case Intrinsic::longjmp:
2200 Out << "longjmp(*(jmp_buf*)";
2201 writeOperand(I.getOperand(1));
2202 Out << ", ";
2203 writeOperand(I.getOperand(2));
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002204 Out << ')';
Chris Lattnere42cde22004-02-15 22:51:47 +00002205 return;
Chris Lattner33e9d292005-02-28 19:29:46 +00002206 case Intrinsic::prefetch:
Chris Lattnerca76f352005-02-28 19:36:15 +00002207 Out << "LLVM_PREFETCH((const void *)";
Chris Lattner33e9d292005-02-28 19:29:46 +00002208 writeOperand(I.getOperand(1));
2209 Out << ", ";
2210 writeOperand(I.getOperand(2));
2211 Out << ", ";
2212 writeOperand(I.getOperand(3));
2213 Out << ")";
Chris Lattnerca76f352005-02-28 19:36:15 +00002214 return;
Jim Laskey7075d6f2006-03-08 19:31:15 +00002215 case Intrinsic::dbg_stoppoint: {
2216 // If we use writeOperand directly we get a "u" suffix which is rejected
2217 // by gcc.
Jim Laskey580418e2006-03-23 18:08:29 +00002218 DbgStopPointInst &SPI = cast<DbgStopPointInst>(I);
Jim Laskey7075d6f2006-03-08 19:31:15 +00002219
2220 Out << "\n#line "
Jim Laskey580418e2006-03-23 18:08:29 +00002221 << SPI.getLine()
2222 << " \"" << SPI.getDirectory()
2223 << SPI.getFileName() << "\"\n";
Jim Laskey7075d6f2006-03-08 19:31:15 +00002224 return;
2225 }
Chris Lattner18ac3c82003-05-08 18:41:45 +00002226 }
2227 }
2228
Chris Lattner4394d512004-11-13 22:21:56 +00002229 Value *Callee = I.getCalledValue();
Misha Brukmand6a29a52005-04-20 16:05:03 +00002230
Chris Lattner0c54d892006-05-23 23:39:48 +00002231 // If this is a call to a struct-return function, assign to the first
2232 // parameter instead of passing it to the call.
2233 bool isStructRet = I.getCallingConv() == CallingConv::CSRet;
2234 if (isStructRet) {
2235 Out << "*(";
2236 writeOperand(I.getOperand(1));
2237 Out << ") = ";
2238 }
2239
Chris Lattnerfe673d92005-05-06 06:53:07 +00002240 if (I.isTailCall()) Out << " /*tail*/ ";
Chris Lattner4394d512004-11-13 22:21:56 +00002241
2242 const PointerType *PTy = cast<PointerType>(Callee->getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002243 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
Chris Lattner0c54d892006-05-23 23:39:48 +00002244
2245 if (!WroteCallee) {
2246 // If this is an indirect call to a struct return function, we need to cast
2247 // the pointer.
2248 bool NeedsCast = isStructRet && !isa<Function>(Callee);
Misha Brukmand6a29a52005-04-20 16:05:03 +00002249
Chris Lattner0c54d892006-05-23 23:39:48 +00002250 // GCC is a real PITA. It does not permit codegening casts of functions to
2251 // function pointers if they are in a call (it generates a trap instruction
2252 // instead!). We work around this by inserting a cast to void* in between
2253 // the function and the function pointer cast. Unfortunately, we can't just
2254 // form the constant expression here, because the folder will immediately
2255 // nuke it.
2256 //
2257 // Note finally, that this is completely unsafe. ANSI C does not guarantee
2258 // that void* and function pointers have the same size. :( To deal with this
2259 // in the common case, we handle casts where the number of arguments passed
2260 // match exactly.
2261 //
2262 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Callee))
Reid Spencer3da59db2006-11-27 01:05:10 +00002263 if (CE->isCast())
Chris Lattner0c54d892006-05-23 23:39:48 +00002264 if (Function *RF = dyn_cast<Function>(CE->getOperand(0))) {
2265 NeedsCast = true;
2266 Callee = RF;
2267 }
2268
2269 if (NeedsCast) {
2270 // Ok, just cast the pointer type.
2271 Out << "((";
2272 if (!isStructRet)
2273 printType(Out, I.getCalledValue()->getType());
2274 else
2275 printStructReturnPointerFunctionType(Out,
2276 cast<PointerType>(I.getCalledValue()->getType()));
2277 Out << ")(void*)";
2278 }
2279 writeOperand(Callee);
2280 if (NeedsCast) Out << ')';
2281 }
2282
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002283 Out << '(';
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002284
Chris Lattner4394d512004-11-13 22:21:56 +00002285 unsigned NumDeclaredParams = FTy->getNumParams();
2286
Chris Lattner0c54d892006-05-23 23:39:48 +00002287 CallSite::arg_iterator AI = I.op_begin()+1, AE = I.op_end();
2288 unsigned ArgNo = 0;
2289 if (isStructRet) { // Skip struct return argument.
2290 ++AI;
2291 ++ArgNo;
2292 }
2293
2294 bool PrintedArg = false;
2295 for (; AI != AE; ++AI, ++ArgNo) {
2296 if (PrintedArg) Out << ", ";
2297 if (ArgNo < NumDeclaredParams &&
2298 (*AI)->getType() != FTy->getParamType(ArgNo)) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002299 Out << '(';
Chris Lattner0c54d892006-05-23 23:39:48 +00002300 printType(Out, FTy->getParamType(ArgNo));
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002301 Out << ')';
Chris Lattner4394d512004-11-13 22:21:56 +00002302 }
Chris Lattner9bda5f52003-06-28 17:53:05 +00002303 writeOperand(*AI);
Chris Lattner0c54d892006-05-23 23:39:48 +00002304 PrintedArg = true;
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002305 }
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002306 Out << ')';
Misha Brukmand6a29a52005-04-20 16:05:03 +00002307}
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002308
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002309
2310//This converts the llvm constraint string to something gcc is expecting.
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002311//TODO: work out platform independent constraints and factor those out
Andrew Lenharth238581f2006-11-28 19:56:02 +00002312// of the per target tables
2313// handle multiple constraint codes
Andrew Lenharthe0cf0752006-11-28 19:53:36 +00002314std::string CWriter::InterpretASMConstraint(InlineAsm::ConstraintInfo& c) {
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002315
2316 assert(c.Codes.size() == 1 && "Too many asm constraint codes to handle");
2317
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002318 const char** table = 0;
Andrew Lenharthe0cf0752006-11-28 19:53:36 +00002319
2320 //Grab the translation table from TargetAsmInfo if it exists
2321 if (!TAsm) {
2322 std::string E;
2323 const TargetMachineRegistry::Entry* Match =
2324 TargetMachineRegistry::getClosestStaticTargetForModule(*TheModule, E);
2325 if (Match) {
2326 //Per platform Target Machines don't exist, so create it
2327 // this must be done only once
2328 const TargetMachine* TM = Match->CtorFn(*TheModule, "");
2329 TAsm = TM->getTargetAsmInfo();
2330 }
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002331 }
Andrew Lenharthe0cf0752006-11-28 19:53:36 +00002332 if (TAsm)
2333 table = TAsm->getAsmCBE();
2334
2335 //Search the translation table if it exists
2336 for (int i = 0; table && table[i]; i += 2)
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002337 if (c.Codes[0] == table[i])
2338 return table[i+1];
2339
Andrew Lenharth85f22282006-11-28 22:25:32 +00002340 //default is identity
2341 return c.Codes[0];
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002342}
2343
2344//TODO: import logic from AsmPrinter.cpp
Andrew Lenharthe0cf0752006-11-28 19:53:36 +00002345static std::string gccifyAsm(std::string asmstr) {
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002346 for (std::string::size_type i = 0; i != asmstr.size(); ++i)
2347 if (asmstr[i] == '\n')
2348 asmstr.replace(i, 1, "\\n");
2349 else if (asmstr[i] == '\t')
2350 asmstr.replace(i, 1, "\\t");
2351 else if (asmstr[i] == '$') {
2352 if (asmstr[i + 1] == '{') {
2353 std::string::size_type a = asmstr.find_first_of(':', i + 1);
2354 std::string::size_type b = asmstr.find_first_of('}', i + 1);
2355 std::string n = "%" +
2356 asmstr.substr(a + 1, b - a - 1) +
2357 asmstr.substr(i + 2, a - i - 2);
2358 asmstr.replace(i, b - i + 1, n);
2359 i += n.size() - 1;
2360 } else
2361 asmstr.replace(i, 1, "%");
2362 }
2363 else if (asmstr[i] == '%')//grr
2364 { asmstr.replace(i, 1, "%%"); ++i;}
2365
2366 return asmstr;
2367}
2368
Andrew Lenharth238581f2006-11-28 19:56:02 +00002369//TODO: assumptions about what consume arguments from the call are likely wrong
2370// handle communitivity
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002371void CWriter::visitInlineAsm(CallInst &CI) {
2372 InlineAsm* as = cast<InlineAsm>(CI.getOperand(0));
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002373 std::vector<InlineAsm::ConstraintInfo> Constraints = as->ParseConstraints();
2374 std::vector<std::pair<std::string, Value*> > Input;
2375 std::vector<std::pair<std::string, Value*> > Output;
2376 std::string Clobber;
2377 int count = CI.getType() == Type::VoidTy ? 1 : 0;
2378 for (std::vector<InlineAsm::ConstraintInfo>::iterator I = Constraints.begin(),
2379 E = Constraints.end(); I != E; ++I) {
2380 assert(I->Codes.size() == 1 && "Too many asm constraint codes to handle");
2381 std::string c =
Andrew Lenharthe0cf0752006-11-28 19:53:36 +00002382 InterpretASMConstraint(*I);
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002383 switch(I->Type) {
2384 default:
2385 assert(0 && "Unknown asm constraint");
2386 break;
2387 case InlineAsm::isInput: {
2388 if (c.size()) {
2389 Input.push_back(std::make_pair(c, count ? CI.getOperand(count) : &CI));
2390 ++count; //consume arg
2391 }
2392 break;
2393 }
2394 case InlineAsm::isOutput: {
2395 if (c.size()) {
2396 Output.push_back(std::make_pair("="+((I->isEarlyClobber ? "&" : "")+c),
2397 count ? CI.getOperand(count) : &CI));
2398 ++count; //consume arg
2399 }
2400 break;
2401 }
2402 case InlineAsm::isClobber: {
2403 if (c.size())
2404 Clobber += ",\"" + c + "\"";
2405 break;
2406 }
2407 }
2408 }
2409
2410 //fix up the asm string for gcc
Andrew Lenharthe0cf0752006-11-28 19:53:36 +00002411 std::string asmstr = gccifyAsm(as->getAsmString());
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002412
2413 Out << "__asm__ volatile (\"" << asmstr << "\"\n";
2414 Out << " :";
2415 for (std::vector<std::pair<std::string, Value*> >::iterator I = Output.begin(),
2416 E = Output.end(); I != E; ++I) {
2417 Out << "\"" << I->first << "\"(";
2418 writeOperandRaw(I->second);
2419 Out << ")";
2420 if (I + 1 != E)
2421 Out << ",";
2422 }
2423 Out << "\n :";
2424 for (std::vector<std::pair<std::string, Value*> >::iterator I = Input.begin(),
2425 E = Input.end(); I != E; ++I) {
2426 Out << "\"" << I->first << "\"(";
2427 writeOperandRaw(I->second);
2428 Out << ")";
2429 if (I + 1 != E)
2430 Out << ",";
2431 }
Andrew Lenharthf45148e2006-11-28 23:07:32 +00002432 if (Clobber.size())
2433 Out << "\n :" << Clobber.substr(1);
2434 Out << ")";
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002435}
2436
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002437void CWriter::visitMallocInst(MallocInst &I) {
Chris Lattnerf31182a2004-02-13 23:18:48 +00002438 assert(0 && "lowerallocations pass didn't work!");
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002439}
2440
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002441void CWriter::visitAllocaInst(AllocaInst &I) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002442 Out << '(';
Nick Hildenbrandt98502372002-11-18 20:55:50 +00002443 printType(Out, I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002444 Out << ") alloca(sizeof(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00002445 printType(Out, I.getType()->getElementType());
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002446 Out << ')';
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002447 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002448 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002449 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002450 }
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002451 Out << ')';
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002452}
2453
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002454void CWriter::visitFreeInst(FreeInst &I) {
Chris Lattnerf31182a2004-02-13 23:18:48 +00002455 assert(0 && "lowerallocations pass didn't work!");
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002456}
2457
Chris Lattner23e90702003-11-25 20:49:55 +00002458void CWriter::printIndexingExpression(Value *Ptr, gep_type_iterator I,
2459 gep_type_iterator E) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00002460 bool HasImplicitAddress = false;
2461 // If accessing a global value with no indexing, avoid *(&GV) syndrome
Reid Spencer3ed469c2006-11-02 20:25:50 +00002462 if (isa<GlobalValue>(Ptr)) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00002463 HasImplicitAddress = true;
Chris Lattnera8ab89e2003-06-17 04:39:14 +00002464 } else if (isDirectAlloca(Ptr)) {
2465 HasImplicitAddress = true;
Chris Lattnerd0c668c2002-05-09 21:18:38 +00002466 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002467
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00002468 if (I == E) {
2469 if (!HasImplicitAddress)
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002470 Out << '*'; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002471
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00002472 writeOperandInternal(Ptr);
2473 return;
2474 }
2475
Chris Lattner23e90702003-11-25 20:49:55 +00002476 const Constant *CI = dyn_cast<Constant>(I.getOperand());
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00002477 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
2478 Out << "(&";
2479
2480 writeOperandInternal(Ptr);
2481
Nick Hildenbrandte548f002002-09-25 20:29:26 +00002482 if (HasImplicitAddress && (!CI || !CI->isNullValue())) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002483 Out << ')';
Nick Hildenbrandte548f002002-09-25 20:29:26 +00002484 HasImplicitAddress = false; // HIA is only true if we haven't addressed yet
2485 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002486
Nick Hildenbrandte548f002002-09-25 20:29:26 +00002487 assert(!HasImplicitAddress || (CI && CI->isNullValue()) &&
2488 "Can only have implicit address with direct accessing");
2489
2490 if (HasImplicitAddress) {
2491 ++I;
Chris Lattner23e90702003-11-25 20:49:55 +00002492 } else if (CI && CI->isNullValue()) {
2493 gep_type_iterator TmpI = I; ++TmpI;
2494
Nick Hildenbrandte548f002002-09-25 20:29:26 +00002495 // Print out the -> operator if possible...
Chris Lattner23e90702003-11-25 20:49:55 +00002496 if (TmpI != E && isa<StructType>(*TmpI)) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00002497 Out << (HasImplicitAddress ? "." : "->");
Reid Spencerb83eb642006-10-20 07:07:24 +00002498 Out << "field" << cast<ConstantInt>(TmpI.getOperand())->getZExtValue();
Chris Lattner23e90702003-11-25 20:49:55 +00002499 I = ++TmpI;
2500 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002501 }
Chris Lattner5dfe7672002-08-22 22:48:55 +00002502
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002503 for (; I != E; ++I)
Chris Lattner23e90702003-11-25 20:49:55 +00002504 if (isa<StructType>(*I)) {
Reid Spencerb83eb642006-10-20 07:07:24 +00002505 Out << ".field" << cast<ConstantInt>(I.getOperand())->getZExtValue();
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002506 } else {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002507 Out << '[';
Chris Lattner23e90702003-11-25 20:49:55 +00002508 writeOperand(I.getOperand());
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002509 Out << ']';
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002510 }
2511}
2512
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002513void CWriter::visitLoadInst(LoadInst &I) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002514 Out << '*';
Chris Lattner9d30e222005-02-14 16:47:52 +00002515 if (I.isVolatile()) {
Chris Lattner8399e022005-02-15 05:52:14 +00002516 Out << "((";
Chris Lattnerb94388a2005-09-27 20:52:44 +00002517 printType(Out, I.getType(), "volatile*");
2518 Out << ")";
Chris Lattner9d30e222005-02-14 16:47:52 +00002519 }
2520
Chris Lattner5dfe7672002-08-22 22:48:55 +00002521 writeOperand(I.getOperand(0));
Chris Lattner9d30e222005-02-14 16:47:52 +00002522
2523 if (I.isVolatile())
Misha Brukman23ba0c52005-03-08 00:26:08 +00002524 Out << ')';
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002525}
2526
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002527void CWriter::visitStoreInst(StoreInst &I) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002528 Out << '*';
Chris Lattner9d30e222005-02-14 16:47:52 +00002529 if (I.isVolatile()) {
Chris Lattner8399e022005-02-15 05:52:14 +00002530 Out << "((";
Chris Lattnerb94388a2005-09-27 20:52:44 +00002531 printType(Out, I.getOperand(0)->getType(), " volatile*");
2532 Out << ")";
Chris Lattner9d30e222005-02-14 16:47:52 +00002533 }
Chris Lattner5dfe7672002-08-22 22:48:55 +00002534 writeOperand(I.getPointerOperand());
Misha Brukman23ba0c52005-03-08 00:26:08 +00002535 if (I.isVolatile()) Out << ')';
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002536 Out << " = ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002537 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002538}
2539
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002540void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002541 Out << '&';
Chris Lattner23e90702003-11-25 20:49:55 +00002542 printIndexingExpression(I.getPointerOperand(), gep_type_begin(I),
2543 gep_type_end(I));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002544}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00002545
Chris Lattner4d45bd02003-10-18 05:57:43 +00002546void CWriter::visitVAArgInst(VAArgInst &I) {
Andrew Lenharth558bc882005-06-18 18:34:52 +00002547 Out << "va_arg(*(va_list*)";
Chris Lattner4d45bd02003-10-18 05:57:43 +00002548 writeOperand(I.getOperand(0));
Andrew Lenharth558bc882005-06-18 18:34:52 +00002549 Out << ", ";
Chris Lattner7635ea42003-11-03 01:01:59 +00002550 printType(Out, I.getType());
Andrew Lenharth558bc882005-06-18 18:34:52 +00002551 Out << ");\n ";
Chris Lattner4d45bd02003-10-18 05:57:43 +00002552}
2553
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00002554//===----------------------------------------------------------------------===//
2555// External Interface declaration
2556//===----------------------------------------------------------------------===//
2557
Chris Lattner1911fd42006-09-04 04:14:57 +00002558bool CTargetMachine::addPassesToEmitWholeFile(PassManager &PM,
2559 std::ostream &o,
2560 CodeGenFileType FileType,
2561 bool Fast) {
Chris Lattner0431c962005-06-25 02:48:37 +00002562 if (FileType != TargetMachine::AssemblyFile) return true;
2563
Chris Lattner99c59e82004-05-23 21:23:35 +00002564 PM.add(createLowerGCPass());
Chris Lattnerf7fe4942005-03-03 01:04:50 +00002565 PM.add(createLowerAllocationsPass(true));
Chris Lattner94f4f8e2004-02-13 23:00:29 +00002566 PM.add(createLowerInvokePass());
Chris Lattnerbad13eb2005-11-02 17:42:58 +00002567 PM.add(createCFGSimplificationPass()); // clean up after lower invoke.
Chris Lattnerf9a05322006-02-13 22:22:42 +00002568 PM.add(new CBackendNameAllUsedStructsAndMergeFunctions());
Chris Lattnerbc641b92006-03-23 05:43:16 +00002569 PM.add(new CWriter(o));
Chris Lattnerf31182a2004-02-13 23:18:48 +00002570 return false;
2571}