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