blob: 96fc53948b32a33d5d3c4c3977d0a1128392e6b7 [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;
299 GV->replaceAllUsesWith(ConstantExpr::getCast(OldGV, GV->getType()));
300 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;
315 GV->replaceAllUsesWith(ConstantExpr::getCast(OldGV, GV->getType()));
316 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
Chris Lattner94f4f8e2004-02-13 23:00:29 +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 Spencer555a0b12006-12-11 20:39:15 +00001717 } else if (isa<BitCastInst>(*I) &&
1718 ((I->getType()->isFloatingPoint() &&
1719 I->getOperand(0)->getType()->isInteger()) ||
1720 (I->getType()->isInteger() &&
1721 I->getOperand(0)->getType()->isFloatingPoint()))) {
1722 // We need a temporary for the BitCast to use so it can pluck a
1723 // value out of a union to do the BitCast.
1724 Out << " llvmBitCastUnion " << Mang->getValueName(&*I)
1725 << "__BITCAST_TEMPORARY;\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001726 }
Chris Lattnerd1cf1b42002-09-20 23:26:33 +00001727
Chris Lattner0c54d892006-05-23 23:39:48 +00001728 if (PrintedVar)
1729 Out << '\n';
Chris Lattnerd1cf1b42002-09-20 23:26:33 +00001730
Chris Lattner395fd592004-12-13 21:52:52 +00001731 if (F.hasExternalLinkage() && F.getName() == "main")
Chris Lattner50a8a172005-05-06 06:58:42 +00001732 Out << " CODE_FOR_MAIN();\n";
Chris Lattner395fd592004-12-13 21:52:52 +00001733
Chris Lattner16c7bb22002-05-09 02:28:59 +00001734 // print the basic blocks
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001735 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattnercb3ad012004-05-09 20:41:32 +00001736 if (Loop *L = LI->getLoopFor(BB)) {
1737 if (L->getHeader() == BB && L->getParentLoop() == 0)
1738 printLoop(L);
1739 } else {
1740 printBasicBlock(BB);
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001741 }
Chris Lattner8c3c4bf2002-05-09 15:49:41 +00001742 }
Misha Brukmand6a29a52005-04-20 16:05:03 +00001743
Chris Lattner8c3c4bf2002-05-09 15:49:41 +00001744 Out << "}\n\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001745}
1746
Chris Lattnercb3ad012004-05-09 20:41:32 +00001747void CWriter::printLoop(Loop *L) {
1748 Out << " do { /* Syntactic loop '" << L->getHeader()->getName()
1749 << "' to make GCC happy */\n";
1750 for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i) {
1751 BasicBlock *BB = L->getBlocks()[i];
1752 Loop *BBLoop = LI->getLoopFor(BB);
1753 if (BBLoop == L)
1754 printBasicBlock(BB);
1755 else if (BB == BBLoop->getHeader() && BBLoop->getParentLoop() == L)
Misha Brukmand6a29a52005-04-20 16:05:03 +00001756 printLoop(BBLoop);
Chris Lattnercb3ad012004-05-09 20:41:32 +00001757 }
1758 Out << " } while (1); /* end of syntactic loop '"
1759 << L->getHeader()->getName() << "' */\n";
1760}
1761
1762void CWriter::printBasicBlock(BasicBlock *BB) {
1763
1764 // Don't print the label for the basic block if there are no uses, or if
1765 // the only terminator use is the predecessor basic block's terminator.
1766 // We have to scan the use list because PHI nodes use basic blocks too but
1767 // do not require a label to be generated.
1768 //
1769 bool NeedsLabel = false;
1770 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
1771 if (isGotoCodeNecessary(*PI, BB)) {
1772 NeedsLabel = true;
1773 break;
1774 }
Misha Brukmand6a29a52005-04-20 16:05:03 +00001775
Chris Lattnercb3ad012004-05-09 20:41:32 +00001776 if (NeedsLabel) Out << Mang->getValueName(BB) << ":\n";
Misha Brukmand6a29a52005-04-20 16:05:03 +00001777
Chris Lattnercb3ad012004-05-09 20:41:32 +00001778 // Output all of the instructions in the basic block...
1779 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E;
1780 ++II) {
1781 if (!isInlinableInst(*II) && !isDirectAlloca(II)) {
Andrew Lenharth29c277f2006-11-27 23:50:49 +00001782 if (II->getType() != Type::VoidTy && !isInlineAsm(*II))
Chris Lattnercb3ad012004-05-09 20:41:32 +00001783 outputLValue(II);
1784 else
1785 Out << " ";
1786 visit(*II);
1787 Out << ";\n";
1788 }
1789 }
Misha Brukmand6a29a52005-04-20 16:05:03 +00001790
Chris Lattnercb3ad012004-05-09 20:41:32 +00001791 // Don't emit prefix or suffix for the terminator...
1792 visit(*BB->getTerminator());
1793}
1794
1795
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001796// Specific Instruction type classes... note that all of the casts are
Misha Brukman5560c9d2003-08-18 14:43:39 +00001797// necessary because we use the instruction classes as opaque types...
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001798//
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001799void CWriter::visitReturnInst(ReturnInst &I) {
Chris Lattner0c54d892006-05-23 23:39:48 +00001800 // If this is a struct return function, return the temporary struct.
1801 if (I.getParent()->getParent()->getCallingConv() == CallingConv::CSRet) {
1802 Out << " return StructReturn;\n";
1803 return;
1804 }
1805
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001806 // Don't output a void return if this is the last basic block in the function
Misha Brukmand6a29a52005-04-20 16:05:03 +00001807 if (I.getNumOperands() == 0 &&
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001808 &*--I.getParent()->getParent()->end() == I.getParent() &&
1809 !I.getParent()->size() == 1) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001810 return;
Chris Lattner963b70b2002-05-21 18:05:19 +00001811 }
Chris Lattner44408262002-05-09 03:50:42 +00001812
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001813 Out << " return";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001814 if (I.getNumOperands()) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001815 Out << ' ';
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001816 writeOperand(I.getOperand(0));
Chris Lattner16c7bb22002-05-09 02:28:59 +00001817 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001818 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001819}
1820
Chris Lattnera9f5e052003-04-22 20:19:52 +00001821void CWriter::visitSwitchInst(SwitchInst &SI) {
Chris Lattnera05e0ec2004-05-09 03:42:48 +00001822
Chris Lattnera9f5e052003-04-22 20:19:52 +00001823 Out << " switch (";
1824 writeOperand(SI.getOperand(0));
Chris Lattnerf1acd962003-04-23 19:15:13 +00001825 Out << ") {\n default:\n";
John Criswell30cc2272004-10-25 18:30:09 +00001826 printPHICopiesForSuccessor (SI.getParent(), SI.getDefaultDest(), 2);
Chris Lattnerf1acd962003-04-23 19:15:13 +00001827 printBranchToBlock(SI.getParent(), SI.getDefaultDest(), 2);
Chris Lattnera9f5e052003-04-22 20:19:52 +00001828 Out << ";\n";
1829 for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) {
1830 Out << " case ";
1831 writeOperand(SI.getOperand(i));
1832 Out << ":\n";
1833 BasicBlock *Succ = cast<BasicBlock>(SI.getOperand(i+1));
John Criswell30cc2272004-10-25 18:30:09 +00001834 printPHICopiesForSuccessor (SI.getParent(), Succ, 2);
Chris Lattnera9f5e052003-04-22 20:19:52 +00001835 printBranchToBlock(SI.getParent(), Succ, 2);
Chris Lattnerefd02c72005-03-19 17:35:11 +00001836 if (Function::iterator(Succ) == next(Function::iterator(SI.getParent())))
Chris Lattnera9f5e052003-04-22 20:19:52 +00001837 Out << " break;\n";
1838 }
1839 Out << " }\n";
1840}
1841
Chris Lattnera9d12c02004-10-16 18:12:13 +00001842void CWriter::visitUnreachableInst(UnreachableInst &I) {
Chris Lattner963869e2004-10-17 23:49:11 +00001843 Out << " /*UNREACHABLE*/;\n";
Chris Lattnera9d12c02004-10-16 18:12:13 +00001844}
1845
Chris Lattnercb3ad012004-05-09 20:41:32 +00001846bool CWriter::isGotoCodeNecessary(BasicBlock *From, BasicBlock *To) {
1847 /// FIXME: This should be reenabled, but loop reordering safe!!
1848 return true;
1849
Chris Lattner67c2d182005-03-18 16:12:37 +00001850 if (next(Function::iterator(From)) != Function::iterator(To))
1851 return true; // Not the direct successor, we need a goto.
Chris Lattnercb3ad012004-05-09 20:41:32 +00001852
1853 //isa<SwitchInst>(From->getTerminator())
Chris Lattner9bda5f52003-06-28 17:53:05 +00001854
Chris Lattnercb3ad012004-05-09 20:41:32 +00001855 if (LI->getLoopFor(From) != LI->getLoopFor(To))
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001856 return true;
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001857 return false;
1858}
1859
John Criswell57bbfce2004-10-20 14:38:39 +00001860void CWriter::printPHICopiesForSuccessor (BasicBlock *CurBlock,
Misha Brukmand6a29a52005-04-20 16:05:03 +00001861 BasicBlock *Successor,
John Criswell57bbfce2004-10-20 14:38:39 +00001862 unsigned Indent) {
1863 for (BasicBlock::iterator I = Successor->begin(); isa<PHINode>(I); ++I) {
1864 PHINode *PN = cast<PHINode>(I);
1865 // Now we have to do the printing.
1866 Value *IV = PN->getIncomingValueForBlock(CurBlock);
1867 if (!isa<UndefValue>(IV)) {
1868 Out << std::string(Indent, ' ');
1869 Out << " " << Mang->getValueName(I) << "__PHI_TEMPORARY = ";
1870 writeOperand(IV);
1871 Out << "; /* for PHI node */\n";
1872 }
1873 }
1874}
1875
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001876void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
Chris Lattner9bda5f52003-06-28 17:53:05 +00001877 unsigned Indent) {
Chris Lattnercb3ad012004-05-09 20:41:32 +00001878 if (isGotoCodeNecessary(CurBB, Succ)) {
Chris Lattneredd8ce12003-05-03 03:14:35 +00001879 Out << std::string(Indent, ' ') << " goto ";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001880 writeOperand(Succ);
1881 Out << ";\n";
1882 }
1883}
1884
Misha Brukman37f92e22003-09-11 22:34:13 +00001885// Branch instruction printing - Avoid printing out a branch to a basic block
1886// that immediately succeeds the current one.
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001887//
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001888void CWriter::visitBranchInst(BranchInst &I) {
Chris Lattnera05e0ec2004-05-09 03:42:48 +00001889
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001890 if (I.isConditional()) {
Chris Lattner5ef6ffd2003-10-12 08:12:58 +00001891 if (isGotoCodeNecessary(I.getParent(), I.getSuccessor(0))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001892 Out << " if (";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001893 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001894 Out << ") {\n";
Misha Brukmand6a29a52005-04-20 16:05:03 +00001895
John Criswell57bbfce2004-10-20 14:38:39 +00001896 printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(0), 2);
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001897 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
Misha Brukmand6a29a52005-04-20 16:05:03 +00001898
Chris Lattner5ef6ffd2003-10-12 08:12:58 +00001899 if (isGotoCodeNecessary(I.getParent(), I.getSuccessor(1))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001900 Out << " } else {\n";
John Criswell57bbfce2004-10-20 14:38:39 +00001901 printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(1), 2);
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001902 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001903 }
1904 } else {
Misha Brukman5560c9d2003-08-18 14:43:39 +00001905 // First goto not necessary, assume second one is...
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001906 Out << " if (!";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001907 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001908 Out << ") {\n";
1909
John Criswell57bbfce2004-10-20 14:38:39 +00001910 printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(1), 2);
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001911 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001912 }
1913
1914 Out << " }\n";
1915 } else {
John Criswell57bbfce2004-10-20 14:38:39 +00001916 printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(0), 0);
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001917 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001918 }
1919 Out << "\n";
1920}
1921
Chris Lattner84e66652003-05-03 07:11:00 +00001922// PHI nodes get copied into temporary values at the end of predecessor basic
1923// blocks. We now need to copy these temporary values into the REAL value for
1924// the PHI.
1925void CWriter::visitPHINode(PHINode &I) {
1926 writeOperand(&I);
1927 Out << "__PHI_TEMPORARY";
1928}
1929
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001930
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001931void CWriter::visitBinaryOperator(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001932 // binary instructions, shift instructions, setCond instructions.
Chris Lattnerf5612b762003-04-23 19:09:22 +00001933 assert(!isa<PointerType>(I.getType()));
Brian Gaeke031a1122003-06-23 20:00:51 +00001934
1935 // We must cast the results of binary operations which might be promoted.
1936 bool needsCast = false;
1937 if ((I.getType() == Type::UByteTy) || (I.getType() == Type::SByteTy)
Brian Gaekee99f4cf2003-06-25 03:05:33 +00001938 || (I.getType() == Type::UShortTy) || (I.getType() == Type::ShortTy)
1939 || (I.getType() == Type::FloatTy)) {
Brian Gaeke031a1122003-06-23 20:00:51 +00001940 needsCast = true;
1941 Out << "((";
Chris Lattner7635ea42003-11-03 01:01:59 +00001942 printType(Out, I.getType());
Brian Gaeke031a1122003-06-23 20:00:51 +00001943 Out << ")(";
1944 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001945
Chris Lattner6d9b69f2005-03-03 21:12:04 +00001946 // If this is a negation operation, print it out as such. For FP, we don't
1947 // want to print "-0.0 - X".
1948 if (BinaryOperator::isNeg(&I)) {
John Criswellce4e1e42005-07-14 19:41:16 +00001949 Out << "-(";
Chris Lattner6d9b69f2005-03-03 21:12:04 +00001950 writeOperand(BinaryOperator::getNegArgument(cast<BinaryOperator>(&I)));
John Criswellce4e1e42005-07-14 19:41:16 +00001951 Out << ")";
Reid Spencer0a783f72006-11-02 01:53:59 +00001952 } else if (I.getOpcode() == Instruction::FRem) {
Chris Lattner57da2522005-08-23 20:22:50 +00001953 // Output a call to fmod/fmodf instead of emitting a%b
1954 if (I.getType() == Type::FloatTy)
1955 Out << "fmodf(";
1956 else
1957 Out << "fmod(";
1958 writeOperand(I.getOperand(0));
1959 Out << ", ";
1960 writeOperand(I.getOperand(1));
1961 Out << ")";
Chris Lattner6d9b69f2005-03-03 21:12:04 +00001962 } else {
Reid Spencer1628cec2006-10-26 06:15:43 +00001963
1964 // Write out the cast of the instruction's value back to the proper type
1965 // if necessary.
1966 bool NeedsClosingParens = writeInstructionCast(I);
1967
1968 // Certain instructions require the operand to be forced to a specific type
1969 // so we use writeOperandWithCast here instead of writeOperand. Similarly
1970 // below for operand 1
1971 writeOperandWithCast(I.getOperand(0), I.getOpcode());
Chris Lattner6d9b69f2005-03-03 21:12:04 +00001972
1973 switch (I.getOpcode()) {
1974 case Instruction::Add: Out << " + "; break;
1975 case Instruction::Sub: Out << " - "; break;
1976 case Instruction::Mul: Out << '*'; break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001977 case Instruction::URem:
1978 case Instruction::SRem:
1979 case Instruction::FRem: Out << '%'; break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001980 case Instruction::UDiv:
1981 case Instruction::SDiv:
1982 case Instruction::FDiv: Out << '/'; break;
Chris Lattner6d9b69f2005-03-03 21:12:04 +00001983 case Instruction::And: Out << " & "; break;
1984 case Instruction::Or: Out << " | "; break;
1985 case Instruction::Xor: Out << " ^ "; break;
1986 case Instruction::SetEQ: Out << " == "; break;
1987 case Instruction::SetNE: Out << " != "; break;
1988 case Instruction::SetLE: Out << " <= "; break;
1989 case Instruction::SetGE: Out << " >= "; break;
1990 case Instruction::SetLT: Out << " < "; break;
1991 case Instruction::SetGT: Out << " > "; break;
1992 case Instruction::Shl : Out << " << "; break;
Reid Spencer3822ff52006-11-08 06:47:33 +00001993 case Instruction::LShr:
1994 case Instruction::AShr: Out << " >> "; break;
Bill Wendlingf5da1332006-12-07 22:21:48 +00001995 default: cerr << "Invalid operator type!" << I; abort();
Chris Lattner6d9b69f2005-03-03 21:12:04 +00001996 }
1997
Reid Spencer1628cec2006-10-26 06:15:43 +00001998 writeOperandWithCast(I.getOperand(1), I.getOpcode());
1999 if (NeedsClosingParens)
2000 Out << "))";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002001 }
2002
Brian Gaeke031a1122003-06-23 20:00:51 +00002003 if (needsCast) {
2004 Out << "))";
2005 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002006}
2007
Reid Spencer555a0b12006-12-11 20:39:15 +00002008static const char * getFloatBitCastField(const Type *Ty) {
2009 switch (Ty->getTypeID()) {
2010 default: assert(0 && "Invalid Type");
2011 case Type::FloatTyID: return "Float";
2012 case Type::UIntTyID: return "UInt";
2013 case Type::IntTyID: return "SInt";
2014 case Type::DoubleTyID:return "Double";
2015 case Type::ULongTyID: return "ULong";
2016 case Type::LongTyID: return "SLong";
2017 }
2018}
2019
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002020void CWriter::visitCastInst(CastInst &I) {
Reid Spencer3da59db2006-11-27 01:05:10 +00002021 const Type *DstTy = I.getType();
2022 const Type *SrcTy = I.getOperand(0)->getType();
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002023 Out << '(';
Reid Spencer555a0b12006-12-11 20:39:15 +00002024 if (isa<BitCastInst>(I) &&
2025 ((I.getType()->isFloatingPoint() &&
2026 I.getOperand(0)->getType()->isInteger()) ||
2027 (I.getType()->isInteger() &&
2028 I.getOperand(0)->getType()->isFloatingPoint()))) {
2029 // These int<->float and long<->double casts need to be handled specially
2030 Out << Mang->getValueName(&I) << "__BITCAST_TEMPORARY."
2031 << getFloatBitCastField(I.getOperand(0)->getType()) << " = ";
2032 writeOperand(I.getOperand(0));
2033 Out << ", " << Mang->getValueName(&I) << "__BITCAST_TEMPORARY."
2034 << getFloatBitCastField(I.getType());
2035 } else {
2036 printCast(I.getOpcode(), SrcTy, DstTy);
2037 if (I.getOpcode() == Instruction::SExt && SrcTy == Type::BoolTy) {
2038 // Make sure we really get a sext from bool by subtracing the bool from 0
2039 Out << "0-";
2040 }
2041 writeOperand(I.getOperand(0));
2042 if (DstTy == Type::BoolTy &&
2043 (I.getOpcode() == Instruction::Trunc ||
2044 I.getOpcode() == Instruction::FPToUI ||
2045 I.getOpcode() == Instruction::FPToSI ||
2046 I.getOpcode() == Instruction::PtrToInt)) {
2047 // Make sure we really get a trunc to bool by anding the operand with 1
2048 Out << "&1u";
2049 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002050 }
2051 Out << ')';
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002052}
2053
Chris Lattner9f24a072004-03-12 05:52:14 +00002054void CWriter::visitSelectInst(SelectInst &I) {
2055 Out << "((";
2056 writeOperand(I.getCondition());
2057 Out << ") ? (";
2058 writeOperand(I.getTrueValue());
2059 Out << ") : (";
2060 writeOperand(I.getFalseValue());
Misha Brukmand6a29a52005-04-20 16:05:03 +00002061 Out << "))";
Chris Lattner9f24a072004-03-12 05:52:14 +00002062}
2063
2064
Chris Lattnerc2421432004-05-09 04:30:20 +00002065void CWriter::lowerIntrinsics(Function &F) {
2066 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
2067 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
2068 if (CallInst *CI = dyn_cast<CallInst>(I++))
2069 if (Function *F = CI->getCalledFunction())
2070 switch (F->getIntrinsicID()) {
2071 case Intrinsic::not_intrinsic:
2072 case Intrinsic::vastart:
2073 case Intrinsic::vacopy:
2074 case Intrinsic::vaend:
2075 case Intrinsic::returnaddress:
2076 case Intrinsic::frameaddress:
2077 case Intrinsic::setjmp:
2078 case Intrinsic::longjmp:
Chris Lattner33e9d292005-02-28 19:29:46 +00002079 case Intrinsic::prefetch:
Jim Laskey7075d6f2006-03-08 19:31:15 +00002080 case Intrinsic::dbg_stoppoint:
Chris Lattner1e142892006-09-09 06:17:12 +00002081 case Intrinsic::powi_f32:
2082 case Intrinsic::powi_f64:
Chris Lattnerc2421432004-05-09 04:30:20 +00002083 // We directly implement these intrinsics
2084 break;
2085 default:
Chris Lattner87242152006-03-13 23:09:05 +00002086 // If this is an intrinsic that directly corresponds to a GCC
2087 // builtin, we handle it.
2088 const char *BuiltinName = "";
2089#define GET_GCC_BUILTIN_NAME
2090#include "llvm/Intrinsics.gen"
2091#undef GET_GCC_BUILTIN_NAME
2092 // If we handle it, don't lower it.
2093 if (BuiltinName[0]) break;
2094
Chris Lattnerc2421432004-05-09 04:30:20 +00002095 // All other intrinsic calls we must lower.
Chris Lattner67c2d182005-03-18 16:12:37 +00002096 Instruction *Before = 0;
Misha Brukmand6a29a52005-04-20 16:05:03 +00002097 if (CI != &BB->front())
Chris Lattner67c2d182005-03-18 16:12:37 +00002098 Before = prior(BasicBlock::iterator(CI));
Misha Brukmand6a29a52005-04-20 16:05:03 +00002099
Chris Lattnerc2421432004-05-09 04:30:20 +00002100 IL.LowerIntrinsicCall(CI);
2101 if (Before) { // Move iterator to instruction after call
2102 I = Before; ++I;
2103 } else {
2104 I = BB->begin();
Chris Lattner4ef51372004-02-14 00:31:10 +00002105 }
Chris Lattner87242152006-03-13 23:09:05 +00002106 break;
Chris Lattnerc2421432004-05-09 04:30:20 +00002107 }
Chris Lattner4ef51372004-02-14 00:31:10 +00002108}
2109
2110
2111
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002112void CWriter::visitCallInst(CallInst &I) {
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002113 //check if we have inline asm
2114 if (isInlineAsm(I)) {
2115 visitInlineAsm(I);
2116 return;
2117 }
2118
Chris Lattner87242152006-03-13 23:09:05 +00002119 bool WroteCallee = false;
2120
Chris Lattner18ac3c82003-05-08 18:41:45 +00002121 // Handle intrinsic function calls first...
2122 if (Function *F = I.getCalledFunction())
Brian Gaeked0fde302003-11-11 22:41:34 +00002123 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
Chris Lattner18ac3c82003-05-08 18:41:45 +00002124 switch (ID) {
Chris Lattner87242152006-03-13 23:09:05 +00002125 default: {
2126 // If this is an intrinsic that directly corresponds to a GCC
2127 // builtin, we emit it here.
2128 const char *BuiltinName = "";
2129#define GET_GCC_BUILTIN_NAME
2130#include "llvm/Intrinsics.gen"
2131#undef GET_GCC_BUILTIN_NAME
2132 assert(BuiltinName[0] && "Unknown LLVM intrinsic!");
2133
2134 Out << BuiltinName;
2135 WroteCallee = true;
2136 break;
2137 }
Misha Brukmand6a29a52005-04-20 16:05:03 +00002138 case Intrinsic::vastart:
Chris Lattner4d45bd02003-10-18 05:57:43 +00002139 Out << "0; ";
Misha Brukmand6a29a52005-04-20 16:05:03 +00002140
Andrew Lenharth558bc882005-06-18 18:34:52 +00002141 Out << "va_start(*(va_list*)";
2142 writeOperand(I.getOperand(1));
2143 Out << ", ";
Chris Lattner18ac3c82003-05-08 18:41:45 +00002144 // Output the last argument to the enclosing function...
Chris Lattnere4d5c442005-03-15 04:54:21 +00002145 if (I.getParent()->getParent()->arg_empty()) {
Bill Wendlingf5da1332006-12-07 22:21:48 +00002146 cerr << "The C backend does not currently support zero "
2147 << "argument varargs functions, such as '"
2148 << I.getParent()->getParent()->getName() << "'!\n";
Chris Lattner4065ef92003-10-23 18:39:22 +00002149 abort();
2150 }
Chris Lattnerc5e7df12005-03-15 04:59:17 +00002151 writeOperand(--I.getParent()->getParent()->arg_end());
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002152 Out << ')';
Chris Lattner18ac3c82003-05-08 18:41:45 +00002153 return;
Chris Lattner317201d2004-03-13 00:24:00 +00002154 case Intrinsic::vaend:
Chris Lattnerf8e6f4f2004-08-10 00:19:16 +00002155 if (!isa<ConstantPointerNull>(I.getOperand(1))) {
Andrew Lenharth558bc882005-06-18 18:34:52 +00002156 Out << "0; va_end(*(va_list*)";
Chris Lattnerf8e6f4f2004-08-10 00:19:16 +00002157 writeOperand(I.getOperand(1));
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002158 Out << ')';
Chris Lattnerf8e6f4f2004-08-10 00:19:16 +00002159 } else {
2160 Out << "va_end(*(va_list*)0)";
2161 }
Chris Lattner18ac3c82003-05-08 18:41:45 +00002162 return;
Chris Lattner317201d2004-03-13 00:24:00 +00002163 case Intrinsic::vacopy:
Andrew Lenharth558bc882005-06-18 18:34:52 +00002164 Out << "0; ";
2165 Out << "va_copy(*(va_list*)";
Chris Lattner18ac3c82003-05-08 18:41:45 +00002166 writeOperand(I.getOperand(1));
Andrew Lenharth213e5572005-06-22 21:04:42 +00002167 Out << ", *(va_list*)";
Andrew Lenharth558bc882005-06-18 18:34:52 +00002168 writeOperand(I.getOperand(2));
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002169 Out << ')';
Chris Lattner18ac3c82003-05-08 18:41:45 +00002170 return;
Chris Lattnerfe1554a2004-02-14 02:55:36 +00002171 case Intrinsic::returnaddress:
2172 Out << "__builtin_return_address(";
2173 writeOperand(I.getOperand(1));
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002174 Out << ')';
Chris Lattnerfe1554a2004-02-14 02:55:36 +00002175 return;
2176 case Intrinsic::frameaddress:
2177 Out << "__builtin_frame_address(";
2178 writeOperand(I.getOperand(1));
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002179 Out << ')';
Chris Lattnerfe1554a2004-02-14 02:55:36 +00002180 return;
Chris Lattner1e142892006-09-09 06:17:12 +00002181 case Intrinsic::powi_f32:
2182 case Intrinsic::powi_f64:
2183 Out << "__builtin_powi(";
2184 writeOperand(I.getOperand(1));
2185 Out << ", ";
2186 writeOperand(I.getOperand(2));
2187 Out << ')';
2188 return;
Chris Lattnere42cde22004-02-15 22:51:47 +00002189 case Intrinsic::setjmp:
2190 Out << "setjmp(*(jmp_buf*)";
2191 writeOperand(I.getOperand(1));
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002192 Out << ')';
Chris Lattnere42cde22004-02-15 22:51:47 +00002193 return;
2194 case Intrinsic::longjmp:
2195 Out << "longjmp(*(jmp_buf*)";
2196 writeOperand(I.getOperand(1));
2197 Out << ", ";
2198 writeOperand(I.getOperand(2));
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002199 Out << ')';
Chris Lattnere42cde22004-02-15 22:51:47 +00002200 return;
Chris Lattner33e9d292005-02-28 19:29:46 +00002201 case Intrinsic::prefetch:
Chris Lattnerca76f352005-02-28 19:36:15 +00002202 Out << "LLVM_PREFETCH((const void *)";
Chris Lattner33e9d292005-02-28 19:29:46 +00002203 writeOperand(I.getOperand(1));
2204 Out << ", ";
2205 writeOperand(I.getOperand(2));
2206 Out << ", ";
2207 writeOperand(I.getOperand(3));
2208 Out << ")";
Chris Lattnerca76f352005-02-28 19:36:15 +00002209 return;
Jim Laskey7075d6f2006-03-08 19:31:15 +00002210 case Intrinsic::dbg_stoppoint: {
2211 // If we use writeOperand directly we get a "u" suffix which is rejected
2212 // by gcc.
Jim Laskey580418e2006-03-23 18:08:29 +00002213 DbgStopPointInst &SPI = cast<DbgStopPointInst>(I);
Jim Laskey7075d6f2006-03-08 19:31:15 +00002214
2215 Out << "\n#line "
Jim Laskey580418e2006-03-23 18:08:29 +00002216 << SPI.getLine()
2217 << " \"" << SPI.getDirectory()
2218 << SPI.getFileName() << "\"\n";
Jim Laskey7075d6f2006-03-08 19:31:15 +00002219 return;
2220 }
Chris Lattner18ac3c82003-05-08 18:41:45 +00002221 }
2222 }
2223
Chris Lattner4394d512004-11-13 22:21:56 +00002224 Value *Callee = I.getCalledValue();
Misha Brukmand6a29a52005-04-20 16:05:03 +00002225
Chris Lattner0c54d892006-05-23 23:39:48 +00002226 // If this is a call to a struct-return function, assign to the first
2227 // parameter instead of passing it to the call.
2228 bool isStructRet = I.getCallingConv() == CallingConv::CSRet;
2229 if (isStructRet) {
2230 Out << "*(";
2231 writeOperand(I.getOperand(1));
2232 Out << ") = ";
2233 }
2234
Chris Lattnerfe673d92005-05-06 06:53:07 +00002235 if (I.isTailCall()) Out << " /*tail*/ ";
Chris Lattner4394d512004-11-13 22:21:56 +00002236
2237 const PointerType *PTy = cast<PointerType>(Callee->getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002238 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
Chris Lattner0c54d892006-05-23 23:39:48 +00002239
2240 if (!WroteCallee) {
2241 // If this is an indirect call to a struct return function, we need to cast
2242 // the pointer.
2243 bool NeedsCast = isStructRet && !isa<Function>(Callee);
Misha Brukmand6a29a52005-04-20 16:05:03 +00002244
Chris Lattner0c54d892006-05-23 23:39:48 +00002245 // GCC is a real PITA. It does not permit codegening casts of functions to
2246 // function pointers if they are in a call (it generates a trap instruction
2247 // instead!). We work around this by inserting a cast to void* in between
2248 // the function and the function pointer cast. Unfortunately, we can't just
2249 // form the constant expression here, because the folder will immediately
2250 // nuke it.
2251 //
2252 // Note finally, that this is completely unsafe. ANSI C does not guarantee
2253 // that void* and function pointers have the same size. :( To deal with this
2254 // in the common case, we handle casts where the number of arguments passed
2255 // match exactly.
2256 //
2257 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Callee))
Reid Spencer3da59db2006-11-27 01:05:10 +00002258 if (CE->isCast())
Chris Lattner0c54d892006-05-23 23:39:48 +00002259 if (Function *RF = dyn_cast<Function>(CE->getOperand(0))) {
2260 NeedsCast = true;
2261 Callee = RF;
2262 }
2263
2264 if (NeedsCast) {
2265 // Ok, just cast the pointer type.
2266 Out << "((";
2267 if (!isStructRet)
2268 printType(Out, I.getCalledValue()->getType());
2269 else
2270 printStructReturnPointerFunctionType(Out,
2271 cast<PointerType>(I.getCalledValue()->getType()));
2272 Out << ")(void*)";
2273 }
2274 writeOperand(Callee);
2275 if (NeedsCast) Out << ')';
2276 }
2277
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002278 Out << '(';
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002279
Chris Lattner4394d512004-11-13 22:21:56 +00002280 unsigned NumDeclaredParams = FTy->getNumParams();
2281
Chris Lattner0c54d892006-05-23 23:39:48 +00002282 CallSite::arg_iterator AI = I.op_begin()+1, AE = I.op_end();
2283 unsigned ArgNo = 0;
2284 if (isStructRet) { // Skip struct return argument.
2285 ++AI;
2286 ++ArgNo;
2287 }
2288
2289 bool PrintedArg = false;
2290 for (; AI != AE; ++AI, ++ArgNo) {
2291 if (PrintedArg) Out << ", ";
2292 if (ArgNo < NumDeclaredParams &&
2293 (*AI)->getType() != FTy->getParamType(ArgNo)) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002294 Out << '(';
Chris Lattner0c54d892006-05-23 23:39:48 +00002295 printType(Out, FTy->getParamType(ArgNo));
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002296 Out << ')';
Chris Lattner4394d512004-11-13 22:21:56 +00002297 }
Chris Lattner9bda5f52003-06-28 17:53:05 +00002298 writeOperand(*AI);
Chris Lattner0c54d892006-05-23 23:39:48 +00002299 PrintedArg = true;
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002300 }
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002301 Out << ')';
Misha Brukmand6a29a52005-04-20 16:05:03 +00002302}
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002303
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002304
2305//This converts the llvm constraint string to something gcc is expecting.
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002306//TODO: work out platform independent constraints and factor those out
Andrew Lenharth238581f2006-11-28 19:56:02 +00002307// of the per target tables
2308// handle multiple constraint codes
Andrew Lenharthe0cf0752006-11-28 19:53:36 +00002309std::string CWriter::InterpretASMConstraint(InlineAsm::ConstraintInfo& c) {
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002310
2311 assert(c.Codes.size() == 1 && "Too many asm constraint codes to handle");
2312
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002313 const char** table = 0;
Andrew Lenharthe0cf0752006-11-28 19:53:36 +00002314
2315 //Grab the translation table from TargetAsmInfo if it exists
2316 if (!TAsm) {
2317 std::string E;
2318 const TargetMachineRegistry::Entry* Match =
2319 TargetMachineRegistry::getClosestStaticTargetForModule(*TheModule, E);
2320 if (Match) {
2321 //Per platform Target Machines don't exist, so create it
2322 // this must be done only once
2323 const TargetMachine* TM = Match->CtorFn(*TheModule, "");
2324 TAsm = TM->getTargetAsmInfo();
2325 }
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002326 }
Andrew Lenharthe0cf0752006-11-28 19:53:36 +00002327 if (TAsm)
2328 table = TAsm->getAsmCBE();
2329
2330 //Search the translation table if it exists
2331 for (int i = 0; table && table[i]; i += 2)
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002332 if (c.Codes[0] == table[i])
2333 return table[i+1];
2334
Andrew Lenharth85f22282006-11-28 22:25:32 +00002335 //default is identity
2336 return c.Codes[0];
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002337}
2338
2339//TODO: import logic from AsmPrinter.cpp
Andrew Lenharthe0cf0752006-11-28 19:53:36 +00002340static std::string gccifyAsm(std::string asmstr) {
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002341 for (std::string::size_type i = 0; i != asmstr.size(); ++i)
2342 if (asmstr[i] == '\n')
2343 asmstr.replace(i, 1, "\\n");
2344 else if (asmstr[i] == '\t')
2345 asmstr.replace(i, 1, "\\t");
2346 else if (asmstr[i] == '$') {
2347 if (asmstr[i + 1] == '{') {
2348 std::string::size_type a = asmstr.find_first_of(':', i + 1);
2349 std::string::size_type b = asmstr.find_first_of('}', i + 1);
2350 std::string n = "%" +
2351 asmstr.substr(a + 1, b - a - 1) +
2352 asmstr.substr(i + 2, a - i - 2);
2353 asmstr.replace(i, b - i + 1, n);
2354 i += n.size() - 1;
2355 } else
2356 asmstr.replace(i, 1, "%");
2357 }
2358 else if (asmstr[i] == '%')//grr
2359 { asmstr.replace(i, 1, "%%"); ++i;}
2360
2361 return asmstr;
2362}
2363
Andrew Lenharth238581f2006-11-28 19:56:02 +00002364//TODO: assumptions about what consume arguments from the call are likely wrong
2365// handle communitivity
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002366void CWriter::visitInlineAsm(CallInst &CI) {
2367 InlineAsm* as = cast<InlineAsm>(CI.getOperand(0));
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002368 std::vector<InlineAsm::ConstraintInfo> Constraints = as->ParseConstraints();
2369 std::vector<std::pair<std::string, Value*> > Input;
2370 std::vector<std::pair<std::string, Value*> > Output;
2371 std::string Clobber;
2372 int count = CI.getType() == Type::VoidTy ? 1 : 0;
2373 for (std::vector<InlineAsm::ConstraintInfo>::iterator I = Constraints.begin(),
2374 E = Constraints.end(); I != E; ++I) {
2375 assert(I->Codes.size() == 1 && "Too many asm constraint codes to handle");
2376 std::string c =
Andrew Lenharthe0cf0752006-11-28 19:53:36 +00002377 InterpretASMConstraint(*I);
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002378 switch(I->Type) {
2379 default:
2380 assert(0 && "Unknown asm constraint");
2381 break;
2382 case InlineAsm::isInput: {
2383 if (c.size()) {
2384 Input.push_back(std::make_pair(c, count ? CI.getOperand(count) : &CI));
2385 ++count; //consume arg
2386 }
2387 break;
2388 }
2389 case InlineAsm::isOutput: {
2390 if (c.size()) {
2391 Output.push_back(std::make_pair("="+((I->isEarlyClobber ? "&" : "")+c),
2392 count ? CI.getOperand(count) : &CI));
2393 ++count; //consume arg
2394 }
2395 break;
2396 }
2397 case InlineAsm::isClobber: {
2398 if (c.size())
2399 Clobber += ",\"" + c + "\"";
2400 break;
2401 }
2402 }
2403 }
2404
2405 //fix up the asm string for gcc
Andrew Lenharthe0cf0752006-11-28 19:53:36 +00002406 std::string asmstr = gccifyAsm(as->getAsmString());
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002407
2408 Out << "__asm__ volatile (\"" << asmstr << "\"\n";
2409 Out << " :";
2410 for (std::vector<std::pair<std::string, Value*> >::iterator I = Output.begin(),
2411 E = Output.end(); I != E; ++I) {
2412 Out << "\"" << I->first << "\"(";
2413 writeOperandRaw(I->second);
2414 Out << ")";
2415 if (I + 1 != E)
2416 Out << ",";
2417 }
2418 Out << "\n :";
2419 for (std::vector<std::pair<std::string, Value*> >::iterator I = Input.begin(),
2420 E = Input.end(); I != E; ++I) {
2421 Out << "\"" << I->first << "\"(";
2422 writeOperandRaw(I->second);
2423 Out << ")";
2424 if (I + 1 != E)
2425 Out << ",";
2426 }
Andrew Lenharthf45148e2006-11-28 23:07:32 +00002427 if (Clobber.size())
2428 Out << "\n :" << Clobber.substr(1);
2429 Out << ")";
Andrew Lenharth29c277f2006-11-27 23:50:49 +00002430}
2431
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002432void CWriter::visitMallocInst(MallocInst &I) {
Chris Lattnerf31182a2004-02-13 23:18:48 +00002433 assert(0 && "lowerallocations pass didn't work!");
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002434}
2435
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002436void CWriter::visitAllocaInst(AllocaInst &I) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002437 Out << '(';
Nick Hildenbrandt98502372002-11-18 20:55:50 +00002438 printType(Out, I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002439 Out << ") alloca(sizeof(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00002440 printType(Out, I.getType()->getElementType());
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002441 Out << ')';
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002442 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002443 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002444 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002445 }
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002446 Out << ')';
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002447}
2448
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002449void CWriter::visitFreeInst(FreeInst &I) {
Chris Lattnerf31182a2004-02-13 23:18:48 +00002450 assert(0 && "lowerallocations pass didn't work!");
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002451}
2452
Chris Lattner23e90702003-11-25 20:49:55 +00002453void CWriter::printIndexingExpression(Value *Ptr, gep_type_iterator I,
2454 gep_type_iterator E) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00002455 bool HasImplicitAddress = false;
2456 // If accessing a global value with no indexing, avoid *(&GV) syndrome
Reid Spencer3ed469c2006-11-02 20:25:50 +00002457 if (isa<GlobalValue>(Ptr)) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00002458 HasImplicitAddress = true;
Chris Lattnera8ab89e2003-06-17 04:39:14 +00002459 } else if (isDirectAlloca(Ptr)) {
2460 HasImplicitAddress = true;
Chris Lattnerd0c668c2002-05-09 21:18:38 +00002461 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002462
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00002463 if (I == E) {
2464 if (!HasImplicitAddress)
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002465 Out << '*'; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002466
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00002467 writeOperandInternal(Ptr);
2468 return;
2469 }
2470
Chris Lattner23e90702003-11-25 20:49:55 +00002471 const Constant *CI = dyn_cast<Constant>(I.getOperand());
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00002472 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
2473 Out << "(&";
2474
2475 writeOperandInternal(Ptr);
2476
Nick Hildenbrandte548f002002-09-25 20:29:26 +00002477 if (HasImplicitAddress && (!CI || !CI->isNullValue())) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002478 Out << ')';
Nick Hildenbrandte548f002002-09-25 20:29:26 +00002479 HasImplicitAddress = false; // HIA is only true if we haven't addressed yet
2480 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002481
Nick Hildenbrandte548f002002-09-25 20:29:26 +00002482 assert(!HasImplicitAddress || (CI && CI->isNullValue()) &&
2483 "Can only have implicit address with direct accessing");
2484
2485 if (HasImplicitAddress) {
2486 ++I;
Chris Lattner23e90702003-11-25 20:49:55 +00002487 } else if (CI && CI->isNullValue()) {
2488 gep_type_iterator TmpI = I; ++TmpI;
2489
Nick Hildenbrandte548f002002-09-25 20:29:26 +00002490 // Print out the -> operator if possible...
Chris Lattner23e90702003-11-25 20:49:55 +00002491 if (TmpI != E && isa<StructType>(*TmpI)) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00002492 Out << (HasImplicitAddress ? "." : "->");
Reid Spencerb83eb642006-10-20 07:07:24 +00002493 Out << "field" << cast<ConstantInt>(TmpI.getOperand())->getZExtValue();
Chris Lattner23e90702003-11-25 20:49:55 +00002494 I = ++TmpI;
2495 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002496 }
Chris Lattner5dfe7672002-08-22 22:48:55 +00002497
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002498 for (; I != E; ++I)
Chris Lattner23e90702003-11-25 20:49:55 +00002499 if (isa<StructType>(*I)) {
Reid Spencerb83eb642006-10-20 07:07:24 +00002500 Out << ".field" << cast<ConstantInt>(I.getOperand())->getZExtValue();
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002501 } else {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002502 Out << '[';
Chris Lattner23e90702003-11-25 20:49:55 +00002503 writeOperand(I.getOperand());
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002504 Out << ']';
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002505 }
2506}
2507
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002508void CWriter::visitLoadInst(LoadInst &I) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002509 Out << '*';
Chris Lattner9d30e222005-02-14 16:47:52 +00002510 if (I.isVolatile()) {
Chris Lattner8399e022005-02-15 05:52:14 +00002511 Out << "((";
Chris Lattnerb94388a2005-09-27 20:52:44 +00002512 printType(Out, I.getType(), "volatile*");
2513 Out << ")";
Chris Lattner9d30e222005-02-14 16:47:52 +00002514 }
2515
Chris Lattner5dfe7672002-08-22 22:48:55 +00002516 writeOperand(I.getOperand(0));
Chris Lattner9d30e222005-02-14 16:47:52 +00002517
2518 if (I.isVolatile())
Misha Brukman23ba0c52005-03-08 00:26:08 +00002519 Out << ')';
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002520}
2521
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002522void CWriter::visitStoreInst(StoreInst &I) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002523 Out << '*';
Chris Lattner9d30e222005-02-14 16:47:52 +00002524 if (I.isVolatile()) {
Chris Lattner8399e022005-02-15 05:52:14 +00002525 Out << "((";
Chris Lattnerb94388a2005-09-27 20:52:44 +00002526 printType(Out, I.getOperand(0)->getType(), " volatile*");
2527 Out << ")";
Chris Lattner9d30e222005-02-14 16:47:52 +00002528 }
Chris Lattner5dfe7672002-08-22 22:48:55 +00002529 writeOperand(I.getPointerOperand());
Misha Brukman23ba0c52005-03-08 00:26:08 +00002530 if (I.isVolatile()) Out << ')';
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002531 Out << " = ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002532 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002533}
2534
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002535void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002536 Out << '&';
Chris Lattner23e90702003-11-25 20:49:55 +00002537 printIndexingExpression(I.getPointerOperand(), gep_type_begin(I),
2538 gep_type_end(I));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002539}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00002540
Chris Lattner4d45bd02003-10-18 05:57:43 +00002541void CWriter::visitVAArgInst(VAArgInst &I) {
Andrew Lenharth558bc882005-06-18 18:34:52 +00002542 Out << "va_arg(*(va_list*)";
Chris Lattner4d45bd02003-10-18 05:57:43 +00002543 writeOperand(I.getOperand(0));
Andrew Lenharth558bc882005-06-18 18:34:52 +00002544 Out << ", ";
Chris Lattner7635ea42003-11-03 01:01:59 +00002545 printType(Out, I.getType());
Andrew Lenharth558bc882005-06-18 18:34:52 +00002546 Out << ");\n ";
Chris Lattner4d45bd02003-10-18 05:57:43 +00002547}
2548
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00002549//===----------------------------------------------------------------------===//
2550// External Interface declaration
2551//===----------------------------------------------------------------------===//
2552
Chris Lattner1911fd42006-09-04 04:14:57 +00002553bool CTargetMachine::addPassesToEmitWholeFile(PassManager &PM,
2554 std::ostream &o,
2555 CodeGenFileType FileType,
2556 bool Fast) {
Chris Lattner0431c962005-06-25 02:48:37 +00002557 if (FileType != TargetMachine::AssemblyFile) return true;
2558
Chris Lattner99c59e82004-05-23 21:23:35 +00002559 PM.add(createLowerGCPass());
Chris Lattnerf7fe4942005-03-03 01:04:50 +00002560 PM.add(createLowerAllocationsPass(true));
Chris Lattner94f4f8e2004-02-13 23:00:29 +00002561 PM.add(createLowerInvokePass());
Chris Lattnerbad13eb2005-11-02 17:42:58 +00002562 PM.add(createCFGSimplificationPass()); // clean up after lower invoke.
Chris Lattnerf9a05322006-02-13 22:22:42 +00002563 PM.add(new CBackendNameAllUsedStructsAndMergeFunctions());
Chris Lattnerbc641b92006-03-23 05:43:16 +00002564 PM.add(new CWriter(o));
Chris Lattnerf31182a2004-02-13 23:18:48 +00002565 return false;
2566}