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