Chris Lattner | 3af3ba8 | 2002-05-09 21:31:18 +0000 | [diff] [blame] | 1 | //===-- Writer.cpp - Library for converting LLVM code to C ----------------===// |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // 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 Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 897bf0d | 2004-02-13 06:18:21 +0000 | [diff] [blame] | 10 | // This library converts LLVM code to C code, compilable by GCC and other C |
| 11 | // compilers. |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 12 | // |
Chris Lattner | edd8ce1 | 2003-05-03 03:14:35 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
Chris Lattner | 84e6665 | 2003-05-03 07:11:00 +0000 | [diff] [blame] | 14 | |
Chris Lattner | f31182a | 2004-02-13 23:18:48 +0000 | [diff] [blame] | 15 | #include "CTargetMachine.h" |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 16 | #include "llvm/CallingConv.h" |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
Chris Lattner | 2f5f51a | 2002-05-09 03:28:37 +0000 | [diff] [blame] | 18 | #include "llvm/DerivedTypes.h" |
| 19 | #include "llvm/Module.h" |
Chris Lattner | a9f5e05 | 2003-04-22 20:19:52 +0000 | [diff] [blame] | 20 | #include "llvm/Instructions.h" |
Chris Lattner | 0e9f93e | 2002-08-31 00:29:16 +0000 | [diff] [blame] | 21 | #include "llvm/Pass.h" |
Chris Lattner | 94f4f8e | 2004-02-13 23:00:29 +0000 | [diff] [blame] | 22 | #include "llvm/PassManager.h" |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 23 | #include "llvm/SymbolTable.h" |
Chris Lattner | 18ac3c8 | 2003-05-08 18:41:45 +0000 | [diff] [blame] | 24 | #include "llvm/Intrinsics.h" |
Jim Laskey | 580418e | 2006-03-23 18:08:29 +0000 | [diff] [blame] | 25 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | d1cf1b4 | 2002-09-20 23:26:33 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/ConstantsScanner.h" |
Chris Lattner | cb3ad01 | 2004-05-09 20:41:32 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/FindUsedTypes.h" |
| 28 | #include "llvm/Analysis/LoopInfo.h" |
Chris Lattner | 3048373 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/IntrinsicLowering.h" |
Chris Lattner | 94f4f8e | 2004-02-13 23:00:29 +0000 | [diff] [blame] | 30 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | d36c970 | 2004-07-11 02:48:49 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetMachineRegistry.h" |
Chris Lattner | 23e9070 | 2003-11-25 20:49:55 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CallSite.h" |
Chris Lattner | a05e0ec | 2004-05-09 03:42:48 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CFG.h" |
Chris Lattner | 23e9070 | 2003-11-25 20:49:55 +0000 | [diff] [blame] | 34 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 35 | #include "llvm/Support/InstVisitor.h" |
Brian Gaeke | 49a178b | 2003-07-25 20:21:06 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Mangler.h" |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 37 | #include "llvm/Support/MathExtras.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 67c2d18 | 2005-03-18 16:12:37 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/STLExtras.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 40 | #include "llvm/Support/MathExtras.h" |
| 41 | #include "llvm/Config/config.h" |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 42 | #include <algorithm> |
Reid Spencer | 954da37 | 2004-07-04 12:19:56 +0000 | [diff] [blame] | 43 | #include <iostream> |
Duraid Madina | d2dec7d | 2005-12-27 10:40:34 +0000 | [diff] [blame] | 44 | #include <ios> |
Nick Hildenbrandt | 9850237 | 2002-11-18 20:55:50 +0000 | [diff] [blame] | 45 | #include <sstream> |
Chris Lattner | 897bf0d | 2004-02-13 06:18:21 +0000 | [diff] [blame] | 46 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 47 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 48 | namespace { |
Chris Lattner | d36c970 | 2004-07-11 02:48:49 +0000 | [diff] [blame] | 49 | // Register the target. |
Chris Lattner | 71d24aa | 2004-07-11 03:27:42 +0000 | [diff] [blame] | 50 | RegisterTarget<CTargetMachine> X("c", " C backend"); |
Chris Lattner | d36c970 | 2004-07-11 02:48:49 +0000 | [diff] [blame] | 51 | |
Chris Lattner | f9a0532 | 2006-02-13 22:22:42 +0000 | [diff] [blame] | 52 | /// 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 Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 55 | /// |
Chris Lattner | f9a0532 | 2006-02-13 22:22:42 +0000 | [diff] [blame] | 56 | class CBackendNameAllUsedStructsAndMergeFunctions : public ModulePass { |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 57 | 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 Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 65 | virtual bool runOnModule(Module &M); |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 66 | }; |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 68 | /// 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 Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 71 | std::ostream &Out; |
Chris Lattner | bc641b9 | 2006-03-23 05:43:16 +0000 | [diff] [blame] | 72 | DefaultIntrinsicLowering IL; |
Brian Gaeke | d9fb37a | 2003-07-24 20:20:44 +0000 | [diff] [blame] | 73 | Mangler *Mang; |
Chris Lattner | cb3ad01 | 2004-05-09 20:41:32 +0000 | [diff] [blame] | 74 | LoopInfo *LI; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 75 | const Module *TheModule; |
Chris Lattner | edd8ce1 | 2003-05-03 03:14:35 +0000 | [diff] [blame] | 76 | std::map<const Type *, std::string> TypeNames; |
Nick Hildenbrandt | 67aa2e2 | 2002-09-14 21:36:24 +0000 | [diff] [blame] | 77 | |
Chris Lattner | edd8ce1 | 2003-05-03 03:14:35 +0000 | [diff] [blame] | 78 | std::map<const ConstantFP *, unsigned> FPConstantMap; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 79 | public: |
Chris Lattner | bc641b9 | 2006-03-23 05:43:16 +0000 | [diff] [blame] | 80 | CWriter(std::ostream &o) : Out(o) {} |
Chris Lattner | 0e9f93e | 2002-08-31 00:29:16 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 94f4f8e | 2004-02-13 23:00:29 +0000 | [diff] [blame] | 82 | virtual const char *getPassName() const { return "C backend"; } |
Chris Lattner | 0e9f93e | 2002-08-31 00:29:16 +0000 | [diff] [blame] | 83 | |
Chris Lattner | cb3ad01 | 2004-05-09 20:41:32 +0000 | [diff] [blame] | 84 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 85 | AU.addRequired<LoopInfo>(); |
| 86 | AU.setPreservesAll(); |
| 87 | } |
| 88 | |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 89 | virtual bool doInitialization(Module &M); |
Chris Lattner | 0e9f93e | 2002-08-31 00:29:16 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 91 | bool runOnFunction(Function &F) { |
Chris Lattner | cb3ad01 | 2004-05-09 20:41:32 +0000 | [diff] [blame] | 92 | LI = &getAnalysis<LoopInfo>(); |
| 93 | |
Chris Lattner | 3150e2d | 2004-12-05 06:49:44 +0000 | [diff] [blame] | 94 | // Get rid of intrinsics we can't handle. |
| 95 | lowerIntrinsics(F); |
| 96 | |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 97 | // Output all floating point constants that cannot be printed accurately. |
| 98 | printFloatingPointConstants(F); |
Chris Lattner | 3150e2d | 2004-12-05 06:49:44 +0000 | [diff] [blame] | 99 | |
| 100 | // Ensure that no local symbols conflict with global symbols. |
| 101 | F.renameLocalSymbols(); |
| 102 | |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 103 | printFunction(F); |
| 104 | FPConstantMap.clear(); |
| 105 | return false; |
| 106 | } |
Chris Lattner | 0e9f93e | 2002-08-31 00:29:16 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 108 | virtual bool doFinalization(Module &M) { |
Chris Lattner | 0e9f93e | 2002-08-31 00:29:16 +0000 | [diff] [blame] | 109 | // Free memory... |
Brian Gaeke | d9fb37a | 2003-07-24 20:20:44 +0000 | [diff] [blame] | 110 | delete Mang; |
Chris Lattner | 0e9f93e | 2002-08-31 00:29:16 +0000 | [diff] [blame] | 111 | TypeNames.clear(); |
Chris Lattner | cb3ad01 | 2004-05-09 20:41:32 +0000 | [diff] [blame] | 112 | return false; |
Chris Lattner | 0e9f93e | 2002-08-31 00:29:16 +0000 | [diff] [blame] | 113 | } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 114 | |
Chris Lattner | edd8ce1 | 2003-05-03 03:14:35 +0000 | [diff] [blame] | 115 | std::ostream &printType(std::ostream &Out, const Type *Ty, |
| 116 | const std::string &VariableName = "", |
Chris Lattner | 7635ea4 | 2003-11-03 01:01:59 +0000 | [diff] [blame] | 117 | bool IgnoreName = false); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 119 | void printStructReturnPointerFunctionType(std::ostream &Out, |
| 120 | const PointerType *Ty); |
| 121 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 122 | void writeOperand(Value *Operand); |
| 123 | void writeOperandInternal(Value *Operand); |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 124 | |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 125 | private : |
Chris Lattner | c242143 | 2004-05-09 04:30:20 +0000 | [diff] [blame] | 126 | void lowerIntrinsics(Function &F); |
Chris Lattner | 4ef5137 | 2004-02-14 00:31:10 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 128 | void printModule(Module *M); |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 129 | void printModuleTypes(const SymbolTable &ST); |
Chris Lattner | 2c601a7 | 2002-09-20 15:05:40 +0000 | [diff] [blame] | 130 | void printContainedStructs(const Type *Ty, std::set<const StructType *> &); |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 131 | void printFloatingPointConstants(Function &F); |
Chris Lattner | 4cda835 | 2002-08-20 16:55:48 +0000 | [diff] [blame] | 132 | void printFunctionSignature(const Function *F, bool Prototype); |
| 133 | |
Chris Lattner | 94f4f8e | 2004-02-13 23:00:29 +0000 | [diff] [blame] | 134 | void printFunction(Function &); |
Chris Lattner | cb3ad01 | 2004-05-09 20:41:32 +0000 | [diff] [blame] | 135 | void printBasicBlock(BasicBlock *BB); |
| 136 | void printLoop(Loop *L); |
Chris Lattner | b5af06a | 2002-05-09 03:06:06 +0000 | [diff] [blame] | 137 | |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 138 | void printConstant(Constant *CPV); |
| 139 | void printConstantArray(ConstantArray *CPA); |
Robert Bocchino | b78e838 | 2006-01-20 20:43:57 +0000 | [diff] [blame] | 140 | void printConstantPacked(ConstantPacked *CP); |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 141 | |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 142 | // isInlinableInst - Attempt to inline instructions into their uses to build |
| 143 | // trees as much as possible. To do this, we have to consistently decide |
| 144 | // what is acceptable to inline, so that variable declarations don't get |
| 145 | // printed and an extra copy of the expr is not emitted. |
| 146 | // |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 147 | static bool isInlinableInst(const Instruction &I) { |
Chris Lattner | 433e25a | 2004-05-20 20:25:50 +0000 | [diff] [blame] | 148 | // Always inline setcc instructions, even if they are shared by multiple |
| 149 | // expressions. GCC generates horrible code if we don't. |
| 150 | if (isa<SetCondInst>(I)) return true; |
| 151 | |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 152 | // Must be an expression, must be used exactly once. If it is dead, we |
| 153 | // emit it inline where it would go. |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 154 | if (I.getType() == Type::VoidTy || !I.hasOneUse() || |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 155 | isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I) || |
Andrew Lenharth | 558bc88 | 2005-06-18 18:34:52 +0000 | [diff] [blame] | 156 | isa<LoadInst>(I) || isa<VAArgInst>(I)) |
Chris Lattner | d4e8d31 | 2003-07-23 20:45:31 +0000 | [diff] [blame] | 157 | // Don't inline a load across a store or other bad things! |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 158 | return false; |
| 159 | |
| 160 | // Only inline instruction it it's use is in the same BB as the inst. |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 161 | return I.getParent() == cast<Instruction>(I.use_back())->getParent(); |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Chris Lattner | a8ab89e | 2003-06-17 04:39:14 +0000 | [diff] [blame] | 164 | // isDirectAlloca - Define fixed sized allocas in the entry block as direct |
| 165 | // variables which are accessed with the & operator. This causes GCC to |
| 166 | // generate significantly better code than to emit alloca calls directly. |
| 167 | // |
| 168 | static const AllocaInst *isDirectAlloca(const Value *V) { |
| 169 | const AllocaInst *AI = dyn_cast<AllocaInst>(V); |
| 170 | if (!AI) return false; |
| 171 | if (AI->isArrayAllocation()) |
| 172 | return 0; // FIXME: we can also inline fixed size array allocas! |
Chris Lattner | 02a3be0 | 2003-09-20 14:39:18 +0000 | [diff] [blame] | 173 | if (AI->getParent() != &AI->getParent()->getParent()->getEntryBlock()) |
Chris Lattner | a8ab89e | 2003-06-17 04:39:14 +0000 | [diff] [blame] | 174 | return 0; |
| 175 | return AI; |
| 176 | } |
| 177 | |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 178 | // Instruction visitation functions |
| 179 | friend class InstVisitor<CWriter>; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 180 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 181 | void visitReturnInst(ReturnInst &I); |
| 182 | void visitBranchInst(BranchInst &I); |
Chris Lattner | a9f5e05 | 2003-04-22 20:19:52 +0000 | [diff] [blame] | 183 | void visitSwitchInst(SwitchInst &I); |
Chris Lattner | cb3ad01 | 2004-05-09 20:41:32 +0000 | [diff] [blame] | 184 | void visitInvokeInst(InvokeInst &I) { |
| 185 | assert(0 && "Lowerinvoke pass didn't work!"); |
| 186 | } |
| 187 | |
| 188 | void visitUnwindInst(UnwindInst &I) { |
| 189 | assert(0 && "Lowerinvoke pass didn't work!"); |
| 190 | } |
Chris Lattner | a9d12c0 | 2004-10-16 18:12:13 +0000 | [diff] [blame] | 191 | void visitUnreachableInst(UnreachableInst &I); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 192 | |
Chris Lattner | 84e6665 | 2003-05-03 07:11:00 +0000 | [diff] [blame] | 193 | void visitPHINode(PHINode &I); |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 194 | void visitBinaryOperator(Instruction &I); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 195 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 196 | void visitCastInst (CastInst &I); |
Chris Lattner | 9f24a07 | 2004-03-12 05:52:14 +0000 | [diff] [blame] | 197 | void visitSelectInst(SelectInst &I); |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 198 | void visitCallInst (CallInst &I); |
| 199 | void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); } |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 200 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 201 | void visitMallocInst(MallocInst &I); |
| 202 | void visitAllocaInst(AllocaInst &I); |
| 203 | void visitFreeInst (FreeInst &I); |
| 204 | void visitLoadInst (LoadInst &I); |
| 205 | void visitStoreInst (StoreInst &I); |
| 206 | void visitGetElementPtrInst(GetElementPtrInst &I); |
Chris Lattner | 4d45bd0 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 207 | void visitVAArgInst (VAArgInst &I); |
Chris Lattner | 2f5f51a | 2002-05-09 03:28:37 +0000 | [diff] [blame] | 208 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 209 | void visitInstruction(Instruction &I) { |
Chris Lattner | e7f65d3b | 2002-06-30 16:07:20 +0000 | [diff] [blame] | 210 | std::cerr << "C Writer does not know about " << I; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 211 | abort(); |
| 212 | } |
| 213 | |
| 214 | void outputLValue(Instruction *I) { |
Brian Gaeke | d9fb37a | 2003-07-24 20:20:44 +0000 | [diff] [blame] | 215 | Out << " " << Mang->getValueName(I) << " = "; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 216 | } |
Chris Lattner | cb3ad01 | 2004-05-09 20:41:32 +0000 | [diff] [blame] | 217 | |
| 218 | bool isGotoCodeNecessary(BasicBlock *From, BasicBlock *To); |
John Criswell | 57bbfce | 2004-10-20 14:38:39 +0000 | [diff] [blame] | 219 | void printPHICopiesForSuccessor(BasicBlock *CurBlock, |
| 220 | BasicBlock *Successor, unsigned Indent); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 221 | void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock, |
| 222 | unsigned Indent); |
Chris Lattner | 23e9070 | 2003-11-25 20:49:55 +0000 | [diff] [blame] | 223 | void printIndexingExpression(Value *Ptr, gep_type_iterator I, |
| 224 | gep_type_iterator E); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 225 | }; |
Chris Lattner | 4ef5137 | 2004-02-14 00:31:10 +0000 | [diff] [blame] | 226 | } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 227 | |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 228 | /// This method inserts names for any unnamed structure types that are used by |
| 229 | /// the program, and removes names from structure types that are not used by the |
| 230 | /// program. |
| 231 | /// |
Chris Lattner | f9a0532 | 2006-02-13 22:22:42 +0000 | [diff] [blame] | 232 | bool CBackendNameAllUsedStructsAndMergeFunctions::runOnModule(Module &M) { |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 233 | // Get a set of types that are used by the program... |
| 234 | std::set<const Type *> UT = getAnalysis<FindUsedTypes>().getTypes(); |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 236 | // Loop over the module symbol table, removing types from UT that are |
Chris Lattner | dbf69f1 | 2005-03-08 16:19:59 +0000 | [diff] [blame] | 237 | // already named, and removing names for types that are not used. |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 238 | // |
| 239 | SymbolTable &MST = M.getSymbolTable(); |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 240 | for (SymbolTable::type_iterator TI = MST.type_begin(), TE = MST.type_end(); |
| 241 | TI != TE; ) { |
| 242 | SymbolTable::type_iterator I = TI++; |
Chris Lattner | dbf69f1 | 2005-03-08 16:19:59 +0000 | [diff] [blame] | 243 | |
| 244 | // If this is not used, remove it from the symbol table. |
| 245 | std::set<const Type *>::iterator UTI = UT.find(I->second); |
| 246 | if (UTI == UT.end()) |
| 247 | MST.remove(I); |
| 248 | else |
| 249 | UT.erase(UTI); // Only keep one name for this type. |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 250 | } |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 251 | |
| 252 | // UT now contains types that are not named. Loop over it, naming |
| 253 | // structure types. |
| 254 | // |
| 255 | bool Changed = false; |
Chris Lattner | 9d1af97 | 2004-05-28 05:47:27 +0000 | [diff] [blame] | 256 | unsigned RenameCounter = 0; |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 257 | for (std::set<const Type *>::const_iterator I = UT.begin(), E = UT.end(); |
| 258 | I != E; ++I) |
| 259 | if (const StructType *ST = dyn_cast<StructType>(*I)) { |
Chris Lattner | 9d1af97 | 2004-05-28 05:47:27 +0000 | [diff] [blame] | 260 | while (M.addTypeName("unnamed"+utostr(RenameCounter), ST)) |
| 261 | ++RenameCounter; |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 262 | Changed = true; |
| 263 | } |
Chris Lattner | f9a0532 | 2006-02-13 22:22:42 +0000 | [diff] [blame] | 264 | |
| 265 | |
| 266 | // Loop over all external functions and globals. If we have two with |
| 267 | // identical names, merge them. |
| 268 | // FIXME: This code should disappear when we don't allow values with the same |
| 269 | // names when they have different types! |
| 270 | std::map<std::string, GlobalValue*> ExtSymbols; |
| 271 | for (Module::iterator I = M.begin(), E = M.end(); I != E;) { |
| 272 | Function *GV = I++; |
| 273 | if (GV->isExternal() && GV->hasName()) { |
| 274 | std::pair<std::map<std::string, GlobalValue*>::iterator, bool> X |
| 275 | = ExtSymbols.insert(std::make_pair(GV->getName(), GV)); |
| 276 | if (!X.second) { |
| 277 | // Found a conflict, replace this global with the previous one. |
| 278 | GlobalValue *OldGV = X.first->second; |
| 279 | GV->replaceAllUsesWith(ConstantExpr::getCast(OldGV, GV->getType())); |
| 280 | GV->eraseFromParent(); |
| 281 | Changed = true; |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | // Do the same for globals. |
| 286 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 287 | I != E;) { |
| 288 | GlobalVariable *GV = I++; |
| 289 | if (GV->isExternal() && GV->hasName()) { |
| 290 | std::pair<std::map<std::string, GlobalValue*>::iterator, bool> X |
| 291 | = ExtSymbols.insert(std::make_pair(GV->getName(), GV)); |
| 292 | if (!X.second) { |
| 293 | // Found a conflict, replace this global with the previous one. |
| 294 | GlobalValue *OldGV = X.first->second; |
| 295 | GV->replaceAllUsesWith(ConstantExpr::getCast(OldGV, GV->getType())); |
| 296 | GV->eraseFromParent(); |
| 297 | Changed = true; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 302 | return Changed; |
| 303 | } |
| 304 | |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 305 | /// printStructReturnPointerFunctionType - This is like printType for a struct |
| 306 | /// return type, except, instead of printing the type as void (*)(Struct*, ...) |
| 307 | /// print it as "Struct (*)(...)", for struct return functions. |
| 308 | void CWriter::printStructReturnPointerFunctionType(std::ostream &Out, |
| 309 | const PointerType *TheTy) { |
| 310 | const FunctionType *FTy = cast<FunctionType>(TheTy->getElementType()); |
| 311 | std::stringstream FunctionInnards; |
| 312 | FunctionInnards << " (*) ("; |
| 313 | bool PrintedType = false; |
| 314 | |
| 315 | FunctionType::param_iterator I = FTy->param_begin(), E = FTy->param_end(); |
| 316 | const Type *RetTy = cast<PointerType>(I->get())->getElementType(); |
| 317 | for (++I; I != E; ++I) { |
| 318 | if (PrintedType) |
| 319 | FunctionInnards << ", "; |
| 320 | printType(FunctionInnards, *I, ""); |
| 321 | PrintedType = true; |
| 322 | } |
| 323 | if (FTy->isVarArg()) { |
| 324 | if (PrintedType) |
| 325 | FunctionInnards << ", ..."; |
| 326 | } else if (!PrintedType) { |
| 327 | FunctionInnards << "void"; |
| 328 | } |
| 329 | FunctionInnards << ')'; |
| 330 | std::string tstr = FunctionInnards.str(); |
| 331 | printType(Out, RetTy, tstr); |
| 332 | } |
| 333 | |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 334 | |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame] | 335 | // Pass the Type* and the variable name and this prints out the variable |
| 336 | // declaration. |
| 337 | // |
Chris Lattner | edd8ce1 | 2003-05-03 03:14:35 +0000 | [diff] [blame] | 338 | std::ostream &CWriter::printType(std::ostream &Out, const Type *Ty, |
| 339 | const std::string &NameSoFar, |
Chris Lattner | 7635ea4 | 2003-11-03 01:01:59 +0000 | [diff] [blame] | 340 | bool IgnoreName) { |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame] | 341 | if (Ty->isPrimitiveType()) |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 342 | switch (Ty->getTypeID()) { |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame] | 343 | case Type::VoidTyID: return Out << "void " << NameSoFar; |
| 344 | case Type::BoolTyID: return Out << "bool " << NameSoFar; |
| 345 | case Type::UByteTyID: return Out << "unsigned char " << NameSoFar; |
| 346 | case Type::SByteTyID: return Out << "signed char " << NameSoFar; |
| 347 | case Type::UShortTyID: return Out << "unsigned short " << NameSoFar; |
| 348 | case Type::ShortTyID: return Out << "short " << NameSoFar; |
| 349 | case Type::UIntTyID: return Out << "unsigned " << NameSoFar; |
| 350 | case Type::IntTyID: return Out << "int " << NameSoFar; |
| 351 | case Type::ULongTyID: return Out << "unsigned long long " << NameSoFar; |
| 352 | case Type::LongTyID: return Out << "signed long long " << NameSoFar; |
| 353 | case Type::FloatTyID: return Out << "float " << NameSoFar; |
| 354 | case Type::DoubleTyID: return Out << "double " << NameSoFar; |
| 355 | default : |
Chris Lattner | 76e2df2 | 2004-07-15 02:14:30 +0000 | [diff] [blame] | 356 | std::cerr << "Unknown primitive type: " << *Ty << "\n"; |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame] | 357 | abort(); |
| 358 | } |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 359 | |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame] | 360 | // Check to see if the type is named. |
Chris Lattner | 04b72c8 | 2002-10-16 00:08:22 +0000 | [diff] [blame] | 361 | if (!IgnoreName || isa<OpaqueType>(Ty)) { |
Chris Lattner | edd8ce1 | 2003-05-03 03:14:35 +0000 | [diff] [blame] | 362 | std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty); |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 363 | if (I != TypeNames.end()) return Out << I->second << ' ' << NameSoFar; |
Chris Lattner | 04b72c8 | 2002-10-16 00:08:22 +0000 | [diff] [blame] | 364 | } |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame] | 365 | |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 366 | switch (Ty->getTypeID()) { |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame] | 367 | case Type::FunctionTyID: { |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 368 | const FunctionType *FTy = cast<FunctionType>(Ty); |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 369 | std::stringstream FunctionInnards; |
Joel Stanley | 54f6032 | 2003-06-25 04:52:09 +0000 | [diff] [blame] | 370 | FunctionInnards << " (" << NameSoFar << ") ("; |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 371 | for (FunctionType::param_iterator I = FTy->param_begin(), |
| 372 | E = FTy->param_end(); I != E; ++I) { |
| 373 | if (I != FTy->param_begin()) |
Joel Stanley | 54f6032 | 2003-06-25 04:52:09 +0000 | [diff] [blame] | 374 | FunctionInnards << ", "; |
| 375 | printType(FunctionInnards, *I, ""); |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame] | 376 | } |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 377 | if (FTy->isVarArg()) { |
| 378 | if (FTy->getNumParams()) |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 379 | FunctionInnards << ", ..."; |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 380 | } else if (!FTy->getNumParams()) { |
Joel Stanley | 54f6032 | 2003-06-25 04:52:09 +0000 | [diff] [blame] | 381 | FunctionInnards << "void"; |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame] | 382 | } |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 383 | FunctionInnards << ')'; |
Joel Stanley | 54f6032 | 2003-06-25 04:52:09 +0000 | [diff] [blame] | 384 | std::string tstr = FunctionInnards.str(); |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 385 | printType(Out, FTy->getReturnType(), tstr); |
Nick Hildenbrandt | 9850237 | 2002-11-18 20:55:50 +0000 | [diff] [blame] | 386 | return Out; |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame] | 387 | } |
| 388 | case Type::StructTyID: { |
| 389 | const StructType *STy = cast<StructType>(Ty); |
| 390 | Out << NameSoFar + " {\n"; |
| 391 | unsigned Idx = 0; |
Chris Lattner | d21cd80 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 392 | for (StructType::element_iterator I = STy->element_begin(), |
| 393 | E = STy->element_end(); I != E; ++I) { |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame] | 394 | Out << " "; |
Nick Hildenbrandt | 9850237 | 2002-11-18 20:55:50 +0000 | [diff] [blame] | 395 | printType(Out, *I, "field" + utostr(Idx++)); |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame] | 396 | Out << ";\n"; |
| 397 | } |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 398 | return Out << '}'; |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 399 | } |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame] | 400 | |
| 401 | case Type::PointerTyID: { |
| 402 | const PointerType *PTy = cast<PointerType>(Ty); |
Vikram S. Adve | 969c4ad | 2002-08-25 20:00:08 +0000 | [diff] [blame] | 403 | std::string ptrName = "*" + NameSoFar; |
| 404 | |
Robert Bocchino | b78e838 | 2006-01-20 20:43:57 +0000 | [diff] [blame] | 405 | if (isa<ArrayType>(PTy->getElementType()) || |
| 406 | isa<PackedType>(PTy->getElementType())) |
Chris Lattner | 8917d36 | 2003-11-03 04:31:54 +0000 | [diff] [blame] | 407 | ptrName = "(" + ptrName + ")"; |
Vikram S. Adve | 969c4ad | 2002-08-25 20:00:08 +0000 | [diff] [blame] | 408 | |
Nick Hildenbrandt | 9850237 | 2002-11-18 20:55:50 +0000 | [diff] [blame] | 409 | return printType(Out, PTy->getElementType(), ptrName); |
Misha Brukman | 8f9f9a2 | 2003-07-21 16:34:35 +0000 | [diff] [blame] | 410 | } |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame] | 411 | |
| 412 | case Type::ArrayTyID: { |
| 413 | const ArrayType *ATy = cast<ArrayType>(Ty); |
| 414 | unsigned NumElements = ATy->getNumElements(); |
Chris Lattner | 3e3b6f7 | 2004-12-15 23:13:15 +0000 | [diff] [blame] | 415 | if (NumElements == 0) NumElements = 1; |
Nick Hildenbrandt | 9850237 | 2002-11-18 20:55:50 +0000 | [diff] [blame] | 416 | return printType(Out, ATy->getElementType(), |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame] | 417 | NameSoFar + "[" + utostr(NumElements) + "]"); |
| 418 | } |
Chris Lattner | 04b72c8 | 2002-10-16 00:08:22 +0000 | [diff] [blame] | 419 | |
Robert Bocchino | b78e838 | 2006-01-20 20:43:57 +0000 | [diff] [blame] | 420 | case Type::PackedTyID: { |
| 421 | const PackedType *PTy = cast<PackedType>(Ty); |
| 422 | unsigned NumElements = PTy->getNumElements(); |
| 423 | if (NumElements == 0) NumElements = 1; |
| 424 | return printType(Out, PTy->getElementType(), |
| 425 | NameSoFar + "[" + utostr(NumElements) + "]"); |
| 426 | } |
| 427 | |
Chris Lattner | 04b72c8 | 2002-10-16 00:08:22 +0000 | [diff] [blame] | 428 | case Type::OpaqueTyID: { |
| 429 | static int Count = 0; |
Chris Lattner | edd8ce1 | 2003-05-03 03:14:35 +0000 | [diff] [blame] | 430 | std::string TyName = "struct opaque_" + itostr(Count++); |
Chris Lattner | 04b72c8 | 2002-10-16 00:08:22 +0000 | [diff] [blame] | 431 | assert(TypeNames.find(Ty) == TypeNames.end()); |
| 432 | TypeNames[Ty] = TyName; |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 433 | return Out << TyName << ' ' << NameSoFar; |
Chris Lattner | 04b72c8 | 2002-10-16 00:08:22 +0000 | [diff] [blame] | 434 | } |
Chris Lattner | 83c5775 | 2002-08-19 22:17:53 +0000 | [diff] [blame] | 435 | default: |
| 436 | assert(0 && "Unhandled case in getTypeProps!"); |
| 437 | abort(); |
| 438 | } |
| 439 | |
| 440 | return Out; |
| 441 | } |
| 442 | |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 443 | void CWriter::printConstantArray(ConstantArray *CPA) { |
| 444 | |
| 445 | // As a special case, print the array as a string if it is an array of |
| 446 | // ubytes or an array of sbytes with positive values. |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 447 | // |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 448 | const Type *ETy = CPA->getType()->getElementType(); |
| 449 | bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy); |
| 450 | |
| 451 | // Make sure the last character is a null char, as automatically added by C |
Chris Lattner | 02da6c0 | 2003-06-16 12:09:09 +0000 | [diff] [blame] | 452 | if (isString && (CPA->getNumOperands() == 0 || |
| 453 | !cast<Constant>(*(CPA->op_end()-1))->isNullValue())) |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 454 | isString = false; |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 455 | |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 456 | if (isString) { |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 457 | Out << '\"'; |
Chris Lattner | 02da6c0 | 2003-06-16 12:09:09 +0000 | [diff] [blame] | 458 | // Keep track of whether the last number was a hexadecimal escape |
| 459 | bool LastWasHex = false; |
| 460 | |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 461 | // Do not include the last character, which we know is null |
| 462 | for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) { |
Chris Lattner | c07736a | 2003-07-23 15:22:26 +0000 | [diff] [blame] | 463 | unsigned char C = cast<ConstantInt>(CPA->getOperand(i))->getRawValue(); |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 464 | |
Chris Lattner | 02da6c0 | 2003-06-16 12:09:09 +0000 | [diff] [blame] | 465 | // Print it out literally if it is a printable character. The only thing |
| 466 | // to be careful about is when the last letter output was a hex escape |
| 467 | // code, in which case we have to be careful not to print out hex digits |
Chris Lattner | da92090 | 2003-06-16 12:21:19 +0000 | [diff] [blame] | 468 | // explicitly (the C compiler thinks it is a continuation of the previous |
| 469 | // character, sheesh...) |
Chris Lattner | 02da6c0 | 2003-06-16 12:09:09 +0000 | [diff] [blame] | 470 | // |
| 471 | if (isprint(C) && (!LastWasHex || !isxdigit(C))) { |
| 472 | LastWasHex = false; |
Nick Hildenbrandt | 088b472 | 2002-11-06 21:40:23 +0000 | [diff] [blame] | 473 | if (C == '"' || C == '\\') |
| 474 | Out << "\\" << C; |
Nick Hildenbrandt | c3dd2af | 2002-09-30 21:11:55 +0000 | [diff] [blame] | 475 | else |
| 476 | Out << C; |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 477 | } else { |
Chris Lattner | 02da6c0 | 2003-06-16 12:09:09 +0000 | [diff] [blame] | 478 | LastWasHex = false; |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 479 | switch (C) { |
| 480 | case '\n': Out << "\\n"; break; |
| 481 | case '\t': Out << "\\t"; break; |
| 482 | case '\r': Out << "\\r"; break; |
| 483 | case '\v': Out << "\\v"; break; |
| 484 | case '\a': Out << "\\a"; break; |
Nick Hildenbrandt | c3dd2af | 2002-09-30 21:11:55 +0000 | [diff] [blame] | 485 | case '\"': Out << "\\\""; break; |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 486 | case '\'': Out << "\\\'"; break; |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 487 | default: |
| 488 | Out << "\\x"; |
Chris Lattner | 02da6c0 | 2003-06-16 12:09:09 +0000 | [diff] [blame] | 489 | Out << (char)(( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A')); |
| 490 | Out << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A')); |
| 491 | LastWasHex = true; |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 492 | break; |
| 493 | } |
| 494 | } |
| 495 | } |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 496 | Out << '\"'; |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 497 | } else { |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 498 | Out << '{'; |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 499 | if (CPA->getNumOperands()) { |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 500 | Out << ' '; |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 501 | printConstant(cast<Constant>(CPA->getOperand(0))); |
| 502 | for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) { |
| 503 | Out << ", "; |
| 504 | printConstant(cast<Constant>(CPA->getOperand(i))); |
| 505 | } |
| 506 | } |
| 507 | Out << " }"; |
| 508 | } |
| 509 | } |
| 510 | |
Robert Bocchino | b78e838 | 2006-01-20 20:43:57 +0000 | [diff] [blame] | 511 | void CWriter::printConstantPacked(ConstantPacked *CP) { |
| 512 | Out << '{'; |
| 513 | if (CP->getNumOperands()) { |
| 514 | Out << ' '; |
| 515 | printConstant(cast<Constant>(CP->getOperand(0))); |
| 516 | for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) { |
| 517 | Out << ", "; |
| 518 | printConstant(cast<Constant>(CP->getOperand(i))); |
| 519 | } |
| 520 | } |
| 521 | Out << " }"; |
| 522 | } |
| 523 | |
Chris Lattner | dd76aca | 2003-10-05 00:40:51 +0000 | [diff] [blame] | 524 | // isFPCSafeToPrint - Returns true if we may assume that CFP may be written out |
| 525 | // textually as a double (rather than as a reference to a stack-allocated |
| 526 | // variable). We decide this by converting CFP to a string and back into a |
| 527 | // double, and then checking whether the conversion results in a bit-equal |
| 528 | // double to the original value of CFP. This depends on us and the target C |
| 529 | // compiler agreeing on the conversion process (which is pretty likely since we |
| 530 | // only deal in IEEE FP). |
| 531 | // |
Chris Lattner | cb3ad01 | 2004-05-09 20:41:32 +0000 | [diff] [blame] | 532 | static bool isFPCSafeToPrint(const ConstantFP *CFP) { |
Chris Lattner | 4946a8c | 2003-10-13 17:13:53 +0000 | [diff] [blame] | 533 | #if HAVE_PRINTF_A |
Chris Lattner | 5ef6ffd | 2003-10-12 08:12:58 +0000 | [diff] [blame] | 534 | char Buffer[100]; |
| 535 | sprintf(Buffer, "%a", CFP->getValue()); |
| 536 | |
| 537 | if (!strncmp(Buffer, "0x", 2) || |
| 538 | !strncmp(Buffer, "-0x", 3) || |
| 539 | !strncmp(Buffer, "+0x", 3)) |
| 540 | return atof(Buffer) == CFP->getValue(); |
| 541 | return false; |
| 542 | #else |
Brian Gaeke | b471a23 | 2003-06-17 23:55:35 +0000 | [diff] [blame] | 543 | std::string StrVal = ftostr(CFP->getValue()); |
Chris Lattner | 9860e77 | 2003-10-12 04:36:29 +0000 | [diff] [blame] | 544 | |
| 545 | while (StrVal[0] == ' ') |
| 546 | StrVal.erase(StrVal.begin()); |
| 547 | |
Chris Lattner | dd76aca | 2003-10-05 00:40:51 +0000 | [diff] [blame] | 548 | // Check to make sure that the stringized number is not some string like "Inf" |
| 549 | // or NaN. Check that the string matches the "[-+]?[0-9]" regex. |
Brian Gaeke | b471a23 | 2003-06-17 23:55:35 +0000 | [diff] [blame] | 550 | if ((StrVal[0] >= '0' && StrVal[0] <= '9') || |
| 551 | ((StrVal[0] == '-' || StrVal[0] == '+') && |
| 552 | (StrVal[1] >= '0' && StrVal[1] <= '9'))) |
| 553 | // Reparse stringized version! |
Chris Lattner | dd76aca | 2003-10-05 00:40:51 +0000 | [diff] [blame] | 554 | return atof(StrVal.c_str()) == CFP->getValue(); |
Brian Gaeke | b471a23 | 2003-06-17 23:55:35 +0000 | [diff] [blame] | 555 | return false; |
Chris Lattner | 5ef6ffd | 2003-10-12 08:12:58 +0000 | [diff] [blame] | 556 | #endif |
Brian Gaeke | b471a23 | 2003-06-17 23:55:35 +0000 | [diff] [blame] | 557 | } |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 558 | |
| 559 | // printConstant - The LLVM Constant to C Constant converter. |
| 560 | void CWriter::printConstant(Constant *CPV) { |
| 561 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) { |
| 562 | switch (CE->getOpcode()) { |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 563 | case Instruction::Cast: |
| 564 | Out << "(("; |
Nick Hildenbrandt | 9850237 | 2002-11-18 20:55:50 +0000 | [diff] [blame] | 565 | printType(Out, CPV->getType()); |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 566 | Out << ')'; |
Chris Lattner | 84c0d5e | 2003-05-14 17:50:19 +0000 | [diff] [blame] | 567 | printConstant(CE->getOperand(0)); |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 568 | Out << ')'; |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 569 | return; |
| 570 | |
| 571 | case Instruction::GetElementPtr: |
Nick Hildenbrandt | d2eb386 | 2002-10-03 20:47:24 +0000 | [diff] [blame] | 572 | Out << "(&("; |
Chris Lattner | 23e9070 | 2003-11-25 20:49:55 +0000 | [diff] [blame] | 573 | printIndexingExpression(CE->getOperand(0), gep_type_begin(CPV), |
| 574 | gep_type_end(CPV)); |
Nick Hildenbrandt | d2eb386 | 2002-10-03 20:47:24 +0000 | [diff] [blame] | 575 | Out << "))"; |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 576 | return; |
Chris Lattner | 6683dbf | 2004-04-01 05:28:26 +0000 | [diff] [blame] | 577 | case Instruction::Select: |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 578 | Out << '('; |
Chris Lattner | 6683dbf | 2004-04-01 05:28:26 +0000 | [diff] [blame] | 579 | printConstant(CE->getOperand(0)); |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 580 | Out << '?'; |
Chris Lattner | 6683dbf | 2004-04-01 05:28:26 +0000 | [diff] [blame] | 581 | printConstant(CE->getOperand(1)); |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 582 | Out << ':'; |
Chris Lattner | 6683dbf | 2004-04-01 05:28:26 +0000 | [diff] [blame] | 583 | printConstant(CE->getOperand(2)); |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 584 | Out << ')'; |
Chris Lattner | 6683dbf | 2004-04-01 05:28:26 +0000 | [diff] [blame] | 585 | return; |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 586 | case Instruction::Add: |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 587 | case Instruction::Sub: |
Chris Lattner | 2925592 | 2003-08-14 19:19:53 +0000 | [diff] [blame] | 588 | case Instruction::Mul: |
| 589 | case Instruction::Div: |
| 590 | case Instruction::Rem: |
Chris Lattner | f376e5e | 2004-12-29 04:00:09 +0000 | [diff] [blame] | 591 | case Instruction::And: |
| 592 | case Instruction::Or: |
| 593 | case Instruction::Xor: |
Chris Lattner | 2925592 | 2003-08-14 19:19:53 +0000 | [diff] [blame] | 594 | case Instruction::SetEQ: |
| 595 | case Instruction::SetNE: |
| 596 | case Instruction::SetLT: |
| 597 | case Instruction::SetLE: |
| 598 | case Instruction::SetGT: |
| 599 | case Instruction::SetGE: |
Brian Gaeke | 0415b6c | 2003-11-22 05:02:56 +0000 | [diff] [blame] | 600 | case Instruction::Shl: |
| 601 | case Instruction::Shr: |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 602 | Out << '('; |
Chris Lattner | 84c0d5e | 2003-05-14 17:50:19 +0000 | [diff] [blame] | 603 | printConstant(CE->getOperand(0)); |
Chris Lattner | 2925592 | 2003-08-14 19:19:53 +0000 | [diff] [blame] | 604 | switch (CE->getOpcode()) { |
| 605 | case Instruction::Add: Out << " + "; break; |
| 606 | case Instruction::Sub: Out << " - "; break; |
| 607 | case Instruction::Mul: Out << " * "; break; |
| 608 | case Instruction::Div: Out << " / "; break; |
| 609 | case Instruction::Rem: Out << " % "; break; |
Chris Lattner | f376e5e | 2004-12-29 04:00:09 +0000 | [diff] [blame] | 610 | case Instruction::And: Out << " & "; break; |
| 611 | case Instruction::Or: Out << " | "; break; |
| 612 | case Instruction::Xor: Out << " ^ "; break; |
Chris Lattner | 2925592 | 2003-08-14 19:19:53 +0000 | [diff] [blame] | 613 | case Instruction::SetEQ: Out << " == "; break; |
| 614 | case Instruction::SetNE: Out << " != "; break; |
| 615 | case Instruction::SetLT: Out << " < "; break; |
| 616 | case Instruction::SetLE: Out << " <= "; break; |
| 617 | case Instruction::SetGT: Out << " > "; break; |
| 618 | case Instruction::SetGE: Out << " >= "; break; |
Brian Gaeke | 0415b6c | 2003-11-22 05:02:56 +0000 | [diff] [blame] | 619 | case Instruction::Shl: Out << " << "; break; |
| 620 | case Instruction::Shr: Out << " >> "; break; |
Chris Lattner | 2925592 | 2003-08-14 19:19:53 +0000 | [diff] [blame] | 621 | default: assert(0 && "Illegal opcode here!"); |
| 622 | } |
Chris Lattner | 84c0d5e | 2003-05-14 17:50:19 +0000 | [diff] [blame] | 623 | printConstant(CE->getOperand(1)); |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 624 | Out << ')'; |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 625 | return; |
| 626 | |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 627 | default: |
| 628 | std::cerr << "CWriter Error: Unhandled constant expression: " |
Chris Lattner | 76e2df2 | 2004-07-15 02:14:30 +0000 | [diff] [blame] | 629 | << *CE << "\n"; |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 630 | abort(); |
| 631 | } |
Chris Lattner | a9d12c0 | 2004-10-16 18:12:13 +0000 | [diff] [blame] | 632 | } else if (isa<UndefValue>(CPV) && CPV->getType()->isFirstClassType()) { |
Chris Lattner | 665825e | 2004-10-17 17:48:59 +0000 | [diff] [blame] | 633 | Out << "(("; |
| 634 | printType(Out, CPV->getType()); |
| 635 | Out << ")/*UNDEF*/0)"; |
Chris Lattner | a9d12c0 | 2004-10-16 18:12:13 +0000 | [diff] [blame] | 636 | return; |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 639 | switch (CPV->getType()->getTypeID()) { |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 640 | case Type::BoolTyID: |
Chris Lattner | 33ce777 | 2006-09-28 23:19:29 +0000 | [diff] [blame^] | 641 | Out << (cast<ConstantBool>(CPV)->getValue() ? '1' : '0'); |
| 642 | break; |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 643 | case Type::SByteTyID: |
| 644 | case Type::ShortTyID: |
Chris Lattner | 33ce777 | 2006-09-28 23:19:29 +0000 | [diff] [blame^] | 645 | Out << cast<ConstantSInt>(CPV)->getValue(); |
| 646 | break; |
Chris Lattner | 45343ea | 2003-05-12 15:39:31 +0000 | [diff] [blame] | 647 | case Type::IntTyID: |
| 648 | if ((int)cast<ConstantSInt>(CPV)->getValue() == (int)0x80000000) |
Chris Lattner | 40e7c35 | 2004-11-30 21:33:58 +0000 | [diff] [blame] | 649 | Out << "((int)0x80000000U)"; // Handle MININT specially to avoid warning |
Chris Lattner | 45343ea | 2003-05-12 15:39:31 +0000 | [diff] [blame] | 650 | else |
| 651 | Out << cast<ConstantSInt>(CPV)->getValue(); |
| 652 | break; |
| 653 | |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 654 | case Type::LongTyID: |
Chris Lattner | 40e7c35 | 2004-11-30 21:33:58 +0000 | [diff] [blame] | 655 | if (cast<ConstantSInt>(CPV)->isMinValue()) |
| 656 | Out << "(/*INT64_MIN*/(-9223372036854775807LL)-1)"; |
| 657 | else |
Chris Lattner | 33ce777 | 2006-09-28 23:19:29 +0000 | [diff] [blame^] | 658 | Out << cast<ConstantSInt>(CPV)->getValue() << "ll"; |
| 659 | break; |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 660 | |
| 661 | case Type::UByteTyID: |
| 662 | case Type::UShortTyID: |
Chris Lattner | 33ce777 | 2006-09-28 23:19:29 +0000 | [diff] [blame^] | 663 | Out << cast<ConstantUInt>(CPV)->getValue(); |
| 664 | break; |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 665 | case Type::UIntTyID: |
Chris Lattner | 33ce777 | 2006-09-28 23:19:29 +0000 | [diff] [blame^] | 666 | Out << cast<ConstantUInt>(CPV)->getValue() << 'u'; |
| 667 | break; |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 668 | case Type::ULongTyID: |
Chris Lattner | 33ce777 | 2006-09-28 23:19:29 +0000 | [diff] [blame^] | 669 | Out << cast<ConstantUInt>(CPV)->getValue() << "ull"; |
| 670 | break; |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 671 | |
| 672 | case Type::FloatTyID: |
Chris Lattner | d1cf1b4 | 2002-09-20 23:26:33 +0000 | [diff] [blame] | 673 | case Type::DoubleTyID: { |
| 674 | ConstantFP *FPC = cast<ConstantFP>(CPV); |
Chris Lattner | edd8ce1 | 2003-05-03 03:14:35 +0000 | [diff] [blame] | 675 | std::map<const ConstantFP*, unsigned>::iterator I = FPConstantMap.find(FPC); |
Chris Lattner | d1cf1b4 | 2002-09-20 23:26:33 +0000 | [diff] [blame] | 676 | if (I != FPConstantMap.end()) { |
| 677 | // Because of FP precision problems we must load from a stack allocated |
| 678 | // value that holds the value in hex. |
| 679 | Out << "(*(" << (FPC->getType() == Type::FloatTy ? "float" : "double") |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 680 | << "*)&FPConstant" << I->second << ')'; |
Chris Lattner | d1cf1b4 | 2002-09-20 23:26:33 +0000 | [diff] [blame] | 681 | } else { |
Brian Gaeke | 043c0bb | 2004-07-21 03:15:26 +0000 | [diff] [blame] | 682 | if (IsNAN(FPC->getValue())) { |
| 683 | // The value is NaN |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 684 | |
Brian Gaeke | 8a702e8 | 2004-08-25 19:00:42 +0000 | [diff] [blame] | 685 | // The prefix for a quiet NaN is 0x7FF8. For a signalling NaN, |
| 686 | // it's 0x7ff4. |
| 687 | const unsigned long QuietNaN = 0x7ff8UL; |
| 688 | const unsigned long SignalNaN = 0x7ff4UL; |
| 689 | |
| 690 | // We need to grab the first part of the FP # |
Brian Gaeke | 8a702e8 | 2004-08-25 19:00:42 +0000 | [diff] [blame] | 691 | char Buffer[100]; |
| 692 | |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 693 | uint64_t ll = DoubleToBits(FPC->getValue()); |
Reid Spencer | 2bc320d | 2006-05-31 22:26:11 +0000 | [diff] [blame] | 694 | sprintf(Buffer, "0x%llx", static_cast<long long>(ll)); |
Brian Gaeke | 8a702e8 | 2004-08-25 19:00:42 +0000 | [diff] [blame] | 695 | |
| 696 | std::string Num(&Buffer[0], &Buffer[6]); |
| 697 | unsigned long Val = strtoul(Num.c_str(), 0, 16); |
| 698 | |
Brian Gaeke | 043c0bb | 2004-07-21 03:15:26 +0000 | [diff] [blame] | 699 | if (FPC->getType() == Type::FloatTy) |
Brian Gaeke | 8a702e8 | 2004-08-25 19:00:42 +0000 | [diff] [blame] | 700 | Out << "LLVM_NAN" << (Val == QuietNaN ? "" : "S") << "F(\"" |
| 701 | << Buffer << "\") /*nan*/ "; |
Brian Gaeke | 043c0bb | 2004-07-21 03:15:26 +0000 | [diff] [blame] | 702 | else |
Brian Gaeke | 8a702e8 | 2004-08-25 19:00:42 +0000 | [diff] [blame] | 703 | Out << "LLVM_NAN" << (Val == QuietNaN ? "" : "S") << "(\"" |
| 704 | << Buffer << "\") /*nan*/ "; |
Brian Gaeke | 043c0bb | 2004-07-21 03:15:26 +0000 | [diff] [blame] | 705 | } else if (IsInf(FPC->getValue())) { |
| 706 | // The value is Inf |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 707 | if (FPC->getValue() < 0) Out << '-'; |
Brian Gaeke | 8a702e8 | 2004-08-25 19:00:42 +0000 | [diff] [blame] | 708 | Out << "LLVM_INF" << (FPC->getType() == Type::FloatTy ? "F" : "") |
| 709 | << " /*inf*/ "; |
Brian Gaeke | 043c0bb | 2004-07-21 03:15:26 +0000 | [diff] [blame] | 710 | } else { |
Brian Gaeke | 07b52b3 | 2004-08-25 19:37:26 +0000 | [diff] [blame] | 711 | std::string Num; |
| 712 | #if HAVE_PRINTF_A |
| 713 | // Print out the constant as a floating point number. |
| 714 | char Buffer[100]; |
| 715 | sprintf(Buffer, "%a", FPC->getValue()); |
| 716 | Num = Buffer; |
| 717 | #else |
| 718 | Num = ftostr(FPC->getValue()); |
| 719 | #endif |
Brian Gaeke | 043c0bb | 2004-07-21 03:15:26 +0000 | [diff] [blame] | 720 | Out << Num; |
| 721 | } |
Chris Lattner | d1cf1b4 | 2002-09-20 23:26:33 +0000 | [diff] [blame] | 722 | } |
| 723 | break; |
| 724 | } |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 725 | |
| 726 | case Type::ArrayTyID: |
Chris Lattner | a9d12c0 | 2004-10-16 18:12:13 +0000 | [diff] [blame] | 727 | if (isa<ConstantAggregateZero>(CPV) || isa<UndefValue>(CPV)) { |
Chris Lattner | cfb0fd2 | 2004-02-15 05:54:27 +0000 | [diff] [blame] | 728 | const ArrayType *AT = cast<ArrayType>(CPV->getType()); |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 729 | Out << '{'; |
Chris Lattner | cfb0fd2 | 2004-02-15 05:54:27 +0000 | [diff] [blame] | 730 | if (AT->getNumElements()) { |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 731 | Out << ' '; |
Chris Lattner | cfb0fd2 | 2004-02-15 05:54:27 +0000 | [diff] [blame] | 732 | Constant *CZ = Constant::getNullValue(AT->getElementType()); |
| 733 | printConstant(CZ); |
| 734 | for (unsigned i = 1, e = AT->getNumElements(); i != e; ++i) { |
| 735 | Out << ", "; |
| 736 | printConstant(CZ); |
| 737 | } |
| 738 | } |
| 739 | Out << " }"; |
| 740 | } else { |
| 741 | printConstantArray(cast<ConstantArray>(CPV)); |
| 742 | } |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 743 | break; |
| 744 | |
Robert Bocchino | b78e838 | 2006-01-20 20:43:57 +0000 | [diff] [blame] | 745 | case Type::PackedTyID: |
| 746 | if (isa<ConstantAggregateZero>(CPV) || isa<UndefValue>(CPV)) { |
| 747 | const PackedType *AT = cast<PackedType>(CPV->getType()); |
| 748 | Out << '{'; |
| 749 | if (AT->getNumElements()) { |
| 750 | Out << ' '; |
| 751 | Constant *CZ = Constant::getNullValue(AT->getElementType()); |
| 752 | printConstant(CZ); |
| 753 | for (unsigned i = 1, e = AT->getNumElements(); i != e; ++i) { |
| 754 | Out << ", "; |
| 755 | printConstant(CZ); |
| 756 | } |
| 757 | } |
| 758 | Out << " }"; |
| 759 | } else { |
| 760 | printConstantPacked(cast<ConstantPacked>(CPV)); |
| 761 | } |
| 762 | break; |
| 763 | |
Chris Lattner | cfb0fd2 | 2004-02-15 05:54:27 +0000 | [diff] [blame] | 764 | case Type::StructTyID: |
Chris Lattner | a9d12c0 | 2004-10-16 18:12:13 +0000 | [diff] [blame] | 765 | if (isa<ConstantAggregateZero>(CPV) || isa<UndefValue>(CPV)) { |
Chris Lattner | cfb0fd2 | 2004-02-15 05:54:27 +0000 | [diff] [blame] | 766 | const StructType *ST = cast<StructType>(CPV->getType()); |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 767 | Out << '{'; |
Chris Lattner | cfb0fd2 | 2004-02-15 05:54:27 +0000 | [diff] [blame] | 768 | if (ST->getNumElements()) { |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 769 | Out << ' '; |
Chris Lattner | cfb0fd2 | 2004-02-15 05:54:27 +0000 | [diff] [blame] | 770 | printConstant(Constant::getNullValue(ST->getElementType(0))); |
| 771 | for (unsigned i = 1, e = ST->getNumElements(); i != e; ++i) { |
| 772 | Out << ", "; |
| 773 | printConstant(Constant::getNullValue(ST->getElementType(i))); |
| 774 | } |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 775 | } |
Chris Lattner | cfb0fd2 | 2004-02-15 05:54:27 +0000 | [diff] [blame] | 776 | Out << " }"; |
| 777 | } else { |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 778 | Out << '{'; |
Chris Lattner | cfb0fd2 | 2004-02-15 05:54:27 +0000 | [diff] [blame] | 779 | if (CPV->getNumOperands()) { |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 780 | Out << ' '; |
Chris Lattner | cfb0fd2 | 2004-02-15 05:54:27 +0000 | [diff] [blame] | 781 | printConstant(cast<Constant>(CPV->getOperand(0))); |
| 782 | for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) { |
| 783 | Out << ", "; |
| 784 | printConstant(cast<Constant>(CPV->getOperand(i))); |
| 785 | } |
| 786 | } |
| 787 | Out << " }"; |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 788 | } |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 789 | break; |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 790 | |
| 791 | case Type::PointerTyID: |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 792 | if (isa<ConstantPointerNull>(CPV)) { |
Chris Lattner | cf135cb | 2003-06-02 03:10:53 +0000 | [diff] [blame] | 793 | Out << "(("; |
| 794 | printType(Out, CPV->getType()); |
| 795 | Out << ")/*NULL*/0)"; |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 796 | break; |
Reid Spencer | 518310c | 2004-07-18 00:44:37 +0000 | [diff] [blame] | 797 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(CPV)) { |
| 798 | writeOperand(GV); |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 799 | break; |
| 800 | } |
| 801 | // FALL THROUGH |
| 802 | default: |
Chris Lattner | 76e2df2 | 2004-07-15 02:14:30 +0000 | [diff] [blame] | 803 | std::cerr << "Unknown constant type: " << *CPV << "\n"; |
Chris Lattner | 6d49292 | 2002-08-19 21:32:41 +0000 | [diff] [blame] | 804 | abort(); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | void CWriter::writeOperandInternal(Value *Operand) { |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 809 | if (Instruction *I = dyn_cast<Instruction>(Operand)) |
Chris Lattner | a8ab89e | 2003-06-17 04:39:14 +0000 | [diff] [blame] | 810 | if (isInlinableInst(*I) && !isDirectAlloca(I)) { |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 811 | // Should we inline this instruction to build a tree? |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 812 | Out << '('; |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 813 | visit(*I); |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 814 | Out << ')'; |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 815 | return; |
| 816 | } |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 817 | |
Reid Spencer | 518310c | 2004-07-18 00:44:37 +0000 | [diff] [blame] | 818 | Constant* CPV = dyn_cast<Constant>(Operand); |
| 819 | if (CPV && !isa<GlobalValue>(CPV)) { |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 820 | printConstant(CPV); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 821 | } else { |
Brian Gaeke | d9fb37a | 2003-07-24 20:20:44 +0000 | [diff] [blame] | 822 | Out << Mang->getValueName(Operand); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 823 | } |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 826 | void CWriter::writeOperand(Value *Operand) { |
Chris Lattner | a8ab89e | 2003-06-17 04:39:14 +0000 | [diff] [blame] | 827 | if (isa<GlobalVariable>(Operand) || isDirectAlloca(Operand)) |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 828 | Out << "(&"; // Global variables are references as their addresses by llvm |
| 829 | |
| 830 | writeOperandInternal(Operand); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 831 | |
Chris Lattner | a8ab89e | 2003-06-17 04:39:14 +0000 | [diff] [blame] | 832 | if (isa<GlobalVariable>(Operand) || isDirectAlloca(Operand)) |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 833 | Out << ')'; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Chris Lattner | 4148819 | 2003-06-28 17:15:12 +0000 | [diff] [blame] | 836 | // generateCompilerSpecificCode - This is where we add conditional compilation |
| 837 | // directives to cater to specific compilers as need be. |
| 838 | // |
Chris Lattner | c59c118 | 2003-11-16 22:06:14 +0000 | [diff] [blame] | 839 | static void generateCompilerSpecificCode(std::ostream& Out) { |
Chris Lattner | 9a571ba | 2006-03-07 22:58:23 +0000 | [diff] [blame] | 840 | // Alloca is hard to get, and we don't want to include stdlib.h here. |
Chris Lattner | 4148819 | 2003-06-28 17:15:12 +0000 | [diff] [blame] | 841 | Out << "/* get a declaration for alloca */\n" |
Chris Lattner | d947760 | 2006-06-02 18:54:01 +0000 | [diff] [blame] | 842 | << "#if defined(__CYGWIN__) || defined(__MINGW32__)\n" |
Reid Spencer | a8411a6 | 2005-01-23 04:32:47 +0000 | [diff] [blame] | 843 | << "extern void *_alloca(unsigned long);\n" |
| 844 | << "#define alloca(x) _alloca(x)\n" |
| 845 | << "#elif defined(__APPLE__)\n" |
Joel Stanley | ecd78ac | 2003-02-12 20:45:00 +0000 | [diff] [blame] | 846 | << "extern void *__builtin_alloca(unsigned long);\n" |
| 847 | << "#define alloca(x) __builtin_alloca(x)\n" |
Brian Gaeke | ce63005 | 2004-12-10 05:44:45 +0000 | [diff] [blame] | 848 | << "#elif defined(__sun__)\n" |
| 849 | << "#if defined(__sparcv9)\n" |
| 850 | << "extern void *__builtin_alloca(unsigned long);\n" |
| 851 | << "#else\n" |
| 852 | << "extern void *__builtin_alloca(unsigned int);\n" |
| 853 | << "#endif\n" |
| 854 | << "#define alloca(x) __builtin_alloca(x)\n" |
Jeff Cohen | 3c280bf | 2006-04-17 17:55:41 +0000 | [diff] [blame] | 855 | << "#elif defined(__FreeBSD__) || defined(__OpenBSD__)\n" |
Chris Lattner | 60e6674 | 2004-10-06 04:21:52 +0000 | [diff] [blame] | 856 | << "#define alloca(x) __builtin_alloca(x)\n" |
Jeff Cohen | d01f65a | 2005-01-06 04:21:49 +0000 | [diff] [blame] | 857 | << "#elif !defined(_MSC_VER)\n" |
Joel Stanley | ecd78ac | 2003-02-12 20:45:00 +0000 | [diff] [blame] | 858 | << "#include <alloca.h>\n" |
| 859 | << "#endif\n\n"; |
Chris Lattner | 4148819 | 2003-06-28 17:15:12 +0000 | [diff] [blame] | 860 | |
| 861 | // We output GCC specific attributes to preserve 'linkonce'ness on globals. |
| 862 | // If we aren't being compiled with GCC, just drop these attributes. |
Chris Lattner | 9bda5f5 | 2003-06-28 17:53:05 +0000 | [diff] [blame] | 863 | Out << "#ifndef __GNUC__ /* Can only support \"linkonce\" vars with GCC */\n" |
Chris Lattner | 4148819 | 2003-06-28 17:15:12 +0000 | [diff] [blame] | 864 | << "#define __attribute__(X)\n" |
Brian Gaeke | 27f7a71 | 2003-12-11 00:24:36 +0000 | [diff] [blame] | 865 | << "#endif\n\n"; |
| 866 | |
| 867 | #if 0 |
| 868 | // At some point, we should support "external weak" vs. "weak" linkages. |
| 869 | // On Mac OS X, "external weak" is spelled "__attribute__((weak_import))". |
| 870 | Out << "#if defined(__GNUC__) && defined(__APPLE_CC__)\n" |
| 871 | << "#define __EXTERNAL_WEAK__ __attribute__((weak_import))\n" |
| 872 | << "#elif defined(__GNUC__)\n" |
| 873 | << "#define __EXTERNAL_WEAK__ __attribute__((weak))\n" |
| 874 | << "#else\n" |
| 875 | << "#define __EXTERNAL_WEAK__\n" |
| 876 | << "#endif\n\n"; |
| 877 | #endif |
| 878 | |
| 879 | // For now, turn off the weak linkage attribute on Mac OS X. (See above.) |
| 880 | Out << "#if defined(__GNUC__) && defined(__APPLE_CC__)\n" |
| 881 | << "#define __ATTRIBUTE_WEAK__\n" |
| 882 | << "#elif defined(__GNUC__)\n" |
| 883 | << "#define __ATTRIBUTE_WEAK__ __attribute__((weak))\n" |
| 884 | << "#else\n" |
| 885 | << "#define __ATTRIBUTE_WEAK__\n" |
| 886 | << "#endif\n\n"; |
Brian Gaeke | 043c0bb | 2004-07-21 03:15:26 +0000 | [diff] [blame] | 887 | |
| 888 | // Define NaN and Inf as GCC builtins if using GCC, as 0 otherwise |
| 889 | // From the GCC documentation: |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 890 | // |
Brian Gaeke | 043c0bb | 2004-07-21 03:15:26 +0000 | [diff] [blame] | 891 | // double __builtin_nan (const char *str) |
| 892 | // |
| 893 | // This is an implementation of the ISO C99 function nan. |
| 894 | // |
| 895 | // Since ISO C99 defines this function in terms of strtod, which we do |
| 896 | // not implement, a description of the parsing is in order. The string is |
| 897 | // parsed as by strtol; that is, the base is recognized by leading 0 or |
| 898 | // 0x prefixes. The number parsed is placed in the significand such that |
| 899 | // the least significant bit of the number is at the least significant |
| 900 | // bit of the significand. The number is truncated to fit the significand |
| 901 | // field provided. The significand is forced to be a quiet NaN. |
| 902 | // |
| 903 | // This function, if given a string literal, is evaluated early enough |
| 904 | // that it is considered a compile-time constant. |
| 905 | // |
| 906 | // float __builtin_nanf (const char *str) |
| 907 | // |
| 908 | // Similar to __builtin_nan, except the return type is float. |
| 909 | // |
| 910 | // double __builtin_inf (void) |
| 911 | // |
| 912 | // Similar to __builtin_huge_val, except a warning is generated if the |
| 913 | // target floating-point format does not support infinities. This |
| 914 | // function is suitable for implementing the ISO C99 macro INFINITY. |
| 915 | // |
| 916 | // float __builtin_inff (void) |
| 917 | // |
| 918 | // Similar to __builtin_inf, except the return type is float. |
| 919 | Out << "#ifdef __GNUC__\n" |
Brian Gaeke | 8a702e8 | 2004-08-25 19:00:42 +0000 | [diff] [blame] | 920 | << "#define LLVM_NAN(NanStr) __builtin_nan(NanStr) /* Double */\n" |
| 921 | << "#define LLVM_NANF(NanStr) __builtin_nanf(NanStr) /* Float */\n" |
| 922 | << "#define LLVM_NANS(NanStr) __builtin_nans(NanStr) /* Double */\n" |
| 923 | << "#define LLVM_NANSF(NanStr) __builtin_nansf(NanStr) /* Float */\n" |
| 924 | << "#define LLVM_INF __builtin_inf() /* Double */\n" |
| 925 | << "#define LLVM_INFF __builtin_inff() /* Float */\n" |
Chris Lattner | f9a0532 | 2006-02-13 22:22:42 +0000 | [diff] [blame] | 926 | << "#define LLVM_PREFETCH(addr,rw,locality) " |
| 927 | "__builtin_prefetch(addr,rw,locality)\n" |
Chris Lattner | 9a571ba | 2006-03-07 22:58:23 +0000 | [diff] [blame] | 928 | << "#define __ATTRIBUTE_CTOR__ __attribute__((constructor))\n" |
| 929 | << "#define __ATTRIBUTE_DTOR__ __attribute__((destructor))\n" |
Chris Lattner | dacbe7b | 2006-07-28 20:58:47 +0000 | [diff] [blame] | 930 | << "#define LLVM_ASM __asm__\n" |
Brian Gaeke | 043c0bb | 2004-07-21 03:15:26 +0000 | [diff] [blame] | 931 | << "#else\n" |
Brian Gaeke | 8a702e8 | 2004-08-25 19:00:42 +0000 | [diff] [blame] | 932 | << "#define LLVM_NAN(NanStr) ((double)0.0) /* Double */\n" |
| 933 | << "#define LLVM_NANF(NanStr) 0.0F /* Float */\n" |
| 934 | << "#define LLVM_NANS(NanStr) ((double)0.0) /* Double */\n" |
| 935 | << "#define LLVM_NANSF(NanStr) 0.0F /* Float */\n" |
| 936 | << "#define LLVM_INF ((double)0.0) /* Double */\n" |
| 937 | << "#define LLVM_INFF 0.0F /* Float */\n" |
Chris Lattner | 50a8a17 | 2005-05-06 06:58:42 +0000 | [diff] [blame] | 938 | << "#define LLVM_PREFETCH(addr,rw,locality) /* PREFETCH */\n" |
Chris Lattner | 9a571ba | 2006-03-07 22:58:23 +0000 | [diff] [blame] | 939 | << "#define __ATTRIBUTE_CTOR__\n" |
| 940 | << "#define __ATTRIBUTE_DTOR__\n" |
Chris Lattner | dacbe7b | 2006-07-28 20:58:47 +0000 | [diff] [blame] | 941 | << "#define LLVM_ASM(X)\n" |
Chris Lattner | 50a8a17 | 2005-05-06 06:58:42 +0000 | [diff] [blame] | 942 | << "#endif\n\n"; |
| 943 | |
| 944 | // Output target-specific code that should be inserted into main. |
| 945 | Out << "#define CODE_FOR_MAIN() /* Any target-specific code for main()*/\n"; |
| 946 | // On X86, set the FP control word to 64-bits of precision instead of 80 bits. |
| 947 | Out << "#if defined(__GNUC__) && !defined(__llvm__)\n" |
Evan Cheng | e3db4da | 2006-06-20 22:11:12 +0000 | [diff] [blame] | 948 | << "#if defined(i386) || defined(__i386__) || defined(__i386) || " |
| 949 | << "defined(__x86_64__)\n" |
Chris Lattner | 50a8a17 | 2005-05-06 06:58:42 +0000 | [diff] [blame] | 950 | << "#undef CODE_FOR_MAIN\n" |
| 951 | << "#define CODE_FOR_MAIN() \\\n" |
| 952 | << " {short F;__asm__ (\"fnstcw %0\" : \"=m\" (*&F)); \\\n" |
| 953 | << " F=(F&~0x300)|0x200;__asm__(\"fldcw %0\"::\"m\"(*&F));}\n" |
| 954 | << "#endif\n#endif\n"; |
| 955 | |
Joel Stanley | ecd78ac | 2003-02-12 20:45:00 +0000 | [diff] [blame] | 956 | } |
| 957 | |
Chris Lattner | 9a571ba | 2006-03-07 22:58:23 +0000 | [diff] [blame] | 958 | /// FindStaticTors - Given a static ctor/dtor list, unpack its contents into |
| 959 | /// the StaticTors set. |
| 960 | static void FindStaticTors(GlobalVariable *GV, std::set<Function*> &StaticTors){ |
| 961 | ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); |
| 962 | if (!InitList) return; |
| 963 | |
| 964 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) |
| 965 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){ |
| 966 | if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. |
| 967 | |
| 968 | if (CS->getOperand(1)->isNullValue()) |
| 969 | return; // Found a null terminator, exit printing. |
| 970 | Constant *FP = CS->getOperand(1); |
| 971 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP)) |
| 972 | if (CE->getOpcode() == Instruction::Cast) |
| 973 | FP = CE->getOperand(0); |
| 974 | if (Function *F = dyn_cast<Function>(FP)) |
| 975 | StaticTors.insert(F); |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | enum SpecialGlobalClass { |
| 980 | NotSpecial = 0, |
| 981 | GlobalCtors, GlobalDtors, |
| 982 | NotPrinted |
| 983 | }; |
| 984 | |
| 985 | /// getGlobalVariableClass - If this is a global that is specially recognized |
| 986 | /// by LLVM, return a code that indicates how we should handle it. |
| 987 | static SpecialGlobalClass getGlobalVariableClass(const GlobalVariable *GV) { |
| 988 | // If this is a global ctors/dtors list, handle it now. |
| 989 | if (GV->hasAppendingLinkage() && GV->use_empty()) { |
| 990 | if (GV->getName() == "llvm.global_ctors") |
| 991 | return GlobalCtors; |
| 992 | else if (GV->getName() == "llvm.global_dtors") |
| 993 | return GlobalDtors; |
| 994 | } |
| 995 | |
| 996 | // Otherwise, it it is other metadata, don't print it. This catches things |
| 997 | // like debug information. |
| 998 | if (GV->getSection() == "llvm.metadata") |
| 999 | return NotPrinted; |
| 1000 | |
| 1001 | return NotSpecial; |
| 1002 | } |
| 1003 | |
| 1004 | |
Chris Lattner | 94f4f8e | 2004-02-13 23:00:29 +0000 | [diff] [blame] | 1005 | bool CWriter::doInitialization(Module &M) { |
| 1006 | // Initialize |
| 1007 | TheModule = &M; |
Chris Lattner | c242143 | 2004-05-09 04:30:20 +0000 | [diff] [blame] | 1008 | |
| 1009 | IL.AddPrototypes(M); |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1010 | |
Chris Lattner | 94f4f8e | 2004-02-13 23:00:29 +0000 | [diff] [blame] | 1011 | // Ensure that all structure types have names... |
Chris Lattner | 94f4f8e | 2004-02-13 23:00:29 +0000 | [diff] [blame] | 1012 | Mang = new Mangler(M); |
Chris Lattner | ba9c643 | 2005-11-10 21:39:29 +0000 | [diff] [blame] | 1013 | Mang->markCharUnacceptable('.'); |
Chris Lattner | c59c118 | 2003-11-16 22:06:14 +0000 | [diff] [blame] | 1014 | |
Chris Lattner | 9a571ba | 2006-03-07 22:58:23 +0000 | [diff] [blame] | 1015 | // Keep track of which functions are static ctors/dtors so they can have |
| 1016 | // an attribute added to their prototypes. |
| 1017 | std::set<Function*> StaticCtors, StaticDtors; |
| 1018 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 1019 | I != E; ++I) { |
| 1020 | switch (getGlobalVariableClass(I)) { |
| 1021 | default: break; |
| 1022 | case GlobalCtors: |
| 1023 | FindStaticTors(I, StaticCtors); |
| 1024 | break; |
| 1025 | case GlobalDtors: |
| 1026 | FindStaticTors(I, StaticDtors); |
| 1027 | break; |
| 1028 | } |
| 1029 | } |
| 1030 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 1031 | // get declaration for alloca |
Joel Stanley | ecd78ac | 2003-02-12 20:45:00 +0000 | [diff] [blame] | 1032 | Out << "/* Provide Declarations */\n"; |
Chris Lattner | c59c118 | 2003-11-16 22:06:14 +0000 | [diff] [blame] | 1033 | Out << "#include <stdarg.h>\n"; // Varargs support |
| 1034 | Out << "#include <setjmp.h>\n"; // Unwind support |
Chris Lattner | 4148819 | 2003-06-28 17:15:12 +0000 | [diff] [blame] | 1035 | generateCompilerSpecificCode(Out); |
Chris Lattner | c59c118 | 2003-11-16 22:06:14 +0000 | [diff] [blame] | 1036 | |
Chris Lattner | cf135cb | 2003-06-02 03:10:53 +0000 | [diff] [blame] | 1037 | // Provide a definition for `bool' if not compiling with a C++ compiler. |
| 1038 | Out << "\n" |
Vikram S. Adve | 969c4ad | 2002-08-25 20:00:08 +0000 | [diff] [blame] | 1039 | << "#ifndef __cplusplus\ntypedef unsigned char bool;\n#endif\n" |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1040 | |
Chris Lattner | d1cf1b4 | 2002-09-20 23:26:33 +0000 | [diff] [blame] | 1041 | << "\n\n/* Support for floating point constants */\n" |
| 1042 | << "typedef unsigned long long ConstantDoubleTy;\n" |
Chris Lattner | eb6f8c7 | 2002-11-07 19:43:59 +0000 | [diff] [blame] | 1043 | << "typedef unsigned int ConstantFloatTy;\n" |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1044 | |
Chris Lattner | a4d4a85 | 2002-08-19 21:48:40 +0000 | [diff] [blame] | 1045 | << "\n\n/* Global Declarations */\n"; |
| 1046 | |
| 1047 | // First output all the declarations for the program, because C requires |
| 1048 | // Functions & globals to be declared before they are used. |
| 1049 | // |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 1050 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1051 | // Loop over the symbol table, emitting all named constants... |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 1052 | printModuleTypes(M.getSymbolTable()); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1053 | |
Chris Lattner | a4d4a85 | 2002-08-19 21:48:40 +0000 | [diff] [blame] | 1054 | // Global variable declarations... |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 1055 | if (!M.global_empty()) { |
Vikram S. Adve | 913bfbc | 2002-09-17 11:50:38 +0000 | [diff] [blame] | 1056 | Out << "\n/* External Global Variable Declarations */\n"; |
Chris Lattner | f9a0532 | 2006-02-13 22:22:42 +0000 | [diff] [blame] | 1057 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 1058 | I != E; ++I) { |
Vikram S. Adve | 913bfbc | 2002-09-17 11:50:38 +0000 | [diff] [blame] | 1059 | if (I->hasExternalLinkage()) { |
| 1060 | Out << "extern "; |
Brian Gaeke | d9fb37a | 2003-07-24 20:20:44 +0000 | [diff] [blame] | 1061 | printType(Out, I->getType()->getElementType(), Mang->getValueName(I)); |
Vikram S. Adve | 913bfbc | 2002-09-17 11:50:38 +0000 | [diff] [blame] | 1062 | Out << ";\n"; |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 1063 | } else if (I->hasDLLImportLinkage()) { |
| 1064 | Out << "__declspec(dllimport) "; |
| 1065 | printType(Out, I->getType()->getElementType(), Mang->getValueName(I)); |
| 1066 | Out << ";\n"; |
| 1067 | } |
Chris Lattner | deed7a5 | 2002-05-09 15:18:52 +0000 | [diff] [blame] | 1068 | } |
Chris Lattner | deed7a5 | 2002-05-09 15:18:52 +0000 | [diff] [blame] | 1069 | } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1070 | |
Chris Lattner | a4d4a85 | 2002-08-19 21:48:40 +0000 | [diff] [blame] | 1071 | // Function declarations |
Chris Lattner | 9a571ba | 2006-03-07 22:58:23 +0000 | [diff] [blame] | 1072 | Out << "\n/* Function Declarations */\n"; |
Chris Lattner | 57da252 | 2005-08-23 20:22:50 +0000 | [diff] [blame] | 1073 | Out << "double fmod(double, double);\n"; // Support for FP rem |
| 1074 | Out << "float fmodf(float, float);\n"; |
| 1075 | |
Chris Lattner | 9a571ba | 2006-03-07 22:58:23 +0000 | [diff] [blame] | 1076 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
| 1077 | // Don't print declarations for intrinsic functions. |
| 1078 | if (!I->getIntrinsicID() && |
| 1079 | I->getName() != "setjmp" && I->getName() != "longjmp") { |
| 1080 | printFunctionSignature(I, true); |
| 1081 | if (I->hasWeakLinkage() || I->hasLinkOnceLinkage()) |
| 1082 | Out << " __ATTRIBUTE_WEAK__"; |
| 1083 | if (StaticCtors.count(I)) |
| 1084 | Out << " __ATTRIBUTE_CTOR__"; |
| 1085 | if (StaticDtors.count(I)) |
| 1086 | Out << " __ATTRIBUTE_DTOR__"; |
Chris Lattner | dacbe7b | 2006-07-28 20:58:47 +0000 | [diff] [blame] | 1087 | |
| 1088 | if (I->hasName() && I->getName()[0] == 1) |
| 1089 | Out << " LLVM_ASM(\"" << I->getName().c_str()+1 << "\")"; |
| 1090 | |
Chris Lattner | 9a571ba | 2006-03-07 22:58:23 +0000 | [diff] [blame] | 1091 | Out << ";\n"; |
Chris Lattner | 4cda835 | 2002-08-20 16:55:48 +0000 | [diff] [blame] | 1092 | } |
Chris Lattner | a4d4a85 | 2002-08-19 21:48:40 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
Joel Stanley | 54f6032 | 2003-06-25 04:52:09 +0000 | [diff] [blame] | 1095 | // Output the global variable declarations |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 1096 | if (!M.global_empty()) { |
Joel Stanley | 54f6032 | 2003-06-25 04:52:09 +0000 | [diff] [blame] | 1097 | Out << "\n\n/* Global Variable Declarations */\n"; |
Chris Lattner | f9a0532 | 2006-02-13 22:22:42 +0000 | [diff] [blame] | 1098 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 1099 | I != E; ++I) |
Nick Hildenbrandt | 50de36a | 2002-10-28 19:05:12 +0000 | [diff] [blame] | 1100 | if (!I->isExternal()) { |
Chris Lattner | 9a571ba | 2006-03-07 22:58:23 +0000 | [diff] [blame] | 1101 | // Ignore special globals, such as debug info. |
| 1102 | if (getGlobalVariableClass(I)) |
| 1103 | continue; |
| 1104 | |
Chris Lattner | cc16a8e | 2004-12-03 17:19:10 +0000 | [diff] [blame] | 1105 | if (I->hasInternalLinkage()) |
| 1106 | Out << "static "; |
| 1107 | else |
| 1108 | Out << "extern "; |
Brian Gaeke | d9fb37a | 2003-07-24 20:20:44 +0000 | [diff] [blame] | 1109 | printType(Out, I->getType()->getElementType(), Mang->getValueName(I)); |
Chris Lattner | 2580d4f | 2003-11-03 17:32:38 +0000 | [diff] [blame] | 1110 | |
| 1111 | if (I->hasLinkOnceLinkage()) |
| 1112 | Out << " __attribute__((common))"; |
| 1113 | else if (I->hasWeakLinkage()) |
Brian Gaeke | 27f7a71 | 2003-12-11 00:24:36 +0000 | [diff] [blame] | 1114 | Out << " __ATTRIBUTE_WEAK__"; |
Nick Hildenbrandt | 50de36a | 2002-10-28 19:05:12 +0000 | [diff] [blame] | 1115 | Out << ";\n"; |
| 1116 | } |
| 1117 | } |
| 1118 | |
Vikram S. Adve | 913bfbc | 2002-09-17 11:50:38 +0000 | [diff] [blame] | 1119 | // Output the global variable definitions and contents... |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 1120 | if (!M.global_empty()) { |
Vikram S. Adve | 913bfbc | 2002-09-17 11:50:38 +0000 | [diff] [blame] | 1121 | Out << "\n\n/* Global Variable Definitions and Initialization */\n"; |
Chris Lattner | f9a0532 | 2006-02-13 22:22:42 +0000 | [diff] [blame] | 1122 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 1123 | I != E; ++I) |
Chris Lattner | e8e035b | 2002-10-16 20:08:47 +0000 | [diff] [blame] | 1124 | if (!I->isExternal()) { |
Chris Lattner | 9a571ba | 2006-03-07 22:58:23 +0000 | [diff] [blame] | 1125 | // Ignore special globals, such as debug info. |
| 1126 | if (getGlobalVariableClass(I)) |
| 1127 | continue; |
| 1128 | |
Chris Lattner | e8e035b | 2002-10-16 20:08:47 +0000 | [diff] [blame] | 1129 | if (I->hasInternalLinkage()) |
| 1130 | Out << "static "; |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 1131 | else if (I->hasDLLImportLinkage()) |
| 1132 | Out << "__declspec(dllimport) "; |
| 1133 | else if (I->hasDLLExportLinkage()) |
| 1134 | Out << "__declspec(dllexport) "; |
| 1135 | |
Brian Gaeke | d9fb37a | 2003-07-24 20:20:44 +0000 | [diff] [blame] | 1136 | printType(Out, I->getType()->getElementType(), Mang->getValueName(I)); |
Chris Lattner | 76d9f1b | 2003-06-28 17:08:36 +0000 | [diff] [blame] | 1137 | if (I->hasLinkOnceLinkage()) |
| 1138 | Out << " __attribute__((common))"; |
Chris Lattner | 72ac148d | 2003-10-16 18:29:00 +0000 | [diff] [blame] | 1139 | else if (I->hasWeakLinkage()) |
Brian Gaeke | 27f7a71 | 2003-12-11 00:24:36 +0000 | [diff] [blame] | 1140 | Out << " __ATTRIBUTE_WEAK__"; |
Chris Lattner | 5ea326a | 2003-11-03 17:35:00 +0000 | [diff] [blame] | 1141 | |
| 1142 | // If the initializer is not null, emit the initializer. If it is null, |
| 1143 | // we try to avoid emitting large amounts of zeros. The problem with |
| 1144 | // this, however, occurs when the variable has weak linkage. In this |
| 1145 | // case, the assembler will complain about the variable being both weak |
| 1146 | // and common, so we disable this optimization. |
Chris Lattner | f358c5a | 2004-02-20 05:49:22 +0000 | [diff] [blame] | 1147 | if (!I->getInitializer()->isNullValue()) { |
Chris Lattner | 940b08d | 2003-06-06 07:10:24 +0000 | [diff] [blame] | 1148 | Out << " = " ; |
| 1149 | writeOperand(I->getInitializer()); |
Chris Lattner | f358c5a | 2004-02-20 05:49:22 +0000 | [diff] [blame] | 1150 | } else if (I->hasWeakLinkage()) { |
| 1151 | // We have to specify an initializer, but it doesn't have to be |
| 1152 | // complete. If the value is an aggregate, print out { 0 }, and let |
| 1153 | // the compiler figure out the rest of the zeros. |
| 1154 | Out << " = " ; |
| 1155 | if (isa<StructType>(I->getInitializer()->getType()) || |
Robert Bocchino | b78e838 | 2006-01-20 20:43:57 +0000 | [diff] [blame] | 1156 | isa<ArrayType>(I->getInitializer()->getType()) || |
| 1157 | isa<PackedType>(I->getInitializer()->getType())) { |
Chris Lattner | f358c5a | 2004-02-20 05:49:22 +0000 | [diff] [blame] | 1158 | Out << "{ 0 }"; |
| 1159 | } else { |
| 1160 | // Just print it out normally. |
| 1161 | writeOperand(I->getInitializer()); |
| 1162 | } |
Chris Lattner | 940b08d | 2003-06-06 07:10:24 +0000 | [diff] [blame] | 1163 | } |
Chris Lattner | e8e035b | 2002-10-16 20:08:47 +0000 | [diff] [blame] | 1164 | Out << ";\n"; |
Chris Lattner | a4d4a85 | 2002-08-19 21:48:40 +0000 | [diff] [blame] | 1165 | } |
Chris Lattner | a4d4a85 | 2002-08-19 21:48:40 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Chris Lattner | 94f4f8e | 2004-02-13 23:00:29 +0000 | [diff] [blame] | 1168 | if (!M.empty()) |
Chris Lattner | a4d4a85 | 2002-08-19 21:48:40 +0000 | [diff] [blame] | 1169 | Out << "\n\n/* Function Bodies */\n"; |
Chris Lattner | 94f4f8e | 2004-02-13 23:00:29 +0000 | [diff] [blame] | 1170 | return false; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Chris Lattner | 94f4f8e | 2004-02-13 23:00:29 +0000 | [diff] [blame] | 1173 | |
Chris Lattner | 9860e77 | 2003-10-12 04:36:29 +0000 | [diff] [blame] | 1174 | /// Output all floating point constants that cannot be printed accurately... |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 1175 | void CWriter::printFloatingPointConstants(Function &F) { |
Chris Lattner | 9860e77 | 2003-10-12 04:36:29 +0000 | [diff] [blame] | 1176 | // Scan the module for floating point constants. If any FP constant is used |
| 1177 | // in the function, we want to redirect it here so that we do not depend on |
| 1178 | // the precision of the printed form, unless the printed form preserves |
| 1179 | // precision. |
| 1180 | // |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 1181 | static unsigned FPCounter = 0; |
| 1182 | for (constant_iterator I = constant_begin(&F), E = constant_end(&F); |
| 1183 | I != E; ++I) |
| 1184 | if (const ConstantFP *FPC = dyn_cast<ConstantFP>(*I)) |
| 1185 | if (!isFPCSafeToPrint(FPC) && // Do not put in FPConstantMap if safe. |
| 1186 | !FPConstantMap.count(FPC)) { |
| 1187 | double Val = FPC->getValue(); |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1188 | |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 1189 | FPConstantMap[FPC] = FPCounter; // Number the FP constants |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1190 | |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 1191 | if (FPC->getType() == Type::DoubleTy) { |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 1192 | Out << "static const ConstantDoubleTy FPConstant" << FPCounter++ |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 1193 | << " = 0x" << std::hex << DoubleToBits(Val) << std::dec |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 1194 | << "ULL; /* " << Val << " */\n"; |
| 1195 | } else if (FPC->getType() == Type::FloatTy) { |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 1196 | Out << "static const ConstantFloatTy FPConstant" << FPCounter++ |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 1197 | << " = 0x" << std::hex << FloatToBits(Val) << std::dec |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 1198 | << "U; /* " << Val << " */\n"; |
| 1199 | } else |
| 1200 | assert(0 && "Unknown float type!"); |
| 1201 | } |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1202 | |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1203 | Out << '\n'; |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 1204 | } |
Chris Lattner | 9860e77 | 2003-10-12 04:36:29 +0000 | [diff] [blame] | 1205 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1206 | |
Chris Lattner | 2db41cd | 2002-09-20 15:12:13 +0000 | [diff] [blame] | 1207 | /// printSymbolTable - Run through symbol table looking for type names. If a |
Robert Bocchino | b78e838 | 2006-01-20 20:43:57 +0000 | [diff] [blame] | 1208 | /// type name is found, emit its declaration... |
Chris Lattner | a4c047e | 2002-09-20 14:56:54 +0000 | [diff] [blame] | 1209 | /// |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 1210 | void CWriter::printModuleTypes(const SymbolTable &ST) { |
Chris Lattner | b15fde2 | 2005-03-06 02:28:23 +0000 | [diff] [blame] | 1211 | // We are only interested in the type plane of the symbol table. |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 1212 | SymbolTable::type_const_iterator I = ST.type_begin(); |
| 1213 | SymbolTable::type_const_iterator End = ST.type_end(); |
Chris Lattner | b15fde2 | 2005-03-06 02:28:23 +0000 | [diff] [blame] | 1214 | |
| 1215 | // If there are no type names, exit early. |
| 1216 | if (I == End) return; |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1217 | |
Chris Lattner | 58d04d4 | 2002-09-20 15:18:30 +0000 | [diff] [blame] | 1218 | // Print out forward declarations for structure types before anything else! |
| 1219 | Out << "/* Structure forward decls */\n"; |
| 1220 | for (; I != End; ++I) |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 1221 | if (const Type *STy = dyn_cast<StructType>(I->second)) { |
Chris Lattner | 36c975c | 2005-11-10 18:53:25 +0000 | [diff] [blame] | 1222 | std::string Name = "struct l_" + Mang->makeNameProper(I->first); |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 1223 | Out << Name << ";\n"; |
| 1224 | TypeNames.insert(std::make_pair(STy, Name)); |
| 1225 | } |
Chris Lattner | 58d04d4 | 2002-09-20 15:18:30 +0000 | [diff] [blame] | 1226 | |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1227 | Out << '\n'; |
Chris Lattner | 58d04d4 | 2002-09-20 15:18:30 +0000 | [diff] [blame] | 1228 | |
| 1229 | // Now we can print out typedefs... |
| 1230 | Out << "/* Typedefs */\n"; |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 1231 | for (I = ST.type_begin(); I != End; ++I) { |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 1232 | const Type *Ty = cast<Type>(I->second); |
Chris Lattner | 36c975c | 2005-11-10 18:53:25 +0000 | [diff] [blame] | 1233 | std::string Name = "l_" + Mang->makeNameProper(I->first); |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 1234 | Out << "typedef "; |
| 1235 | printType(Out, Ty, Name); |
| 1236 | Out << ";\n"; |
| 1237 | } |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1238 | |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1239 | Out << '\n'; |
Chris Lattner | 3ef6dc7 | 2002-05-09 14:40:11 +0000 | [diff] [blame] | 1240 | |
Chris Lattner | 2c601a7 | 2002-09-20 15:05:40 +0000 | [diff] [blame] | 1241 | // Keep track of which structures have been printed so far... |
| 1242 | std::set<const StructType *> StructPrinted; |
| 1243 | |
Nick Hildenbrandt | 67aa2e2 | 2002-09-14 21:36:24 +0000 | [diff] [blame] | 1244 | // Loop over all structures then push them into the stack so they are |
| 1245 | // printed in the correct order. |
Chris Lattner | 2db41cd | 2002-09-20 15:12:13 +0000 | [diff] [blame] | 1246 | // |
Chris Lattner | 58d04d4 | 2002-09-20 15:18:30 +0000 | [diff] [blame] | 1247 | Out << "/* Structure contents */\n"; |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 1248 | for (I = ST.type_begin(); I != End; ++I) |
Chris Lattner | 2db41cd | 2002-09-20 15:12:13 +0000 | [diff] [blame] | 1249 | if (const StructType *STy = dyn_cast<StructType>(I->second)) |
Chris Lattner | fa395ec | 2003-11-02 01:29:27 +0000 | [diff] [blame] | 1250 | // Only print out used types! |
Chris Lattner | 6875807 | 2004-05-09 06:20:51 +0000 | [diff] [blame] | 1251 | printContainedStructs(STy, StructPrinted); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Nick Hildenbrandt | 67aa2e2 | 2002-09-14 21:36:24 +0000 | [diff] [blame] | 1254 | // Push the struct onto the stack and recursively push all structs |
| 1255 | // this one depends on. |
Robert Bocchino | b78e838 | 2006-01-20 20:43:57 +0000 | [diff] [blame] | 1256 | // |
| 1257 | // TODO: Make this work properly with packed types |
| 1258 | // |
Chris Lattner | 2c601a7 | 2002-09-20 15:05:40 +0000 | [diff] [blame] | 1259 | void CWriter::printContainedStructs(const Type *Ty, |
| 1260 | std::set<const StructType*> &StructPrinted){ |
Chris Lattner | 14d9b20 | 2006-01-20 18:57:03 +0000 | [diff] [blame] | 1261 | // Don't walk through pointers. |
| 1262 | if (isa<PointerType>(Ty) || Ty->isPrimitiveType()) return; |
| 1263 | |
| 1264 | // Print all contained types first. |
| 1265 | for (Type::subtype_iterator I = Ty->subtype_begin(), |
| 1266 | E = Ty->subtype_end(); I != E; ++I) |
| 1267 | printContainedStructs(*I, StructPrinted); |
| 1268 | |
Misha Brukman | ac25de3 | 2003-07-09 17:33:50 +0000 | [diff] [blame] | 1269 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
Chris Lattner | 14d9b20 | 2006-01-20 18:57:03 +0000 | [diff] [blame] | 1270 | // Check to see if we have already printed this struct. |
| 1271 | if (StructPrinted.insert(STy).second) { |
| 1272 | // Print structure type out. |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1273 | std::string Name = TypeNames[STy]; |
Nick Hildenbrandt | 9850237 | 2002-11-18 20:55:50 +0000 | [diff] [blame] | 1274 | printType(Out, STy, Name, true); |
Chris Lattner | 58d04d4 | 2002-09-20 15:18:30 +0000 | [diff] [blame] | 1275 | Out << ";\n\n"; |
Nick Hildenbrandt | 67aa2e2 | 2002-09-14 21:36:24 +0000 | [diff] [blame] | 1276 | } |
Nick Hildenbrandt | 67aa2e2 | 2002-09-14 21:36:24 +0000 | [diff] [blame] | 1277 | } |
| 1278 | } |
| 1279 | |
Chris Lattner | 4cda835 | 2002-08-20 16:55:48 +0000 | [diff] [blame] | 1280 | void CWriter::printFunctionSignature(const Function *F, bool Prototype) { |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 1281 | /// isCStructReturn - Should this function actually return a struct by-value? |
| 1282 | bool isCStructReturn = F->getCallingConv() == CallingConv::CSRet; |
| 1283 | |
Joel Stanley | 54f6032 | 2003-06-25 04:52:09 +0000 | [diff] [blame] | 1284 | if (F->hasInternalLinkage()) Out << "static "; |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 1285 | if (F->hasDLLImportLinkage()) Out << "__declspec(dllimport) "; |
| 1286 | if (F->hasDLLExportLinkage()) Out << "__declspec(dllexport) "; |
Anton Korobeynikov | bcb9770 | 2006-09-17 20:25:45 +0000 | [diff] [blame] | 1287 | switch (F->getCallingConv()) { |
| 1288 | case CallingConv::X86_StdCall: |
| 1289 | Out << "__stdcall "; |
| 1290 | break; |
| 1291 | case CallingConv::X86_FastCall: |
| 1292 | Out << "__fastcall "; |
| 1293 | break; |
| 1294 | } |
| 1295 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1296 | // Loop over the arguments, printing them... |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 1297 | const FunctionType *FT = cast<FunctionType>(F->getFunctionType()); |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1298 | |
| 1299 | std::stringstream FunctionInnards; |
| 1300 | |
Nick Hildenbrandt | 9850237 | 2002-11-18 20:55:50 +0000 | [diff] [blame] | 1301 | // Print out the name... |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1302 | FunctionInnards << Mang->getValueName(F) << '('; |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1303 | |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 1304 | bool PrintedArg = false; |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 1305 | if (!F->isExternal()) { |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 1306 | if (!F->arg_empty()) { |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 1307 | Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 1308 | |
| 1309 | // If this is a struct-return function, don't print the hidden |
| 1310 | // struct-return argument. |
| 1311 | if (isCStructReturn) { |
| 1312 | assert(I != E && "Invalid struct return function!"); |
| 1313 | ++I; |
| 1314 | } |
| 1315 | |
Chris Lattner | edd8ce1 | 2003-05-03 03:14:35 +0000 | [diff] [blame] | 1316 | std::string ArgName; |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 1317 | for (; I != E; ++I) { |
| 1318 | if (PrintedArg) FunctionInnards << ", "; |
Chris Lattner | 4cda835 | 2002-08-20 16:55:48 +0000 | [diff] [blame] | 1319 | if (I->hasName() || !Prototype) |
Brian Gaeke | d9fb37a | 2003-07-24 20:20:44 +0000 | [diff] [blame] | 1320 | ArgName = Mang->getValueName(I); |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1321 | else |
Chris Lattner | 4cda835 | 2002-08-20 16:55:48 +0000 | [diff] [blame] | 1322 | ArgName = ""; |
Joel Stanley | 54f6032 | 2003-06-25 04:52:09 +0000 | [diff] [blame] | 1323 | printType(FunctionInnards, I->getType(), ArgName); |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 1324 | PrintedArg = true; |
Chris Lattner | deed7a5 | 2002-05-09 15:18:52 +0000 | [diff] [blame] | 1325 | } |
| 1326 | } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1327 | } else { |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 1328 | // Loop over the arguments, printing them. |
| 1329 | FunctionType::param_iterator I = FT->param_begin(), E = FT->param_end(); |
| 1330 | |
| 1331 | // If this is a struct-return function, don't print the hidden |
| 1332 | // struct-return argument. |
| 1333 | if (isCStructReturn) { |
| 1334 | assert(I != E && "Invalid struct return function!"); |
| 1335 | ++I; |
| 1336 | } |
| 1337 | |
| 1338 | for (; I != E; ++I) { |
| 1339 | if (PrintedArg) FunctionInnards << ", "; |
Joel Stanley | 54f6032 | 2003-06-25 04:52:09 +0000 | [diff] [blame] | 1340 | printType(FunctionInnards, *I); |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 1341 | PrintedArg = true; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1342 | } |
| 1343 | } |
| 1344 | |
Chris Lattner | 1f8c4a1 | 2002-09-20 22:32:30 +0000 | [diff] [blame] | 1345 | // Finish printing arguments... if this is a vararg function, print the ..., |
| 1346 | // unless there are no known types, in which case, we just emit (). |
| 1347 | // |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 1348 | if (FT->isVarArg() && PrintedArg) { |
| 1349 | if (PrintedArg) FunctionInnards << ", "; |
Joel Stanley | 54f6032 | 2003-06-25 04:52:09 +0000 | [diff] [blame] | 1350 | FunctionInnards << "..."; // Output varargs portion of signature! |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 1351 | } else if (!FT->isVarArg() && !PrintedArg) { |
Chris Lattner | 4f7e173 | 2003-11-26 00:09:17 +0000 | [diff] [blame] | 1352 | FunctionInnards << "void"; // ret() -> ret(void) in C. |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1353 | } |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1354 | FunctionInnards << ')'; |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 1355 | |
| 1356 | // Get the return tpe for the function. |
| 1357 | const Type *RetTy; |
| 1358 | if (!isCStructReturn) |
| 1359 | RetTy = F->getReturnType(); |
| 1360 | else { |
| 1361 | // If this is a struct-return function, print the struct-return type. |
| 1362 | RetTy = cast<PointerType>(FT->getParamType(0))->getElementType(); |
| 1363 | } |
| 1364 | |
| 1365 | // Print out the return type and the signature built above. |
| 1366 | printType(Out, RetTy, FunctionInnards.str()); |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
Chris Lattner | 94f4f8e | 2004-02-13 23:00:29 +0000 | [diff] [blame] | 1369 | void CWriter::printFunction(Function &F) { |
| 1370 | printFunctionSignature(&F, false); |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 1371 | Out << " {\n"; |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 1372 | |
| 1373 | // If this is a struct return function, handle the result with magic. |
| 1374 | if (F.getCallingConv() == CallingConv::CSRet) { |
| 1375 | const Type *StructTy = |
| 1376 | cast<PointerType>(F.arg_begin()->getType())->getElementType(); |
| 1377 | Out << " "; |
| 1378 | printType(Out, StructTy, "StructReturn"); |
| 1379 | Out << "; /* Struct return temporary */\n"; |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 1380 | |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 1381 | Out << " "; |
| 1382 | printType(Out, F.arg_begin()->getType(), Mang->getValueName(F.arg_begin())); |
| 1383 | Out << " = &StructReturn;\n"; |
| 1384 | } |
| 1385 | |
| 1386 | bool PrintedVar = false; |
| 1387 | |
Chris Lattner | 497e19a | 2002-05-09 20:39:03 +0000 | [diff] [blame] | 1388 | // print local variable information for the function |
Chris Lattner | 94f4f8e | 2004-02-13 23:00:29 +0000 | [diff] [blame] | 1389 | for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) |
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 1390 | if (const AllocaInst *AI = isDirectAlloca(&*I)) { |
Chris Lattner | a8ab89e | 2003-06-17 04:39:14 +0000 | [diff] [blame] | 1391 | Out << " "; |
Brian Gaeke | d9fb37a | 2003-07-24 20:20:44 +0000 | [diff] [blame] | 1392 | printType(Out, AI->getAllocatedType(), Mang->getValueName(AI)); |
Misha Brukman | 41ce39c | 2005-01-31 06:19:57 +0000 | [diff] [blame] | 1393 | Out << "; /* Address-exposed local */\n"; |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 1394 | PrintedVar = true; |
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 1395 | } else if (I->getType() != Type::VoidTy && !isInlinableInst(*I)) { |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 1396 | Out << " "; |
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 1397 | printType(Out, I->getType(), Mang->getValueName(&*I)); |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 1398 | Out << ";\n"; |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1399 | |
Chris Lattner | 84e6665 | 2003-05-03 07:11:00 +0000 | [diff] [blame] | 1400 | if (isa<PHINode>(*I)) { // Print out PHI node temporaries as well... |
| 1401 | Out << " "; |
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 1402 | printType(Out, I->getType(), |
| 1403 | Mang->getValueName(&*I)+"__PHI_TEMPORARY"); |
Chris Lattner | 84e6665 | 2003-05-03 07:11:00 +0000 | [diff] [blame] | 1404 | Out << ";\n"; |
| 1405 | } |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 1406 | PrintedVar = true; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1407 | } |
Chris Lattner | d1cf1b4 | 2002-09-20 23:26:33 +0000 | [diff] [blame] | 1408 | |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 1409 | if (PrintedVar) |
| 1410 | Out << '\n'; |
Chris Lattner | d1cf1b4 | 2002-09-20 23:26:33 +0000 | [diff] [blame] | 1411 | |
Chris Lattner | 395fd59 | 2004-12-13 21:52:52 +0000 | [diff] [blame] | 1412 | if (F.hasExternalLinkage() && F.getName() == "main") |
Chris Lattner | 50a8a17 | 2005-05-06 06:58:42 +0000 | [diff] [blame] | 1413 | Out << " CODE_FOR_MAIN();\n"; |
Chris Lattner | 395fd59 | 2004-12-13 21:52:52 +0000 | [diff] [blame] | 1414 | |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 1415 | // print the basic blocks |
Chris Lattner | 94f4f8e | 2004-02-13 23:00:29 +0000 | [diff] [blame] | 1416 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { |
Chris Lattner | cb3ad01 | 2004-05-09 20:41:32 +0000 | [diff] [blame] | 1417 | if (Loop *L = LI->getLoopFor(BB)) { |
| 1418 | if (L->getHeader() == BB && L->getParentLoop() == 0) |
| 1419 | printLoop(L); |
| 1420 | } else { |
| 1421 | printBasicBlock(BB); |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 1422 | } |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 1423 | } |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1424 | |
Chris Lattner | 8c3c4bf | 2002-05-09 15:49:41 +0000 | [diff] [blame] | 1425 | Out << "}\n\n"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
Chris Lattner | cb3ad01 | 2004-05-09 20:41:32 +0000 | [diff] [blame] | 1428 | void CWriter::printLoop(Loop *L) { |
| 1429 | Out << " do { /* Syntactic loop '" << L->getHeader()->getName() |
| 1430 | << "' to make GCC happy */\n"; |
| 1431 | for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i) { |
| 1432 | BasicBlock *BB = L->getBlocks()[i]; |
| 1433 | Loop *BBLoop = LI->getLoopFor(BB); |
| 1434 | if (BBLoop == L) |
| 1435 | printBasicBlock(BB); |
| 1436 | else if (BB == BBLoop->getHeader() && BBLoop->getParentLoop() == L) |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1437 | printLoop(BBLoop); |
Chris Lattner | cb3ad01 | 2004-05-09 20:41:32 +0000 | [diff] [blame] | 1438 | } |
| 1439 | Out << " } while (1); /* end of syntactic loop '" |
| 1440 | << L->getHeader()->getName() << "' */\n"; |
| 1441 | } |
| 1442 | |
| 1443 | void CWriter::printBasicBlock(BasicBlock *BB) { |
| 1444 | |
| 1445 | // Don't print the label for the basic block if there are no uses, or if |
| 1446 | // the only terminator use is the predecessor basic block's terminator. |
| 1447 | // We have to scan the use list because PHI nodes use basic blocks too but |
| 1448 | // do not require a label to be generated. |
| 1449 | // |
| 1450 | bool NeedsLabel = false; |
| 1451 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) |
| 1452 | if (isGotoCodeNecessary(*PI, BB)) { |
| 1453 | NeedsLabel = true; |
| 1454 | break; |
| 1455 | } |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1456 | |
Chris Lattner | cb3ad01 | 2004-05-09 20:41:32 +0000 | [diff] [blame] | 1457 | if (NeedsLabel) Out << Mang->getValueName(BB) << ":\n"; |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1458 | |
Chris Lattner | cb3ad01 | 2004-05-09 20:41:32 +0000 | [diff] [blame] | 1459 | // Output all of the instructions in the basic block... |
| 1460 | for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; |
| 1461 | ++II) { |
| 1462 | if (!isInlinableInst(*II) && !isDirectAlloca(II)) { |
| 1463 | if (II->getType() != Type::VoidTy) |
| 1464 | outputLValue(II); |
| 1465 | else |
| 1466 | Out << " "; |
| 1467 | visit(*II); |
| 1468 | Out << ";\n"; |
| 1469 | } |
| 1470 | } |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1471 | |
Chris Lattner | cb3ad01 | 2004-05-09 20:41:32 +0000 | [diff] [blame] | 1472 | // Don't emit prefix or suffix for the terminator... |
| 1473 | visit(*BB->getTerminator()); |
| 1474 | } |
| 1475 | |
| 1476 | |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1477 | // Specific Instruction type classes... note that all of the casts are |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 1478 | // necessary because we use the instruction classes as opaque types... |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1479 | // |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1480 | void CWriter::visitReturnInst(ReturnInst &I) { |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 1481 | // If this is a struct return function, return the temporary struct. |
| 1482 | if (I.getParent()->getParent()->getCallingConv() == CallingConv::CSRet) { |
| 1483 | Out << " return StructReturn;\n"; |
| 1484 | return; |
| 1485 | } |
| 1486 | |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1487 | // Don't output a void return if this is the last basic block in the function |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1488 | if (I.getNumOperands() == 0 && |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1489 | &*--I.getParent()->getParent()->end() == I.getParent() && |
| 1490 | !I.getParent()->size() == 1) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1491 | return; |
Chris Lattner | 963b70b | 2002-05-21 18:05:19 +0000 | [diff] [blame] | 1492 | } |
Chris Lattner | 4440826 | 2002-05-09 03:50:42 +0000 | [diff] [blame] | 1493 | |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1494 | Out << " return"; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1495 | if (I.getNumOperands()) { |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1496 | Out << ' '; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1497 | writeOperand(I.getOperand(0)); |
Chris Lattner | 16c7bb2 | 2002-05-09 02:28:59 +0000 | [diff] [blame] | 1498 | } |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1499 | Out << ";\n"; |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Chris Lattner | a9f5e05 | 2003-04-22 20:19:52 +0000 | [diff] [blame] | 1502 | void CWriter::visitSwitchInst(SwitchInst &SI) { |
Chris Lattner | a05e0ec | 2004-05-09 03:42:48 +0000 | [diff] [blame] | 1503 | |
Chris Lattner | a9f5e05 | 2003-04-22 20:19:52 +0000 | [diff] [blame] | 1504 | Out << " switch ("; |
| 1505 | writeOperand(SI.getOperand(0)); |
Chris Lattner | f1acd96 | 2003-04-23 19:15:13 +0000 | [diff] [blame] | 1506 | Out << ") {\n default:\n"; |
John Criswell | 30cc227 | 2004-10-25 18:30:09 +0000 | [diff] [blame] | 1507 | printPHICopiesForSuccessor (SI.getParent(), SI.getDefaultDest(), 2); |
Chris Lattner | f1acd96 | 2003-04-23 19:15:13 +0000 | [diff] [blame] | 1508 | printBranchToBlock(SI.getParent(), SI.getDefaultDest(), 2); |
Chris Lattner | a9f5e05 | 2003-04-22 20:19:52 +0000 | [diff] [blame] | 1509 | Out << ";\n"; |
| 1510 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) { |
| 1511 | Out << " case "; |
| 1512 | writeOperand(SI.getOperand(i)); |
| 1513 | Out << ":\n"; |
| 1514 | BasicBlock *Succ = cast<BasicBlock>(SI.getOperand(i+1)); |
John Criswell | 30cc227 | 2004-10-25 18:30:09 +0000 | [diff] [blame] | 1515 | printPHICopiesForSuccessor (SI.getParent(), Succ, 2); |
Chris Lattner | a9f5e05 | 2003-04-22 20:19:52 +0000 | [diff] [blame] | 1516 | printBranchToBlock(SI.getParent(), Succ, 2); |
Chris Lattner | efd02c7 | 2005-03-19 17:35:11 +0000 | [diff] [blame] | 1517 | if (Function::iterator(Succ) == next(Function::iterator(SI.getParent()))) |
Chris Lattner | a9f5e05 | 2003-04-22 20:19:52 +0000 | [diff] [blame] | 1518 | Out << " break;\n"; |
| 1519 | } |
| 1520 | Out << " }\n"; |
| 1521 | } |
| 1522 | |
Chris Lattner | a9d12c0 | 2004-10-16 18:12:13 +0000 | [diff] [blame] | 1523 | void CWriter::visitUnreachableInst(UnreachableInst &I) { |
Chris Lattner | 963869e | 2004-10-17 23:49:11 +0000 | [diff] [blame] | 1524 | Out << " /*UNREACHABLE*/;\n"; |
Chris Lattner | a9d12c0 | 2004-10-16 18:12:13 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
Chris Lattner | cb3ad01 | 2004-05-09 20:41:32 +0000 | [diff] [blame] | 1527 | bool CWriter::isGotoCodeNecessary(BasicBlock *From, BasicBlock *To) { |
| 1528 | /// FIXME: This should be reenabled, but loop reordering safe!! |
| 1529 | return true; |
| 1530 | |
Chris Lattner | 67c2d18 | 2005-03-18 16:12:37 +0000 | [diff] [blame] | 1531 | if (next(Function::iterator(From)) != Function::iterator(To)) |
| 1532 | return true; // Not the direct successor, we need a goto. |
Chris Lattner | cb3ad01 | 2004-05-09 20:41:32 +0000 | [diff] [blame] | 1533 | |
| 1534 | //isa<SwitchInst>(From->getTerminator()) |
Chris Lattner | 9bda5f5 | 2003-06-28 17:53:05 +0000 | [diff] [blame] | 1535 | |
Chris Lattner | cb3ad01 | 2004-05-09 20:41:32 +0000 | [diff] [blame] | 1536 | if (LI->getLoopFor(From) != LI->getLoopFor(To)) |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1537 | return true; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1538 | return false; |
| 1539 | } |
| 1540 | |
John Criswell | 57bbfce | 2004-10-20 14:38:39 +0000 | [diff] [blame] | 1541 | void CWriter::printPHICopiesForSuccessor (BasicBlock *CurBlock, |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1542 | BasicBlock *Successor, |
John Criswell | 57bbfce | 2004-10-20 14:38:39 +0000 | [diff] [blame] | 1543 | unsigned Indent) { |
| 1544 | for (BasicBlock::iterator I = Successor->begin(); isa<PHINode>(I); ++I) { |
| 1545 | PHINode *PN = cast<PHINode>(I); |
| 1546 | // Now we have to do the printing. |
| 1547 | Value *IV = PN->getIncomingValueForBlock(CurBlock); |
| 1548 | if (!isa<UndefValue>(IV)) { |
| 1549 | Out << std::string(Indent, ' '); |
| 1550 | Out << " " << Mang->getValueName(I) << "__PHI_TEMPORARY = "; |
| 1551 | writeOperand(IV); |
| 1552 | Out << "; /* for PHI node */\n"; |
| 1553 | } |
| 1554 | } |
| 1555 | } |
| 1556 | |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1557 | void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ, |
Chris Lattner | 9bda5f5 | 2003-06-28 17:53:05 +0000 | [diff] [blame] | 1558 | unsigned Indent) { |
Chris Lattner | cb3ad01 | 2004-05-09 20:41:32 +0000 | [diff] [blame] | 1559 | if (isGotoCodeNecessary(CurBB, Succ)) { |
Chris Lattner | edd8ce1 | 2003-05-03 03:14:35 +0000 | [diff] [blame] | 1560 | Out << std::string(Indent, ' ') << " goto "; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1561 | writeOperand(Succ); |
| 1562 | Out << ";\n"; |
| 1563 | } |
| 1564 | } |
| 1565 | |
Misha Brukman | 37f92e2 | 2003-09-11 22:34:13 +0000 | [diff] [blame] | 1566 | // Branch instruction printing - Avoid printing out a branch to a basic block |
| 1567 | // that immediately succeeds the current one. |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1568 | // |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1569 | void CWriter::visitBranchInst(BranchInst &I) { |
Chris Lattner | a05e0ec | 2004-05-09 03:42:48 +0000 | [diff] [blame] | 1570 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1571 | if (I.isConditional()) { |
Chris Lattner | 5ef6ffd | 2003-10-12 08:12:58 +0000 | [diff] [blame] | 1572 | if (isGotoCodeNecessary(I.getParent(), I.getSuccessor(0))) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1573 | Out << " if ("; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1574 | writeOperand(I.getCondition()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1575 | Out << ") {\n"; |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1576 | |
John Criswell | 57bbfce | 2004-10-20 14:38:39 +0000 | [diff] [blame] | 1577 | printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(0), 2); |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1578 | printBranchToBlock(I.getParent(), I.getSuccessor(0), 2); |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1579 | |
Chris Lattner | 5ef6ffd | 2003-10-12 08:12:58 +0000 | [diff] [blame] | 1580 | if (isGotoCodeNecessary(I.getParent(), I.getSuccessor(1))) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1581 | Out << " } else {\n"; |
John Criswell | 57bbfce | 2004-10-20 14:38:39 +0000 | [diff] [blame] | 1582 | printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(1), 2); |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1583 | printBranchToBlock(I.getParent(), I.getSuccessor(1), 2); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1584 | } |
| 1585 | } else { |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 1586 | // First goto not necessary, assume second one is... |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1587 | Out << " if (!"; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1588 | writeOperand(I.getCondition()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1589 | Out << ") {\n"; |
| 1590 | |
John Criswell | 57bbfce | 2004-10-20 14:38:39 +0000 | [diff] [blame] | 1591 | printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(1), 2); |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1592 | printBranchToBlock(I.getParent(), I.getSuccessor(1), 2); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | Out << " }\n"; |
| 1596 | } else { |
John Criswell | 57bbfce | 2004-10-20 14:38:39 +0000 | [diff] [blame] | 1597 | printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(0), 0); |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1598 | printBranchToBlock(I.getParent(), I.getSuccessor(0), 0); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1599 | } |
| 1600 | Out << "\n"; |
| 1601 | } |
| 1602 | |
Chris Lattner | 84e6665 | 2003-05-03 07:11:00 +0000 | [diff] [blame] | 1603 | // PHI nodes get copied into temporary values at the end of predecessor basic |
| 1604 | // blocks. We now need to copy these temporary values into the REAL value for |
| 1605 | // the PHI. |
| 1606 | void CWriter::visitPHINode(PHINode &I) { |
| 1607 | writeOperand(&I); |
| 1608 | Out << "__PHI_TEMPORARY"; |
| 1609 | } |
| 1610 | |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1611 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1612 | void CWriter::visitBinaryOperator(Instruction &I) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1613 | // binary instructions, shift instructions, setCond instructions. |
Chris Lattner | f5612b76 | 2003-04-23 19:09:22 +0000 | [diff] [blame] | 1614 | assert(!isa<PointerType>(I.getType())); |
Brian Gaeke | 031a112 | 2003-06-23 20:00:51 +0000 | [diff] [blame] | 1615 | |
| 1616 | // We must cast the results of binary operations which might be promoted. |
| 1617 | bool needsCast = false; |
| 1618 | if ((I.getType() == Type::UByteTy) || (I.getType() == Type::SByteTy) |
Brian Gaeke | e99f4cf | 2003-06-25 03:05:33 +0000 | [diff] [blame] | 1619 | || (I.getType() == Type::UShortTy) || (I.getType() == Type::ShortTy) |
| 1620 | || (I.getType() == Type::FloatTy)) { |
Brian Gaeke | 031a112 | 2003-06-23 20:00:51 +0000 | [diff] [blame] | 1621 | needsCast = true; |
| 1622 | Out << "(("; |
Chris Lattner | 7635ea4 | 2003-11-03 01:01:59 +0000 | [diff] [blame] | 1623 | printType(Out, I.getType()); |
Brian Gaeke | 031a112 | 2003-06-23 20:00:51 +0000 | [diff] [blame] | 1624 | Out << ")("; |
| 1625 | } |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1626 | |
Chris Lattner | 6d9b69f | 2005-03-03 21:12:04 +0000 | [diff] [blame] | 1627 | // If this is a negation operation, print it out as such. For FP, we don't |
| 1628 | // want to print "-0.0 - X". |
| 1629 | if (BinaryOperator::isNeg(&I)) { |
John Criswell | ce4e1e4 | 2005-07-14 19:41:16 +0000 | [diff] [blame] | 1630 | Out << "-("; |
Chris Lattner | 6d9b69f | 2005-03-03 21:12:04 +0000 | [diff] [blame] | 1631 | writeOperand(BinaryOperator::getNegArgument(cast<BinaryOperator>(&I))); |
John Criswell | ce4e1e4 | 2005-07-14 19:41:16 +0000 | [diff] [blame] | 1632 | Out << ")"; |
Chris Lattner | 57da252 | 2005-08-23 20:22:50 +0000 | [diff] [blame] | 1633 | } else if (I.getOpcode() == Instruction::Rem && |
| 1634 | I.getType()->isFloatingPoint()) { |
| 1635 | // Output a call to fmod/fmodf instead of emitting a%b |
| 1636 | if (I.getType() == Type::FloatTy) |
| 1637 | Out << "fmodf("; |
| 1638 | else |
| 1639 | Out << "fmod("; |
| 1640 | writeOperand(I.getOperand(0)); |
| 1641 | Out << ", "; |
| 1642 | writeOperand(I.getOperand(1)); |
| 1643 | Out << ")"; |
Chris Lattner | 6d9b69f | 2005-03-03 21:12:04 +0000 | [diff] [blame] | 1644 | } else { |
| 1645 | writeOperand(I.getOperand(0)); |
| 1646 | |
| 1647 | switch (I.getOpcode()) { |
| 1648 | case Instruction::Add: Out << " + "; break; |
| 1649 | case Instruction::Sub: Out << " - "; break; |
| 1650 | case Instruction::Mul: Out << '*'; break; |
| 1651 | case Instruction::Div: Out << '/'; break; |
| 1652 | case Instruction::Rem: Out << '%'; break; |
| 1653 | case Instruction::And: Out << " & "; break; |
| 1654 | case Instruction::Or: Out << " | "; break; |
| 1655 | case Instruction::Xor: Out << " ^ "; break; |
| 1656 | case Instruction::SetEQ: Out << " == "; break; |
| 1657 | case Instruction::SetNE: Out << " != "; break; |
| 1658 | case Instruction::SetLE: Out << " <= "; break; |
| 1659 | case Instruction::SetGE: Out << " >= "; break; |
| 1660 | case Instruction::SetLT: Out << " < "; break; |
| 1661 | case Instruction::SetGT: Out << " > "; break; |
| 1662 | case Instruction::Shl : Out << " << "; break; |
| 1663 | case Instruction::Shr : Out << " >> "; break; |
| 1664 | default: std::cerr << "Invalid operator type!" << I; abort(); |
| 1665 | } |
| 1666 | |
| 1667 | writeOperand(I.getOperand(1)); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
Brian Gaeke | 031a112 | 2003-06-23 20:00:51 +0000 | [diff] [blame] | 1670 | if (needsCast) { |
| 1671 | Out << "))"; |
| 1672 | } |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1675 | void CWriter::visitCastInst(CastInst &I) { |
Chris Lattner | d13bd22 | 2003-06-01 03:36:51 +0000 | [diff] [blame] | 1676 | if (I.getType() == Type::BoolTy) { |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1677 | Out << '('; |
Chris Lattner | d13bd22 | 2003-06-01 03:36:51 +0000 | [diff] [blame] | 1678 | writeOperand(I.getOperand(0)); |
| 1679 | Out << " != 0)"; |
| 1680 | return; |
| 1681 | } |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1682 | Out << '('; |
Chris Lattner | 7635ea4 | 2003-11-03 01:01:59 +0000 | [diff] [blame] | 1683 | printType(Out, I.getType()); |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1684 | Out << ')'; |
Chris Lattner | f5612b76 | 2003-04-23 19:09:22 +0000 | [diff] [blame] | 1685 | if (isa<PointerType>(I.getType())&&I.getOperand(0)->getType()->isIntegral() || |
| 1686 | isa<PointerType>(I.getOperand(0)->getType())&&I.getType()->isIntegral()) { |
| 1687 | // Avoid "cast to pointer from integer of different size" warnings |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1688 | Out << "(long)"; |
Chris Lattner | f5612b76 | 2003-04-23 19:09:22 +0000 | [diff] [blame] | 1689 | } |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1690 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1691 | writeOperand(I.getOperand(0)); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
Chris Lattner | 9f24a07 | 2004-03-12 05:52:14 +0000 | [diff] [blame] | 1694 | void CWriter::visitSelectInst(SelectInst &I) { |
| 1695 | Out << "(("; |
| 1696 | writeOperand(I.getCondition()); |
| 1697 | Out << ") ? ("; |
| 1698 | writeOperand(I.getTrueValue()); |
| 1699 | Out << ") : ("; |
| 1700 | writeOperand(I.getFalseValue()); |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1701 | Out << "))"; |
Chris Lattner | 9f24a07 | 2004-03-12 05:52:14 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | |
Chris Lattner | c242143 | 2004-05-09 04:30:20 +0000 | [diff] [blame] | 1705 | void CWriter::lowerIntrinsics(Function &F) { |
| 1706 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 1707 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) |
| 1708 | if (CallInst *CI = dyn_cast<CallInst>(I++)) |
| 1709 | if (Function *F = CI->getCalledFunction()) |
| 1710 | switch (F->getIntrinsicID()) { |
| 1711 | case Intrinsic::not_intrinsic: |
| 1712 | case Intrinsic::vastart: |
| 1713 | case Intrinsic::vacopy: |
| 1714 | case Intrinsic::vaend: |
| 1715 | case Intrinsic::returnaddress: |
| 1716 | case Intrinsic::frameaddress: |
| 1717 | case Intrinsic::setjmp: |
| 1718 | case Intrinsic::longjmp: |
Chris Lattner | 33e9d29 | 2005-02-28 19:29:46 +0000 | [diff] [blame] | 1719 | case Intrinsic::prefetch: |
Jim Laskey | 7075d6f | 2006-03-08 19:31:15 +0000 | [diff] [blame] | 1720 | case Intrinsic::dbg_stoppoint: |
Chris Lattner | 1e14289 | 2006-09-09 06:17:12 +0000 | [diff] [blame] | 1721 | case Intrinsic::powi_f32: |
| 1722 | case Intrinsic::powi_f64: |
Chris Lattner | c242143 | 2004-05-09 04:30:20 +0000 | [diff] [blame] | 1723 | // We directly implement these intrinsics |
| 1724 | break; |
| 1725 | default: |
Chris Lattner | 8724215 | 2006-03-13 23:09:05 +0000 | [diff] [blame] | 1726 | // If this is an intrinsic that directly corresponds to a GCC |
| 1727 | // builtin, we handle it. |
| 1728 | const char *BuiltinName = ""; |
| 1729 | #define GET_GCC_BUILTIN_NAME |
| 1730 | #include "llvm/Intrinsics.gen" |
| 1731 | #undef GET_GCC_BUILTIN_NAME |
| 1732 | // If we handle it, don't lower it. |
| 1733 | if (BuiltinName[0]) break; |
| 1734 | |
Chris Lattner | c242143 | 2004-05-09 04:30:20 +0000 | [diff] [blame] | 1735 | // All other intrinsic calls we must lower. |
Chris Lattner | 67c2d18 | 2005-03-18 16:12:37 +0000 | [diff] [blame] | 1736 | Instruction *Before = 0; |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1737 | if (CI != &BB->front()) |
Chris Lattner | 67c2d18 | 2005-03-18 16:12:37 +0000 | [diff] [blame] | 1738 | Before = prior(BasicBlock::iterator(CI)); |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1739 | |
Chris Lattner | c242143 | 2004-05-09 04:30:20 +0000 | [diff] [blame] | 1740 | IL.LowerIntrinsicCall(CI); |
| 1741 | if (Before) { // Move iterator to instruction after call |
| 1742 | I = Before; ++I; |
| 1743 | } else { |
| 1744 | I = BB->begin(); |
Chris Lattner | 4ef5137 | 2004-02-14 00:31:10 +0000 | [diff] [blame] | 1745 | } |
Chris Lattner | 8724215 | 2006-03-13 23:09:05 +0000 | [diff] [blame] | 1746 | break; |
Chris Lattner | c242143 | 2004-05-09 04:30:20 +0000 | [diff] [blame] | 1747 | } |
Chris Lattner | 4ef5137 | 2004-02-14 00:31:10 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
| 1750 | |
| 1751 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1752 | void CWriter::visitCallInst(CallInst &I) { |
Chris Lattner | 8724215 | 2006-03-13 23:09:05 +0000 | [diff] [blame] | 1753 | bool WroteCallee = false; |
| 1754 | |
Chris Lattner | 18ac3c8 | 2003-05-08 18:41:45 +0000 | [diff] [blame] | 1755 | // Handle intrinsic function calls first... |
| 1756 | if (Function *F = I.getCalledFunction()) |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 1757 | if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) { |
Chris Lattner | 18ac3c8 | 2003-05-08 18:41:45 +0000 | [diff] [blame] | 1758 | switch (ID) { |
Chris Lattner | 8724215 | 2006-03-13 23:09:05 +0000 | [diff] [blame] | 1759 | default: { |
| 1760 | // If this is an intrinsic that directly corresponds to a GCC |
| 1761 | // builtin, we emit it here. |
| 1762 | const char *BuiltinName = ""; |
| 1763 | #define GET_GCC_BUILTIN_NAME |
| 1764 | #include "llvm/Intrinsics.gen" |
| 1765 | #undef GET_GCC_BUILTIN_NAME |
| 1766 | assert(BuiltinName[0] && "Unknown LLVM intrinsic!"); |
| 1767 | |
| 1768 | Out << BuiltinName; |
| 1769 | WroteCallee = true; |
| 1770 | break; |
| 1771 | } |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1772 | case Intrinsic::vastart: |
Chris Lattner | 4d45bd0 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 1773 | Out << "0; "; |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1774 | |
Andrew Lenharth | 558bc88 | 2005-06-18 18:34:52 +0000 | [diff] [blame] | 1775 | Out << "va_start(*(va_list*)"; |
| 1776 | writeOperand(I.getOperand(1)); |
| 1777 | Out << ", "; |
Chris Lattner | 18ac3c8 | 2003-05-08 18:41:45 +0000 | [diff] [blame] | 1778 | // Output the last argument to the enclosing function... |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 1779 | if (I.getParent()->getParent()->arg_empty()) { |
Chris Lattner | 4065ef9 | 2003-10-23 18:39:22 +0000 | [diff] [blame] | 1780 | std::cerr << "The C backend does not currently support zero " |
| 1781 | << "argument varargs functions, such as '" |
| 1782 | << I.getParent()->getParent()->getName() << "'!\n"; |
| 1783 | abort(); |
| 1784 | } |
Chris Lattner | c5e7df1 | 2005-03-15 04:59:17 +0000 | [diff] [blame] | 1785 | writeOperand(--I.getParent()->getParent()->arg_end()); |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1786 | Out << ')'; |
Chris Lattner | 18ac3c8 | 2003-05-08 18:41:45 +0000 | [diff] [blame] | 1787 | return; |
Chris Lattner | 317201d | 2004-03-13 00:24:00 +0000 | [diff] [blame] | 1788 | case Intrinsic::vaend: |
Chris Lattner | f8e6f4f | 2004-08-10 00:19:16 +0000 | [diff] [blame] | 1789 | if (!isa<ConstantPointerNull>(I.getOperand(1))) { |
Andrew Lenharth | 558bc88 | 2005-06-18 18:34:52 +0000 | [diff] [blame] | 1790 | Out << "0; va_end(*(va_list*)"; |
Chris Lattner | f8e6f4f | 2004-08-10 00:19:16 +0000 | [diff] [blame] | 1791 | writeOperand(I.getOperand(1)); |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1792 | Out << ')'; |
Chris Lattner | f8e6f4f | 2004-08-10 00:19:16 +0000 | [diff] [blame] | 1793 | } else { |
| 1794 | Out << "va_end(*(va_list*)0)"; |
| 1795 | } |
Chris Lattner | 18ac3c8 | 2003-05-08 18:41:45 +0000 | [diff] [blame] | 1796 | return; |
Chris Lattner | 317201d | 2004-03-13 00:24:00 +0000 | [diff] [blame] | 1797 | case Intrinsic::vacopy: |
Andrew Lenharth | 558bc88 | 2005-06-18 18:34:52 +0000 | [diff] [blame] | 1798 | Out << "0; "; |
| 1799 | Out << "va_copy(*(va_list*)"; |
Chris Lattner | 18ac3c8 | 2003-05-08 18:41:45 +0000 | [diff] [blame] | 1800 | writeOperand(I.getOperand(1)); |
Andrew Lenharth | 213e557 | 2005-06-22 21:04:42 +0000 | [diff] [blame] | 1801 | Out << ", *(va_list*)"; |
Andrew Lenharth | 558bc88 | 2005-06-18 18:34:52 +0000 | [diff] [blame] | 1802 | writeOperand(I.getOperand(2)); |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1803 | Out << ')'; |
Chris Lattner | 18ac3c8 | 2003-05-08 18:41:45 +0000 | [diff] [blame] | 1804 | return; |
Chris Lattner | fe1554a | 2004-02-14 02:55:36 +0000 | [diff] [blame] | 1805 | case Intrinsic::returnaddress: |
| 1806 | Out << "__builtin_return_address("; |
| 1807 | writeOperand(I.getOperand(1)); |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1808 | Out << ')'; |
Chris Lattner | fe1554a | 2004-02-14 02:55:36 +0000 | [diff] [blame] | 1809 | return; |
| 1810 | case Intrinsic::frameaddress: |
| 1811 | Out << "__builtin_frame_address("; |
| 1812 | writeOperand(I.getOperand(1)); |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1813 | Out << ')'; |
Chris Lattner | fe1554a | 2004-02-14 02:55:36 +0000 | [diff] [blame] | 1814 | return; |
Chris Lattner | 1e14289 | 2006-09-09 06:17:12 +0000 | [diff] [blame] | 1815 | case Intrinsic::powi_f32: |
| 1816 | case Intrinsic::powi_f64: |
| 1817 | Out << "__builtin_powi("; |
| 1818 | writeOperand(I.getOperand(1)); |
| 1819 | Out << ", "; |
| 1820 | writeOperand(I.getOperand(2)); |
| 1821 | Out << ')'; |
| 1822 | return; |
Chris Lattner | e42cde2 | 2004-02-15 22:51:47 +0000 | [diff] [blame] | 1823 | case Intrinsic::setjmp: |
Chris Lattner | 475c553 | 2006-06-06 21:45:47 +0000 | [diff] [blame] | 1824 | #if defined(HAVE__SETJMP) && defined(HAVE__LONGJMP) |
| 1825 | Out << "_"; // Use _setjmp on systems that support it! |
| 1826 | #endif |
Chris Lattner | e42cde2 | 2004-02-15 22:51:47 +0000 | [diff] [blame] | 1827 | Out << "setjmp(*(jmp_buf*)"; |
| 1828 | writeOperand(I.getOperand(1)); |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1829 | Out << ')'; |
Chris Lattner | e42cde2 | 2004-02-15 22:51:47 +0000 | [diff] [blame] | 1830 | return; |
| 1831 | case Intrinsic::longjmp: |
Chris Lattner | 475c553 | 2006-06-06 21:45:47 +0000 | [diff] [blame] | 1832 | #if defined(HAVE__SETJMP) && defined(HAVE__LONGJMP) |
| 1833 | Out << "_"; // Use _longjmp on systems that support it! |
| 1834 | #endif |
Chris Lattner | e42cde2 | 2004-02-15 22:51:47 +0000 | [diff] [blame] | 1835 | Out << "longjmp(*(jmp_buf*)"; |
| 1836 | writeOperand(I.getOperand(1)); |
| 1837 | Out << ", "; |
| 1838 | writeOperand(I.getOperand(2)); |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1839 | Out << ')'; |
Chris Lattner | e42cde2 | 2004-02-15 22:51:47 +0000 | [diff] [blame] | 1840 | return; |
Chris Lattner | 33e9d29 | 2005-02-28 19:29:46 +0000 | [diff] [blame] | 1841 | case Intrinsic::prefetch: |
Chris Lattner | ca76f35 | 2005-02-28 19:36:15 +0000 | [diff] [blame] | 1842 | Out << "LLVM_PREFETCH((const void *)"; |
Chris Lattner | 33e9d29 | 2005-02-28 19:29:46 +0000 | [diff] [blame] | 1843 | writeOperand(I.getOperand(1)); |
| 1844 | Out << ", "; |
| 1845 | writeOperand(I.getOperand(2)); |
| 1846 | Out << ", "; |
| 1847 | writeOperand(I.getOperand(3)); |
| 1848 | Out << ")"; |
Chris Lattner | ca76f35 | 2005-02-28 19:36:15 +0000 | [diff] [blame] | 1849 | return; |
Jim Laskey | 7075d6f | 2006-03-08 19:31:15 +0000 | [diff] [blame] | 1850 | case Intrinsic::dbg_stoppoint: { |
| 1851 | // If we use writeOperand directly we get a "u" suffix which is rejected |
| 1852 | // by gcc. |
Jim Laskey | 580418e | 2006-03-23 18:08:29 +0000 | [diff] [blame] | 1853 | DbgStopPointInst &SPI = cast<DbgStopPointInst>(I); |
Jim Laskey | 7075d6f | 2006-03-08 19:31:15 +0000 | [diff] [blame] | 1854 | |
| 1855 | Out << "\n#line " |
Jim Laskey | 580418e | 2006-03-23 18:08:29 +0000 | [diff] [blame] | 1856 | << SPI.getLine() |
| 1857 | << " \"" << SPI.getDirectory() |
| 1858 | << SPI.getFileName() << "\"\n"; |
Jim Laskey | 7075d6f | 2006-03-08 19:31:15 +0000 | [diff] [blame] | 1859 | return; |
| 1860 | } |
Chris Lattner | 18ac3c8 | 2003-05-08 18:41:45 +0000 | [diff] [blame] | 1861 | } |
| 1862 | } |
| 1863 | |
Chris Lattner | 4394d51 | 2004-11-13 22:21:56 +0000 | [diff] [blame] | 1864 | Value *Callee = I.getCalledValue(); |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1865 | |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 1866 | // If this is a call to a struct-return function, assign to the first |
| 1867 | // parameter instead of passing it to the call. |
| 1868 | bool isStructRet = I.getCallingConv() == CallingConv::CSRet; |
| 1869 | if (isStructRet) { |
| 1870 | Out << "*("; |
| 1871 | writeOperand(I.getOperand(1)); |
| 1872 | Out << ") = "; |
| 1873 | } |
| 1874 | |
Chris Lattner | fe673d9 | 2005-05-06 06:53:07 +0000 | [diff] [blame] | 1875 | if (I.isTailCall()) Out << " /*tail*/ "; |
Chris Lattner | 4394d51 | 2004-11-13 22:21:56 +0000 | [diff] [blame] | 1876 | |
| 1877 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1878 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 1879 | |
| 1880 | if (!WroteCallee) { |
| 1881 | // If this is an indirect call to a struct return function, we need to cast |
| 1882 | // the pointer. |
| 1883 | bool NeedsCast = isStructRet && !isa<Function>(Callee); |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1884 | |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 1885 | // GCC is a real PITA. It does not permit codegening casts of functions to |
| 1886 | // function pointers if they are in a call (it generates a trap instruction |
| 1887 | // instead!). We work around this by inserting a cast to void* in between |
| 1888 | // the function and the function pointer cast. Unfortunately, we can't just |
| 1889 | // form the constant expression here, because the folder will immediately |
| 1890 | // nuke it. |
| 1891 | // |
| 1892 | // Note finally, that this is completely unsafe. ANSI C does not guarantee |
| 1893 | // that void* and function pointers have the same size. :( To deal with this |
| 1894 | // in the common case, we handle casts where the number of arguments passed |
| 1895 | // match exactly. |
| 1896 | // |
| 1897 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Callee)) |
| 1898 | if (CE->getOpcode() == Instruction::Cast) |
| 1899 | if (Function *RF = dyn_cast<Function>(CE->getOperand(0))) { |
| 1900 | NeedsCast = true; |
| 1901 | Callee = RF; |
| 1902 | } |
| 1903 | |
| 1904 | if (NeedsCast) { |
| 1905 | // Ok, just cast the pointer type. |
| 1906 | Out << "(("; |
| 1907 | if (!isStructRet) |
| 1908 | printType(Out, I.getCalledValue()->getType()); |
| 1909 | else |
| 1910 | printStructReturnPointerFunctionType(Out, |
| 1911 | cast<PointerType>(I.getCalledValue()->getType())); |
| 1912 | Out << ")(void*)"; |
| 1913 | } |
| 1914 | writeOperand(Callee); |
| 1915 | if (NeedsCast) Out << ')'; |
| 1916 | } |
| 1917 | |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1918 | Out << '('; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1919 | |
Chris Lattner | 4394d51 | 2004-11-13 22:21:56 +0000 | [diff] [blame] | 1920 | unsigned NumDeclaredParams = FTy->getNumParams(); |
| 1921 | |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 1922 | CallSite::arg_iterator AI = I.op_begin()+1, AE = I.op_end(); |
| 1923 | unsigned ArgNo = 0; |
| 1924 | if (isStructRet) { // Skip struct return argument. |
| 1925 | ++AI; |
| 1926 | ++ArgNo; |
| 1927 | } |
| 1928 | |
| 1929 | bool PrintedArg = false; |
| 1930 | for (; AI != AE; ++AI, ++ArgNo) { |
| 1931 | if (PrintedArg) Out << ", "; |
| 1932 | if (ArgNo < NumDeclaredParams && |
| 1933 | (*AI)->getType() != FTy->getParamType(ArgNo)) { |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1934 | Out << '('; |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 1935 | printType(Out, FTy->getParamType(ArgNo)); |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1936 | Out << ')'; |
Chris Lattner | 4394d51 | 2004-11-13 22:21:56 +0000 | [diff] [blame] | 1937 | } |
Chris Lattner | 9bda5f5 | 2003-06-28 17:53:05 +0000 | [diff] [blame] | 1938 | writeOperand(*AI); |
Chris Lattner | 0c54d89 | 2006-05-23 23:39:48 +0000 | [diff] [blame] | 1939 | PrintedArg = true; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1940 | } |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1941 | Out << ')'; |
Misha Brukman | d6a29a5 | 2005-04-20 16:05:03 +0000 | [diff] [blame] | 1942 | } |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1943 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1944 | void CWriter::visitMallocInst(MallocInst &I) { |
Chris Lattner | f31182a | 2004-02-13 23:18:48 +0000 | [diff] [blame] | 1945 | assert(0 && "lowerallocations pass didn't work!"); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1948 | void CWriter::visitAllocaInst(AllocaInst &I) { |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1949 | Out << '('; |
Nick Hildenbrandt | 9850237 | 2002-11-18 20:55:50 +0000 | [diff] [blame] | 1950 | printType(Out, I.getType()); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1951 | Out << ") alloca(sizeof("; |
Nick Hildenbrandt | 9850237 | 2002-11-18 20:55:50 +0000 | [diff] [blame] | 1952 | printType(Out, I.getType()->getElementType()); |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1953 | Out << ')'; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1954 | if (I.isArrayAllocation()) { |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1955 | Out << " * " ; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1956 | writeOperand(I.getOperand(0)); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1957 | } |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1958 | Out << ')'; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1959 | } |
| 1960 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 1961 | void CWriter::visitFreeInst(FreeInst &I) { |
Chris Lattner | f31182a | 2004-02-13 23:18:48 +0000 | [diff] [blame] | 1962 | assert(0 && "lowerallocations pass didn't work!"); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1963 | } |
| 1964 | |
Chris Lattner | 23e9070 | 2003-11-25 20:49:55 +0000 | [diff] [blame] | 1965 | void CWriter::printIndexingExpression(Value *Ptr, gep_type_iterator I, |
| 1966 | gep_type_iterator E) { |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 1967 | bool HasImplicitAddress = false; |
| 1968 | // If accessing a global value with no indexing, avoid *(&GV) syndrome |
| 1969 | if (GlobalValue *V = dyn_cast<GlobalValue>(Ptr)) { |
| 1970 | HasImplicitAddress = true; |
Chris Lattner | a8ab89e | 2003-06-17 04:39:14 +0000 | [diff] [blame] | 1971 | } else if (isDirectAlloca(Ptr)) { |
| 1972 | HasImplicitAddress = true; |
Chris Lattner | d0c668c | 2002-05-09 21:18:38 +0000 | [diff] [blame] | 1973 | } |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1974 | |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 1975 | if (I == E) { |
| 1976 | if (!HasImplicitAddress) |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1977 | Out << '*'; // Implicit zero first argument: '*x' is equivalent to 'x[0]' |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1978 | |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 1979 | writeOperandInternal(Ptr); |
| 1980 | return; |
| 1981 | } |
| 1982 | |
Chris Lattner | 23e9070 | 2003-11-25 20:49:55 +0000 | [diff] [blame] | 1983 | const Constant *CI = dyn_cast<Constant>(I.getOperand()); |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 1984 | if (HasImplicitAddress && (!CI || !CI->isNullValue())) |
| 1985 | Out << "(&"; |
| 1986 | |
| 1987 | writeOperandInternal(Ptr); |
| 1988 | |
Nick Hildenbrandt | e548f00 | 2002-09-25 20:29:26 +0000 | [diff] [blame] | 1989 | if (HasImplicitAddress && (!CI || !CI->isNullValue())) { |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 1990 | Out << ')'; |
Nick Hildenbrandt | e548f00 | 2002-09-25 20:29:26 +0000 | [diff] [blame] | 1991 | HasImplicitAddress = false; // HIA is only true if we haven't addressed yet |
| 1992 | } |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 1993 | |
Nick Hildenbrandt | e548f00 | 2002-09-25 20:29:26 +0000 | [diff] [blame] | 1994 | assert(!HasImplicitAddress || (CI && CI->isNullValue()) && |
| 1995 | "Can only have implicit address with direct accessing"); |
| 1996 | |
| 1997 | if (HasImplicitAddress) { |
| 1998 | ++I; |
Chris Lattner | 23e9070 | 2003-11-25 20:49:55 +0000 | [diff] [blame] | 1999 | } else if (CI && CI->isNullValue()) { |
| 2000 | gep_type_iterator TmpI = I; ++TmpI; |
| 2001 | |
Nick Hildenbrandt | e548f00 | 2002-09-25 20:29:26 +0000 | [diff] [blame] | 2002 | // Print out the -> operator if possible... |
Chris Lattner | 23e9070 | 2003-11-25 20:49:55 +0000 | [diff] [blame] | 2003 | if (TmpI != E && isa<StructType>(*TmpI)) { |
Chris Lattner | df35a1c | 2002-08-19 23:09:46 +0000 | [diff] [blame] | 2004 | Out << (HasImplicitAddress ? "." : "->"); |
Chris Lattner | bc771c5 | 2003-11-25 23:44:40 +0000 | [diff] [blame] | 2005 | Out << "field" << cast<ConstantUInt>(TmpI.getOperand())->getValue(); |
Chris Lattner | 23e9070 | 2003-11-25 20:49:55 +0000 | [diff] [blame] | 2006 | I = ++TmpI; |
| 2007 | } |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 2008 | } |
Chris Lattner | 5dfe767 | 2002-08-22 22:48:55 +0000 | [diff] [blame] | 2009 | |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 2010 | for (; I != E; ++I) |
Chris Lattner | 23e9070 | 2003-11-25 20:49:55 +0000 | [diff] [blame] | 2011 | if (isa<StructType>(*I)) { |
| 2012 | Out << ".field" << cast<ConstantUInt>(I.getOperand())->getValue(); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 2013 | } else { |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 2014 | Out << '['; |
Chris Lattner | 23e9070 | 2003-11-25 20:49:55 +0000 | [diff] [blame] | 2015 | writeOperand(I.getOperand()); |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 2016 | Out << ']'; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 2017 | } |
| 2018 | } |
| 2019 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 2020 | void CWriter::visitLoadInst(LoadInst &I) { |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 2021 | Out << '*'; |
Chris Lattner | 9d30e22 | 2005-02-14 16:47:52 +0000 | [diff] [blame] | 2022 | if (I.isVolatile()) { |
Chris Lattner | 8399e02 | 2005-02-15 05:52:14 +0000 | [diff] [blame] | 2023 | Out << "(("; |
Chris Lattner | b94388a | 2005-09-27 20:52:44 +0000 | [diff] [blame] | 2024 | printType(Out, I.getType(), "volatile*"); |
| 2025 | Out << ")"; |
Chris Lattner | 9d30e22 | 2005-02-14 16:47:52 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
Chris Lattner | 5dfe767 | 2002-08-22 22:48:55 +0000 | [diff] [blame] | 2028 | writeOperand(I.getOperand(0)); |
Chris Lattner | 9d30e22 | 2005-02-14 16:47:52 +0000 | [diff] [blame] | 2029 | |
| 2030 | if (I.isVolatile()) |
Misha Brukman | 23ba0c5 | 2005-03-08 00:26:08 +0000 | [diff] [blame] | 2031 | Out << ')'; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 2034 | void CWriter::visitStoreInst(StoreInst &I) { |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 2035 | Out << '*'; |
Chris Lattner | 9d30e22 | 2005-02-14 16:47:52 +0000 | [diff] [blame] | 2036 | if (I.isVolatile()) { |
Chris Lattner | 8399e02 | 2005-02-15 05:52:14 +0000 | [diff] [blame] | 2037 | Out << "(("; |
Chris Lattner | b94388a | 2005-09-27 20:52:44 +0000 | [diff] [blame] | 2038 | printType(Out, I.getOperand(0)->getType(), " volatile*"); |
| 2039 | Out << ")"; |
Chris Lattner | 9d30e22 | 2005-02-14 16:47:52 +0000 | [diff] [blame] | 2040 | } |
Chris Lattner | 5dfe767 | 2002-08-22 22:48:55 +0000 | [diff] [blame] | 2041 | writeOperand(I.getPointerOperand()); |
Misha Brukman | 23ba0c5 | 2005-03-08 00:26:08 +0000 | [diff] [blame] | 2042 | if (I.isVolatile()) Out << ')'; |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 2043 | Out << " = "; |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 2044 | writeOperand(I.getOperand(0)); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
Chris Lattner | bb03efd | 2002-06-25 15:57:03 +0000 | [diff] [blame] | 2047 | void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) { |
Misha Brukman | b70aaa6 | 2005-02-14 18:52:35 +0000 | [diff] [blame] | 2048 | Out << '&'; |
Chris Lattner | 23e9070 | 2003-11-25 20:49:55 +0000 | [diff] [blame] | 2049 | printIndexingExpression(I.getPointerOperand(), gep_type_begin(I), |
| 2050 | gep_type_end(I)); |
Chris Lattner | 4fbf26d | 2002-05-09 20:53:56 +0000 | [diff] [blame] | 2051 | } |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 2052 | |
Chris Lattner | 4d45bd0 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 2053 | void CWriter::visitVAArgInst(VAArgInst &I) { |
Andrew Lenharth | 558bc88 | 2005-06-18 18:34:52 +0000 | [diff] [blame] | 2054 | Out << "va_arg(*(va_list*)"; |
Chris Lattner | 4d45bd0 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 2055 | writeOperand(I.getOperand(0)); |
Andrew Lenharth | 558bc88 | 2005-06-18 18:34:52 +0000 | [diff] [blame] | 2056 | Out << ", "; |
Chris Lattner | 7635ea4 | 2003-11-03 01:01:59 +0000 | [diff] [blame] | 2057 | printType(Out, I.getType()); |
Andrew Lenharth | 558bc88 | 2005-06-18 18:34:52 +0000 | [diff] [blame] | 2058 | Out << ");\n "; |
Chris Lattner | 4d45bd0 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
Sumant Kowshik | 9ddc86c | 2002-05-08 18:09:58 +0000 | [diff] [blame] | 2061 | //===----------------------------------------------------------------------===// |
| 2062 | // External Interface declaration |
| 2063 | //===----------------------------------------------------------------------===// |
| 2064 | |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 2065 | bool CTargetMachine::addPassesToEmitWholeFile(PassManager &PM, |
| 2066 | std::ostream &o, |
| 2067 | CodeGenFileType FileType, |
| 2068 | bool Fast) { |
Chris Lattner | 0431c96 | 2005-06-25 02:48:37 +0000 | [diff] [blame] | 2069 | if (FileType != TargetMachine::AssemblyFile) return true; |
| 2070 | |
Chris Lattner | 99c59e8 | 2004-05-23 21:23:35 +0000 | [diff] [blame] | 2071 | PM.add(createLowerGCPass()); |
Chris Lattner | f7fe494 | 2005-03-03 01:04:50 +0000 | [diff] [blame] | 2072 | PM.add(createLowerAllocationsPass(true)); |
Chris Lattner | 94f4f8e | 2004-02-13 23:00:29 +0000 | [diff] [blame] | 2073 | PM.add(createLowerInvokePass()); |
Chris Lattner | bad13eb | 2005-11-02 17:42:58 +0000 | [diff] [blame] | 2074 | PM.add(createCFGSimplificationPass()); // clean up after lower invoke. |
Chris Lattner | f9a0532 | 2006-02-13 22:22:42 +0000 | [diff] [blame] | 2075 | PM.add(new CBackendNameAllUsedStructsAndMergeFunctions()); |
Chris Lattner | bc641b9 | 2006-03-23 05:43:16 +0000 | [diff] [blame] | 2076 | PM.add(new CWriter(o)); |
Chris Lattner | f31182a | 2004-02-13 23:18:48 +0000 | [diff] [blame] | 2077 | return false; |
| 2078 | } |