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