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