blob: ae3ba809408ba33918f7e589a141adc7ed751bb1 [file] [log] [blame]
Chris Lattner3af3ba82002-05-09 21:31:18 +00001//===-- Writer.cpp - Library for converting LLVM code to C ----------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
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.
7//
8//===----------------------------------------------------------------------===//
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00009//
Chris Lattner897bf0d2004-02-13 06:18:21 +000010// This library converts LLVM code to C code, compilable by GCC and other C
11// compilers.
Chris Lattner16c7bb22002-05-09 02:28:59 +000012//
Chris Lattneredd8ce12003-05-03 03:14:35 +000013//===----------------------------------------------------------------------===//
Chris Lattner84e66652003-05-03 07:11:00 +000014
Chris Lattnerf31182a2004-02-13 23:18:48 +000015#include "CTargetMachine.h"
16#include "llvm/Target/TargetMachineImpls.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000017#include "llvm/Constants.h"
Chris Lattner2f5f51a2002-05-09 03:28:37 +000018#include "llvm/DerivedTypes.h"
19#include "llvm/Module.h"
Chris Lattnera9f5e052003-04-22 20:19:52 +000020#include "llvm/Instructions.h"
Chris Lattner0e9f93e2002-08-31 00:29:16 +000021#include "llvm/Pass.h"
Chris Lattner94f4f8e2004-02-13 23:00:29 +000022#include "llvm/PassManager.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000023#include "llvm/SymbolTable.h"
Chris Lattner18ac3c82003-05-08 18:41:45 +000024#include "llvm/Intrinsics.h"
Chris Lattner4ef51372004-02-14 00:31:10 +000025#include "llvm/IntrinsicLowering.h"
Chris Lattnerd1cf1b42002-09-20 23:26:33 +000026#include "llvm/Analysis/ConstantsScanner.h"
Chris Lattnercb3ad012004-05-09 20:41:32 +000027#include "llvm/Analysis/FindUsedTypes.h"
28#include "llvm/Analysis/LoopInfo.h"
Chris Lattner94f4f8e2004-02-13 23:00:29 +000029#include "llvm/Transforms/Scalar.h"
Chris Lattner23e90702003-11-25 20:49:55 +000030#include "llvm/Support/CallSite.h"
Chris Lattnera05e0ec2004-05-09 03:42:48 +000031#include "llvm/Support/CFG.h"
Chris Lattner23e90702003-11-25 20:49:55 +000032#include "llvm/Support/GetElementPtrTypeIterator.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000033#include "llvm/Support/InstVisitor.h"
Brian Gaeke49a178b2003-07-25 20:21:06 +000034#include "llvm/Support/Mangler.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000035#include "Support/StringExtras.h"
Chris Lattner2bcab1f2004-02-24 18:34:10 +000036#include "Config/config.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000037#include <algorithm>
Nick Hildenbrandt98502372002-11-18 20:55:50 +000038#include <sstream>
Chris Lattner897bf0d2004-02-13 06:18:21 +000039using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000040
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000041namespace {
Chris Lattner68758072004-05-09 06:20:51 +000042 /// NameAllUsedStructs - This pass inserts names for any unnamed structure
43 /// types that are used by the program.
44 ///
45 class CBackendNameAllUsedStructs : public Pass {
46 void getAnalysisUsage(AnalysisUsage &AU) const {
47 AU.addRequired<FindUsedTypes>();
48 }
49
50 virtual const char *getPassName() const {
51 return "C backend type canonicalizer";
52 }
53
54 virtual bool run(Module &M);
55 };
56
57 /// CWriter - This class is the main chunk of code that converts an LLVM
58 /// module to a C translation unit.
59 class CWriter : public FunctionPass, public InstVisitor<CWriter> {
Chris Lattneredd8ce12003-05-03 03:14:35 +000060 std::ostream &Out;
Chris Lattner4ef51372004-02-14 00:31:10 +000061 IntrinsicLowering &IL;
Brian Gaeked9fb37a2003-07-24 20:20:44 +000062 Mangler *Mang;
Chris Lattnercb3ad012004-05-09 20:41:32 +000063 LoopInfo *LI;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000064 const Module *TheModule;
Chris Lattneredd8ce12003-05-03 03:14:35 +000065 std::map<const Type *, std::string> TypeNames;
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +000066
Chris Lattneredd8ce12003-05-03 03:14:35 +000067 std::map<const ConstantFP *, unsigned> FPConstantMap;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000068 public:
Chris Lattner4ef51372004-02-14 00:31:10 +000069 CWriter(std::ostream &o, IntrinsicLowering &il) : Out(o), IL(il) {}
Chris Lattner0e9f93e2002-08-31 00:29:16 +000070
Chris Lattner94f4f8e2004-02-13 23:00:29 +000071 virtual const char *getPassName() const { return "C backend"; }
Chris Lattner0e9f93e2002-08-31 00:29:16 +000072
Chris Lattnercb3ad012004-05-09 20:41:32 +000073 void getAnalysisUsage(AnalysisUsage &AU) const {
74 AU.addRequired<LoopInfo>();
75 AU.setPreservesAll();
76 }
77
Chris Lattner68758072004-05-09 06:20:51 +000078 virtual bool doInitialization(Module &M);
Chris Lattner0e9f93e2002-08-31 00:29:16 +000079
Chris Lattner68758072004-05-09 06:20:51 +000080 bool runOnFunction(Function &F) {
Chris Lattnercb3ad012004-05-09 20:41:32 +000081 LI = &getAnalysis<LoopInfo>();
82
Chris Lattner68758072004-05-09 06:20:51 +000083 // Output all floating point constants that cannot be printed accurately.
84 printFloatingPointConstants(F);
85
86 lowerIntrinsics(F);
87 printFunction(F);
88 FPConstantMap.clear();
89 return false;
90 }
Chris Lattner0e9f93e2002-08-31 00:29:16 +000091
Chris Lattner68758072004-05-09 06:20:51 +000092 virtual bool doFinalization(Module &M) {
Chris Lattner0e9f93e2002-08-31 00:29:16 +000093 // Free memory...
Brian Gaeked9fb37a2003-07-24 20:20:44 +000094 delete Mang;
Chris Lattner0e9f93e2002-08-31 00:29:16 +000095 TypeNames.clear();
Chris Lattnercb3ad012004-05-09 20:41:32 +000096 return false;
Chris Lattner0e9f93e2002-08-31 00:29:16 +000097 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000098
Chris Lattneredd8ce12003-05-03 03:14:35 +000099 std::ostream &printType(std::ostream &Out, const Type *Ty,
100 const std::string &VariableName = "",
Chris Lattner7635ea42003-11-03 01:01:59 +0000101 bool IgnoreName = false);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000102
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000103 void writeOperand(Value *Operand);
104 void writeOperandInternal(Value *Operand);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000105
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000106 private :
Chris Lattnerc2421432004-05-09 04:30:20 +0000107 void lowerIntrinsics(Function &F);
Chris Lattner4ef51372004-02-14 00:31:10 +0000108
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000109 bool nameAllUsedStructureTypes(Module &M);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000110 void printModule(Module *M);
Chris Lattner68758072004-05-09 06:20:51 +0000111 void printModuleTypes(const SymbolTable &ST);
Chris Lattner2c601a72002-09-20 15:05:40 +0000112 void printContainedStructs(const Type *Ty, std::set<const StructType *> &);
Chris Lattner68758072004-05-09 06:20:51 +0000113 void printFloatingPointConstants(Function &F);
Chris Lattner4cda8352002-08-20 16:55:48 +0000114 void printFunctionSignature(const Function *F, bool Prototype);
115
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000116 void printFunction(Function &);
Chris Lattnercb3ad012004-05-09 20:41:32 +0000117 void printBasicBlock(BasicBlock *BB);
118 void printLoop(Loop *L);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000119
Chris Lattner6d492922002-08-19 21:32:41 +0000120 void printConstant(Constant *CPV);
121 void printConstantArray(ConstantArray *CPA);
122
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000123 // isInlinableInst - Attempt to inline instructions into their uses to build
124 // trees as much as possible. To do this, we have to consistently decide
125 // what is acceptable to inline, so that variable declarations don't get
126 // printed and an extra copy of the expr is not emitted.
127 //
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000128 static bool isInlinableInst(const Instruction &I) {
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000129 // Must be an expression, must be used exactly once. If it is dead, we
130 // emit it inline where it would go.
Chris Lattnerfd059242003-10-15 16:48:29 +0000131 if (I.getType() == Type::VoidTy || !I.hasOneUse() ||
Nick Hildenbrandt2cf2cbc2002-11-06 20:07:54 +0000132 isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I) ||
Chris Lattner4d45bd02003-10-18 05:57:43 +0000133 isa<LoadInst>(I) || isa<VAArgInst>(I) || isa<VANextInst>(I))
Chris Lattnerd4e8d312003-07-23 20:45:31 +0000134 // Don't inline a load across a store or other bad things!
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000135 return false;
136
137 // Only inline instruction it it's use is in the same BB as the inst.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000138 return I.getParent() == cast<Instruction>(I.use_back())->getParent();
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000139 }
140
Chris Lattnera8ab89e2003-06-17 04:39:14 +0000141 // isDirectAlloca - Define fixed sized allocas in the entry block as direct
142 // variables which are accessed with the & operator. This causes GCC to
143 // generate significantly better code than to emit alloca calls directly.
144 //
145 static const AllocaInst *isDirectAlloca(const Value *V) {
146 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
147 if (!AI) return false;
148 if (AI->isArrayAllocation())
149 return 0; // FIXME: we can also inline fixed size array allocas!
Chris Lattner02a3be02003-09-20 14:39:18 +0000150 if (AI->getParent() != &AI->getParent()->getParent()->getEntryBlock())
Chris Lattnera8ab89e2003-06-17 04:39:14 +0000151 return 0;
152 return AI;
153 }
154
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000155 // Instruction visitation functions
156 friend class InstVisitor<CWriter>;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000157
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000158 void visitReturnInst(ReturnInst &I);
159 void visitBranchInst(BranchInst &I);
Chris Lattnera9f5e052003-04-22 20:19:52 +0000160 void visitSwitchInst(SwitchInst &I);
Chris Lattnercb3ad012004-05-09 20:41:32 +0000161 void visitInvokeInst(InvokeInst &I) {
162 assert(0 && "Lowerinvoke pass didn't work!");
163 }
164
165 void visitUnwindInst(UnwindInst &I) {
166 assert(0 && "Lowerinvoke pass didn't work!");
167 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000168
Chris Lattner84e66652003-05-03 07:11:00 +0000169 void visitPHINode(PHINode &I);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000170 void visitBinaryOperator(Instruction &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000171
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000172 void visitCastInst (CastInst &I);
Chris Lattner9f24a072004-03-12 05:52:14 +0000173 void visitSelectInst(SelectInst &I);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000174 void visitCallInst (CallInst &I);
Chris Lattner9bda5f52003-06-28 17:53:05 +0000175 void visitCallSite (CallSite CS);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000176 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000177
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000178 void visitMallocInst(MallocInst &I);
179 void visitAllocaInst(AllocaInst &I);
180 void visitFreeInst (FreeInst &I);
181 void visitLoadInst (LoadInst &I);
182 void visitStoreInst (StoreInst &I);
183 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner4d45bd02003-10-18 05:57:43 +0000184 void visitVANextInst(VANextInst &I);
185 void visitVAArgInst (VAArgInst &I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000186
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000187 void visitInstruction(Instruction &I) {
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000188 std::cerr << "C Writer does not know about " << I;
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000189 abort();
190 }
191
192 void outputLValue(Instruction *I) {
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000193 Out << " " << Mang->getValueName(I) << " = ";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000194 }
Chris Lattnercb3ad012004-05-09 20:41:32 +0000195
196 bool isGotoCodeNecessary(BasicBlock *From, BasicBlock *To);
Chris Lattnera05e0ec2004-05-09 03:42:48 +0000197 void printPHICopiesForSuccessors(BasicBlock *CurBlock,
198 unsigned Indent);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000199 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
200 unsigned Indent);
Chris Lattner23e90702003-11-25 20:49:55 +0000201 void printIndexingExpression(Value *Ptr, gep_type_iterator I,
202 gep_type_iterator E);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000203 };
Chris Lattner4ef51372004-02-14 00:31:10 +0000204}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000205
Chris Lattner68758072004-05-09 06:20:51 +0000206/// This method inserts names for any unnamed structure types that are used by
207/// the program, and removes names from structure types that are not used by the
208/// program.
209///
210bool CBackendNameAllUsedStructs::run(Module &M) {
211 // Get a set of types that are used by the program...
212 std::set<const Type *> UT = getAnalysis<FindUsedTypes>().getTypes();
213
214 // Loop over the module symbol table, removing types from UT that are
215 // already named, and removing names for structure types that are not used.
216 //
217 SymbolTable &MST = M.getSymbolTable();
218 if (MST.find(Type::TypeTy) != MST.end())
219 for (SymbolTable::type_iterator I = MST.type_begin(Type::TypeTy),
220 E = MST.type_end(Type::TypeTy); I != E; ) {
221 SymbolTable::type_iterator It = I++;
222 if (StructType *STy = dyn_cast<StructType>(It->second)) {
223 // If this is not used, remove it from the symbol table.
224 std::set<const Type *>::iterator UTI = UT.find(STy);
225 if (UTI == UT.end())
226 MST.remove(It->first, It->second);
227 else
228 UT.erase(UTI);
229 }
230 }
231
232 // UT now contains types that are not named. Loop over it, naming
233 // structure types.
234 //
235 bool Changed = false;
236 for (std::set<const Type *>::const_iterator I = UT.begin(), E = UT.end();
237 I != E; ++I)
238 if (const StructType *ST = dyn_cast<StructType>(*I)) {
239 ((Value*)ST)->setName("unnamed", &MST);
240 Changed = true;
241 }
242 return Changed;
243}
244
245
Chris Lattner83c57752002-08-19 22:17:53 +0000246// Pass the Type* and the variable name and this prints out the variable
247// declaration.
248//
Chris Lattneredd8ce12003-05-03 03:14:35 +0000249std::ostream &CWriter::printType(std::ostream &Out, const Type *Ty,
250 const std::string &NameSoFar,
Chris Lattner7635ea42003-11-03 01:01:59 +0000251 bool IgnoreName) {
Chris Lattner83c57752002-08-19 22:17:53 +0000252 if (Ty->isPrimitiveType())
253 switch (Ty->getPrimitiveID()) {
254 case Type::VoidTyID: return Out << "void " << NameSoFar;
255 case Type::BoolTyID: return Out << "bool " << NameSoFar;
256 case Type::UByteTyID: return Out << "unsigned char " << NameSoFar;
257 case Type::SByteTyID: return Out << "signed char " << NameSoFar;
258 case Type::UShortTyID: return Out << "unsigned short " << NameSoFar;
259 case Type::ShortTyID: return Out << "short " << NameSoFar;
260 case Type::UIntTyID: return Out << "unsigned " << NameSoFar;
261 case Type::IntTyID: return Out << "int " << NameSoFar;
262 case Type::ULongTyID: return Out << "unsigned long long " << NameSoFar;
263 case Type::LongTyID: return Out << "signed long long " << NameSoFar;
264 case Type::FloatTyID: return Out << "float " << NameSoFar;
265 case Type::DoubleTyID: return Out << "double " << NameSoFar;
266 default :
267 std::cerr << "Unknown primitive type: " << Ty << "\n";
268 abort();
269 }
270
271 // Check to see if the type is named.
Chris Lattner04b72c82002-10-16 00:08:22 +0000272 if (!IgnoreName || isa<OpaqueType>(Ty)) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000273 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner5864da02003-08-24 21:00:22 +0000274 if (I != TypeNames.end()) return Out << I->second << " " << NameSoFar;
Chris Lattner04b72c82002-10-16 00:08:22 +0000275 }
Chris Lattner83c57752002-08-19 22:17:53 +0000276
Chris Lattner83c57752002-08-19 22:17:53 +0000277 switch (Ty->getPrimitiveID()) {
278 case Type::FunctionTyID: {
279 const FunctionType *MTy = cast<FunctionType>(Ty);
Joel Stanley54f60322003-06-25 04:52:09 +0000280 std::stringstream FunctionInnards;
281 FunctionInnards << " (" << NameSoFar << ") (";
Chris Lattnerd5d89962004-02-09 04:14:01 +0000282 for (FunctionType::param_iterator I = MTy->param_begin(),
283 E = MTy->param_end(); I != E; ++I) {
284 if (I != MTy->param_begin())
Joel Stanley54f60322003-06-25 04:52:09 +0000285 FunctionInnards << ", ";
286 printType(FunctionInnards, *I, "");
Chris Lattner83c57752002-08-19 22:17:53 +0000287 }
288 if (MTy->isVarArg()) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000289 if (MTy->getNumParams())
Joel Stanley54f60322003-06-25 04:52:09 +0000290 FunctionInnards << ", ...";
Chris Lattnerd5d89962004-02-09 04:14:01 +0000291 } else if (!MTy->getNumParams()) {
Joel Stanley54f60322003-06-25 04:52:09 +0000292 FunctionInnards << "void";
Chris Lattner83c57752002-08-19 22:17:53 +0000293 }
Joel Stanley54f60322003-06-25 04:52:09 +0000294 FunctionInnards << ")";
295 std::string tstr = FunctionInnards.str();
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000296 printType(Out, MTy->getReturnType(), tstr);
297 return Out;
Chris Lattner83c57752002-08-19 22:17:53 +0000298 }
299 case Type::StructTyID: {
300 const StructType *STy = cast<StructType>(Ty);
301 Out << NameSoFar + " {\n";
302 unsigned Idx = 0;
Chris Lattnerd21cd802004-02-09 04:37:31 +0000303 for (StructType::element_iterator I = STy->element_begin(),
304 E = STy->element_end(); I != E; ++I) {
Chris Lattner83c57752002-08-19 22:17:53 +0000305 Out << " ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000306 printType(Out, *I, "field" + utostr(Idx++));
Chris Lattner83c57752002-08-19 22:17:53 +0000307 Out << ";\n";
308 }
309 return Out << "}";
310 }
311
312 case Type::PointerTyID: {
313 const PointerType *PTy = cast<PointerType>(Ty);
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000314 std::string ptrName = "*" + NameSoFar;
315
Chris Lattner8917d362003-11-03 04:31:54 +0000316 if (isa<ArrayType>(PTy->getElementType()))
317 ptrName = "(" + ptrName + ")";
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000318
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000319 return printType(Out, PTy->getElementType(), ptrName);
Misha Brukman8f9f9a22003-07-21 16:34:35 +0000320 }
Chris Lattner83c57752002-08-19 22:17:53 +0000321
322 case Type::ArrayTyID: {
323 const ArrayType *ATy = cast<ArrayType>(Ty);
324 unsigned NumElements = ATy->getNumElements();
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000325 return printType(Out, ATy->getElementType(),
Chris Lattner83c57752002-08-19 22:17:53 +0000326 NameSoFar + "[" + utostr(NumElements) + "]");
327 }
Chris Lattner04b72c82002-10-16 00:08:22 +0000328
329 case Type::OpaqueTyID: {
330 static int Count = 0;
Chris Lattneredd8ce12003-05-03 03:14:35 +0000331 std::string TyName = "struct opaque_" + itostr(Count++);
Chris Lattner04b72c82002-10-16 00:08:22 +0000332 assert(TypeNames.find(Ty) == TypeNames.end());
333 TypeNames[Ty] = TyName;
334 return Out << TyName << " " << NameSoFar;
335 }
Chris Lattner83c57752002-08-19 22:17:53 +0000336 default:
337 assert(0 && "Unhandled case in getTypeProps!");
338 abort();
339 }
340
341 return Out;
342}
343
Chris Lattner6d492922002-08-19 21:32:41 +0000344void CWriter::printConstantArray(ConstantArray *CPA) {
345
346 // As a special case, print the array as a string if it is an array of
347 // ubytes or an array of sbytes with positive values.
348 //
349 const Type *ETy = CPA->getType()->getElementType();
350 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
351
352 // Make sure the last character is a null char, as automatically added by C
Chris Lattner02da6c02003-06-16 12:09:09 +0000353 if (isString && (CPA->getNumOperands() == 0 ||
354 !cast<Constant>(*(CPA->op_end()-1))->isNullValue()))
Chris Lattner6d492922002-08-19 21:32:41 +0000355 isString = false;
356
357 if (isString) {
358 Out << "\"";
Chris Lattner02da6c02003-06-16 12:09:09 +0000359 // Keep track of whether the last number was a hexadecimal escape
360 bool LastWasHex = false;
361
Chris Lattner6d492922002-08-19 21:32:41 +0000362 // Do not include the last character, which we know is null
363 for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000364 unsigned char C = cast<ConstantInt>(CPA->getOperand(i))->getRawValue();
Chris Lattner6d492922002-08-19 21:32:41 +0000365
Chris Lattner02da6c02003-06-16 12:09:09 +0000366 // Print it out literally if it is a printable character. The only thing
367 // to be careful about is when the last letter output was a hex escape
368 // code, in which case we have to be careful not to print out hex digits
Chris Lattnerda920902003-06-16 12:21:19 +0000369 // explicitly (the C compiler thinks it is a continuation of the previous
370 // character, sheesh...)
Chris Lattner02da6c02003-06-16 12:09:09 +0000371 //
372 if (isprint(C) && (!LastWasHex || !isxdigit(C))) {
373 LastWasHex = false;
Nick Hildenbrandt088b4722002-11-06 21:40:23 +0000374 if (C == '"' || C == '\\')
375 Out << "\\" << C;
Nick Hildenbrandtc3dd2af2002-09-30 21:11:55 +0000376 else
377 Out << C;
Chris Lattner6d492922002-08-19 21:32:41 +0000378 } else {
Chris Lattner02da6c02003-06-16 12:09:09 +0000379 LastWasHex = false;
Chris Lattner6d492922002-08-19 21:32:41 +0000380 switch (C) {
381 case '\n': Out << "\\n"; break;
382 case '\t': Out << "\\t"; break;
383 case '\r': Out << "\\r"; break;
384 case '\v': Out << "\\v"; break;
385 case '\a': Out << "\\a"; break;
Nick Hildenbrandtc3dd2af2002-09-30 21:11:55 +0000386 case '\"': Out << "\\\""; break;
387 case '\'': Out << "\\\'"; break;
Chris Lattner6d492922002-08-19 21:32:41 +0000388 default:
389 Out << "\\x";
Chris Lattner02da6c02003-06-16 12:09:09 +0000390 Out << (char)(( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'));
391 Out << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
392 LastWasHex = true;
Chris Lattner6d492922002-08-19 21:32:41 +0000393 break;
394 }
395 }
396 }
397 Out << "\"";
398 } else {
399 Out << "{";
400 if (CPA->getNumOperands()) {
401 Out << " ";
402 printConstant(cast<Constant>(CPA->getOperand(0)));
403 for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
404 Out << ", ";
405 printConstant(cast<Constant>(CPA->getOperand(i)));
406 }
407 }
408 Out << " }";
409 }
410}
411
Chris Lattnerdd76aca2003-10-05 00:40:51 +0000412// isFPCSafeToPrint - Returns true if we may assume that CFP may be written out
413// textually as a double (rather than as a reference to a stack-allocated
414// variable). We decide this by converting CFP to a string and back into a
415// double, and then checking whether the conversion results in a bit-equal
416// double to the original value of CFP. This depends on us and the target C
417// compiler agreeing on the conversion process (which is pretty likely since we
418// only deal in IEEE FP).
419//
Chris Lattnercb3ad012004-05-09 20:41:32 +0000420static bool isFPCSafeToPrint(const ConstantFP *CFP) {
Chris Lattner4946a8c2003-10-13 17:13:53 +0000421#if HAVE_PRINTF_A
Chris Lattner5ef6ffd2003-10-12 08:12:58 +0000422 char Buffer[100];
423 sprintf(Buffer, "%a", CFP->getValue());
424
425 if (!strncmp(Buffer, "0x", 2) ||
426 !strncmp(Buffer, "-0x", 3) ||
427 !strncmp(Buffer, "+0x", 3))
428 return atof(Buffer) == CFP->getValue();
429 return false;
430#else
Brian Gaekeb471a232003-06-17 23:55:35 +0000431 std::string StrVal = ftostr(CFP->getValue());
Chris Lattner9860e772003-10-12 04:36:29 +0000432
433 while (StrVal[0] == ' ')
434 StrVal.erase(StrVal.begin());
435
Chris Lattnerdd76aca2003-10-05 00:40:51 +0000436 // Check to make sure that the stringized number is not some string like "Inf"
437 // or NaN. Check that the string matches the "[-+]?[0-9]" regex.
Brian Gaekeb471a232003-06-17 23:55:35 +0000438 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
439 ((StrVal[0] == '-' || StrVal[0] == '+') &&
440 (StrVal[1] >= '0' && StrVal[1] <= '9')))
441 // Reparse stringized version!
Chris Lattnerdd76aca2003-10-05 00:40:51 +0000442 return atof(StrVal.c_str()) == CFP->getValue();
Brian Gaekeb471a232003-06-17 23:55:35 +0000443 return false;
Chris Lattner5ef6ffd2003-10-12 08:12:58 +0000444#endif
Brian Gaekeb471a232003-06-17 23:55:35 +0000445}
Chris Lattner6d492922002-08-19 21:32:41 +0000446
447// printConstant - The LLVM Constant to C Constant converter.
448void CWriter::printConstant(Constant *CPV) {
449 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
450 switch (CE->getOpcode()) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000451 case Instruction::Cast:
452 Out << "((";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000453 printType(Out, CPV->getType());
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000454 Out << ")";
Chris Lattner84c0d5e2003-05-14 17:50:19 +0000455 printConstant(CE->getOperand(0));
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000456 Out << ")";
457 return;
458
459 case Instruction::GetElementPtr:
Nick Hildenbrandtd2eb3862002-10-03 20:47:24 +0000460 Out << "(&(";
Chris Lattner23e90702003-11-25 20:49:55 +0000461 printIndexingExpression(CE->getOperand(0), gep_type_begin(CPV),
462 gep_type_end(CPV));
Nick Hildenbrandtd2eb3862002-10-03 20:47:24 +0000463 Out << "))";
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000464 return;
Chris Lattner6683dbf2004-04-01 05:28:26 +0000465 case Instruction::Select:
466 Out << "(";
467 printConstant(CE->getOperand(0));
468 Out << "?";
469 printConstant(CE->getOperand(1));
470 Out << ":";
471 printConstant(CE->getOperand(2));
472 Out << ")";
473 return;
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000474 case Instruction::Add:
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000475 case Instruction::Sub:
Chris Lattner29255922003-08-14 19:19:53 +0000476 case Instruction::Mul:
477 case Instruction::Div:
478 case Instruction::Rem:
479 case Instruction::SetEQ:
480 case Instruction::SetNE:
481 case Instruction::SetLT:
482 case Instruction::SetLE:
483 case Instruction::SetGT:
484 case Instruction::SetGE:
Brian Gaeke0415b6c2003-11-22 05:02:56 +0000485 case Instruction::Shl:
486 case Instruction::Shr:
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000487 Out << "(";
Chris Lattner84c0d5e2003-05-14 17:50:19 +0000488 printConstant(CE->getOperand(0));
Chris Lattner29255922003-08-14 19:19:53 +0000489 switch (CE->getOpcode()) {
490 case Instruction::Add: Out << " + "; break;
491 case Instruction::Sub: Out << " - "; break;
492 case Instruction::Mul: Out << " * "; break;
493 case Instruction::Div: Out << " / "; break;
494 case Instruction::Rem: Out << " % "; break;
495 case Instruction::SetEQ: Out << " == "; break;
496 case Instruction::SetNE: Out << " != "; break;
497 case Instruction::SetLT: Out << " < "; break;
498 case Instruction::SetLE: Out << " <= "; break;
499 case Instruction::SetGT: Out << " > "; break;
500 case Instruction::SetGE: Out << " >= "; break;
Brian Gaeke0415b6c2003-11-22 05:02:56 +0000501 case Instruction::Shl: Out << " << "; break;
502 case Instruction::Shr: Out << " >> "; break;
Chris Lattner29255922003-08-14 19:19:53 +0000503 default: assert(0 && "Illegal opcode here!");
504 }
Chris Lattner84c0d5e2003-05-14 17:50:19 +0000505 printConstant(CE->getOperand(1));
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000506 Out << ")";
507 return;
508
Chris Lattner6d492922002-08-19 21:32:41 +0000509 default:
510 std::cerr << "CWriter Error: Unhandled constant expression: "
511 << CE << "\n";
512 abort();
513 }
514 }
515
516 switch (CPV->getType()->getPrimitiveID()) {
517 case Type::BoolTyID:
518 Out << (CPV == ConstantBool::False ? "0" : "1"); break;
519 case Type::SByteTyID:
520 case Type::ShortTyID:
Chris Lattner6d492922002-08-19 21:32:41 +0000521 Out << cast<ConstantSInt>(CPV)->getValue(); break;
Chris Lattner45343ea2003-05-12 15:39:31 +0000522 case Type::IntTyID:
523 if ((int)cast<ConstantSInt>(CPV)->getValue() == (int)0x80000000)
524 Out << "((int)0x80000000)"; // Handle MININT specially to avoid warning
525 else
526 Out << cast<ConstantSInt>(CPV)->getValue();
527 break;
528
Chris Lattner6d492922002-08-19 21:32:41 +0000529 case Type::LongTyID:
530 Out << cast<ConstantSInt>(CPV)->getValue() << "ll"; break;
531
532 case Type::UByteTyID:
533 case Type::UShortTyID:
534 Out << cast<ConstantUInt>(CPV)->getValue(); break;
535 case Type::UIntTyID:
536 Out << cast<ConstantUInt>(CPV)->getValue() << "u"; break;
537 case Type::ULongTyID:
538 Out << cast<ConstantUInt>(CPV)->getValue() << "ull"; break;
539
540 case Type::FloatTyID:
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000541 case Type::DoubleTyID: {
542 ConstantFP *FPC = cast<ConstantFP>(CPV);
Chris Lattneredd8ce12003-05-03 03:14:35 +0000543 std::map<const ConstantFP*, unsigned>::iterator I = FPConstantMap.find(FPC);
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000544 if (I != FPConstantMap.end()) {
545 // Because of FP precision problems we must load from a stack allocated
546 // value that holds the value in hex.
547 Out << "(*(" << (FPC->getType() == Type::FloatTy ? "float" : "double")
Chris Lattner9860e772003-10-12 04:36:29 +0000548 << "*)&FPConstant" << I->second << ")";
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000549 } else {
Chris Lattner4946a8c2003-10-13 17:13:53 +0000550#if HAVE_PRINTF_A
Chris Lattnerdd76aca2003-10-05 00:40:51 +0000551 // Print out the constant as a floating point number.
Chris Lattner5ef6ffd2003-10-12 08:12:58 +0000552 char Buffer[100];
553 sprintf(Buffer, "%a", FPC->getValue());
554 Out << Buffer << " /*" << FPC->getValue() << "*/ ";
555#else
Chris Lattnerdd76aca2003-10-05 00:40:51 +0000556 Out << ftostr(FPC->getValue());
Chris Lattner5ef6ffd2003-10-12 08:12:58 +0000557#endif
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000558 }
559 break;
560 }
Chris Lattner6d492922002-08-19 21:32:41 +0000561
562 case Type::ArrayTyID:
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000563 if (isa<ConstantAggregateZero>(CPV)) {
564 const ArrayType *AT = cast<ArrayType>(CPV->getType());
565 Out << "{";
566 if (AT->getNumElements()) {
567 Out << " ";
568 Constant *CZ = Constant::getNullValue(AT->getElementType());
569 printConstant(CZ);
570 for (unsigned i = 1, e = AT->getNumElements(); i != e; ++i) {
571 Out << ", ";
572 printConstant(CZ);
573 }
574 }
575 Out << " }";
576 } else {
577 printConstantArray(cast<ConstantArray>(CPV));
578 }
Chris Lattner6d492922002-08-19 21:32:41 +0000579 break;
580
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000581 case Type::StructTyID:
582 if (isa<ConstantAggregateZero>(CPV)) {
583 const StructType *ST = cast<StructType>(CPV->getType());
584 Out << "{";
585 if (ST->getNumElements()) {
586 Out << " ";
587 printConstant(Constant::getNullValue(ST->getElementType(0)));
588 for (unsigned i = 1, e = ST->getNumElements(); i != e; ++i) {
589 Out << ", ";
590 printConstant(Constant::getNullValue(ST->getElementType(i)));
591 }
Chris Lattner6d492922002-08-19 21:32:41 +0000592 }
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000593 Out << " }";
594 } else {
595 Out << "{";
596 if (CPV->getNumOperands()) {
597 Out << " ";
598 printConstant(cast<Constant>(CPV->getOperand(0)));
599 for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) {
600 Out << ", ";
601 printConstant(cast<Constant>(CPV->getOperand(i)));
602 }
603 }
604 Out << " }";
Chris Lattner6d492922002-08-19 21:32:41 +0000605 }
Chris Lattner6d492922002-08-19 21:32:41 +0000606 break;
Chris Lattner6d492922002-08-19 21:32:41 +0000607
608 case Type::PointerTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000609 if (isa<ConstantPointerNull>(CPV)) {
Chris Lattnercf135cb2003-06-02 03:10:53 +0000610 Out << "((";
611 printType(Out, CPV->getType());
612 Out << ")/*NULL*/0)";
Chris Lattner6d492922002-08-19 21:32:41 +0000613 break;
614 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
615 writeOperand(CPR->getValue());
616 break;
617 }
618 // FALL THROUGH
619 default:
620 std::cerr << "Unknown constant type: " << CPV << "\n";
621 abort();
622 }
623}
624
625void CWriter::writeOperandInternal(Value *Operand) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000626 if (Instruction *I = dyn_cast<Instruction>(Operand))
Chris Lattnera8ab89e2003-06-17 04:39:14 +0000627 if (isInlinableInst(*I) && !isDirectAlloca(I)) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000628 // Should we inline this instruction to build a tree?
629 Out << "(";
630 visit(*I);
631 Out << ")";
632 return;
633 }
634
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000635 if (Constant *CPV = dyn_cast<Constant>(Operand)) {
Chris Lattner6d492922002-08-19 21:32:41 +0000636 printConstant(CPV);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000637 } else {
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000638 Out << Mang->getValueName(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000639 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000640}
641
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000642void CWriter::writeOperand(Value *Operand) {
Chris Lattnera8ab89e2003-06-17 04:39:14 +0000643 if (isa<GlobalVariable>(Operand) || isDirectAlloca(Operand))
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000644 Out << "(&"; // Global variables are references as their addresses by llvm
645
646 writeOperandInternal(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000647
Chris Lattnera8ab89e2003-06-17 04:39:14 +0000648 if (isa<GlobalVariable>(Operand) || isDirectAlloca(Operand))
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000649 Out << ")";
650}
651
Chris Lattner41488192003-06-28 17:15:12 +0000652// generateCompilerSpecificCode - This is where we add conditional compilation
653// directives to cater to specific compilers as need be.
654//
Chris Lattnerc59c1182003-11-16 22:06:14 +0000655static void generateCompilerSpecificCode(std::ostream& Out) {
Chris Lattner41488192003-06-28 17:15:12 +0000656 // Alloca is hard to get, and we don't want to include stdlib.h here...
657 Out << "/* get a declaration for alloca */\n"
658 << "#ifdef sun\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000659 << "extern void *__builtin_alloca(unsigned long);\n"
660 << "#define alloca(x) __builtin_alloca(x)\n"
661 << "#else\n"
Brian Gaekea0145cc2003-06-16 23:57:13 +0000662 << "#ifndef __FreeBSD__\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000663 << "#include <alloca.h>\n"
Brian Gaekea0145cc2003-06-16 23:57:13 +0000664 << "#endif\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000665 << "#endif\n\n";
Chris Lattner41488192003-06-28 17:15:12 +0000666
667 // We output GCC specific attributes to preserve 'linkonce'ness on globals.
668 // If we aren't being compiled with GCC, just drop these attributes.
Chris Lattner9bda5f52003-06-28 17:53:05 +0000669 Out << "#ifndef __GNUC__ /* Can only support \"linkonce\" vars with GCC */\n"
Chris Lattner41488192003-06-28 17:15:12 +0000670 << "#define __attribute__(X)\n"
Brian Gaeke27f7a712003-12-11 00:24:36 +0000671 << "#endif\n\n";
672
673#if 0
674 // At some point, we should support "external weak" vs. "weak" linkages.
675 // On Mac OS X, "external weak" is spelled "__attribute__((weak_import))".
676 Out << "#if defined(__GNUC__) && defined(__APPLE_CC__)\n"
677 << "#define __EXTERNAL_WEAK__ __attribute__((weak_import))\n"
678 << "#elif defined(__GNUC__)\n"
679 << "#define __EXTERNAL_WEAK__ __attribute__((weak))\n"
680 << "#else\n"
681 << "#define __EXTERNAL_WEAK__\n"
682 << "#endif\n\n";
683#endif
684
685 // For now, turn off the weak linkage attribute on Mac OS X. (See above.)
686 Out << "#if defined(__GNUC__) && defined(__APPLE_CC__)\n"
687 << "#define __ATTRIBUTE_WEAK__\n"
688 << "#elif defined(__GNUC__)\n"
689 << "#define __ATTRIBUTE_WEAK__ __attribute__((weak))\n"
690 << "#else\n"
691 << "#define __ATTRIBUTE_WEAK__\n"
692 << "#endif\n\n";
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000693}
694
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000695bool CWriter::doInitialization(Module &M) {
696 // Initialize
697 TheModule = &M;
Chris Lattnerc2421432004-05-09 04:30:20 +0000698
699 IL.AddPrototypes(M);
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000700
701 // Ensure that all structure types have names...
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000702 Mang = new Mangler(M);
Chris Lattnerc59c1182003-11-16 22:06:14 +0000703
Chris Lattner16c7bb22002-05-09 02:28:59 +0000704 // get declaration for alloca
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000705 Out << "/* Provide Declarations */\n";
Chris Lattnerc59c1182003-11-16 22:06:14 +0000706 Out << "#include <stdarg.h>\n"; // Varargs support
707 Out << "#include <setjmp.h>\n"; // Unwind support
Chris Lattner41488192003-06-28 17:15:12 +0000708 generateCompilerSpecificCode(Out);
Chris Lattnerc59c1182003-11-16 22:06:14 +0000709
Chris Lattnercf135cb2003-06-02 03:10:53 +0000710 // Provide a definition for `bool' if not compiling with a C++ compiler.
711 Out << "\n"
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000712 << "#ifndef __cplusplus\ntypedef unsigned char bool;\n#endif\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000713
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000714 << "\n\n/* Support for floating point constants */\n"
715 << "typedef unsigned long long ConstantDoubleTy;\n"
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000716 << "typedef unsigned int ConstantFloatTy;\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000717
Chris Lattnera4d4a852002-08-19 21:48:40 +0000718 << "\n\n/* Global Declarations */\n";
719
720 // First output all the declarations for the program, because C requires
721 // Functions & globals to be declared before they are used.
722 //
Chris Lattner16c7bb22002-05-09 02:28:59 +0000723
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000724 // Loop over the symbol table, emitting all named constants...
Chris Lattner68758072004-05-09 06:20:51 +0000725 printModuleTypes(M.getSymbolTable());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000726
Chris Lattnera4d4a852002-08-19 21:48:40 +0000727 // Global variable declarations...
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000728 if (!M.gempty()) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000729 Out << "\n/* External Global Variable Declarations */\n";
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000730 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000731 if (I->hasExternalLinkage()) {
732 Out << "extern ";
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000733 printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000734 Out << ";\n";
735 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000736 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000737 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000738
Chris Lattnera4d4a852002-08-19 21:48:40 +0000739 // Function declarations
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000740 if (!M.empty()) {
Chris Lattnera4d4a852002-08-19 21:48:40 +0000741 Out << "\n/* Function Declarations */\n";
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000742 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner4ef51372004-02-14 00:31:10 +0000743 // Don't print declarations for intrinsic functions.
Chris Lattner4589ed92004-05-09 16:03:29 +0000744 if (!I->getIntrinsicID() &&
745 I->getName() != "setjmp" && I->getName() != "longjmp") {
Nick Hildenbrandta1a64f82002-11-18 22:21:52 +0000746 printFunctionSignature(I, true);
Brian Gaeke27f7a712003-12-11 00:24:36 +0000747 if (I->hasWeakLinkage()) Out << " __ATTRIBUTE_WEAK__";
John Criswell3799eec2004-02-26 22:20:58 +0000748 if (I->hasLinkOnceLinkage()) Out << " __ATTRIBUTE_WEAK__";
Nick Hildenbrandta1a64f82002-11-18 22:21:52 +0000749 Out << ";\n";
750 }
Chris Lattner4cda8352002-08-20 16:55:48 +0000751 }
Chris Lattnera4d4a852002-08-19 21:48:40 +0000752 }
753
Joel Stanley54f60322003-06-25 04:52:09 +0000754 // Output the global variable declarations
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000755 if (!M.gempty()) {
Joel Stanley54f60322003-06-25 04:52:09 +0000756 Out << "\n\n/* Global Variable Declarations */\n";
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000757 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +0000758 if (!I->isExternal()) {
759 Out << "extern ";
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000760 printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
Chris Lattner2580d4f2003-11-03 17:32:38 +0000761
762 if (I->hasLinkOnceLinkage())
763 Out << " __attribute__((common))";
764 else if (I->hasWeakLinkage())
Brian Gaeke27f7a712003-12-11 00:24:36 +0000765 Out << " __ATTRIBUTE_WEAK__";
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +0000766 Out << ";\n";
767 }
768 }
769
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000770 // Output the global variable definitions and contents...
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000771 if (!M.gempty()) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000772 Out << "\n\n/* Global Variable Definitions and Initialization */\n";
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000773 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
Chris Lattnere8e035b2002-10-16 20:08:47 +0000774 if (!I->isExternal()) {
775 if (I->hasInternalLinkage())
776 Out << "static ";
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000777 printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
Chris Lattner76d9f1b2003-06-28 17:08:36 +0000778 if (I->hasLinkOnceLinkage())
779 Out << " __attribute__((common))";
Chris Lattner72ac148d2003-10-16 18:29:00 +0000780 else if (I->hasWeakLinkage())
Brian Gaeke27f7a712003-12-11 00:24:36 +0000781 Out << " __ATTRIBUTE_WEAK__";
Chris Lattner5ea326a2003-11-03 17:35:00 +0000782
783 // If the initializer is not null, emit the initializer. If it is null,
784 // we try to avoid emitting large amounts of zeros. The problem with
785 // this, however, occurs when the variable has weak linkage. In this
786 // case, the assembler will complain about the variable being both weak
787 // and common, so we disable this optimization.
Chris Lattnerf358c5a2004-02-20 05:49:22 +0000788 if (!I->getInitializer()->isNullValue()) {
Chris Lattner940b08d2003-06-06 07:10:24 +0000789 Out << " = " ;
790 writeOperand(I->getInitializer());
Chris Lattnerf358c5a2004-02-20 05:49:22 +0000791 } else if (I->hasWeakLinkage()) {
792 // We have to specify an initializer, but it doesn't have to be
793 // complete. If the value is an aggregate, print out { 0 }, and let
794 // the compiler figure out the rest of the zeros.
795 Out << " = " ;
796 if (isa<StructType>(I->getInitializer()->getType()) ||
797 isa<ArrayType>(I->getInitializer()->getType())) {
798 Out << "{ 0 }";
799 } else {
800 // Just print it out normally.
801 writeOperand(I->getInitializer());
802 }
Chris Lattner940b08d2003-06-06 07:10:24 +0000803 }
Chris Lattnere8e035b2002-10-16 20:08:47 +0000804 Out << ";\n";
Chris Lattnera4d4a852002-08-19 21:48:40 +0000805 }
Chris Lattnera4d4a852002-08-19 21:48:40 +0000806 }
807
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000808 if (!M.empty())
Chris Lattnera4d4a852002-08-19 21:48:40 +0000809 Out << "\n\n/* Function Bodies */\n";
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000810 return false;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000811}
812
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000813
Chris Lattner9860e772003-10-12 04:36:29 +0000814/// Output all floating point constants that cannot be printed accurately...
Chris Lattner68758072004-05-09 06:20:51 +0000815void CWriter::printFloatingPointConstants(Function &F) {
Chris Lattner9860e772003-10-12 04:36:29 +0000816 union {
817 double D;
Chris Lattnerc2421432004-05-09 04:30:20 +0000818 uint64_t U;
Chris Lattner9860e772003-10-12 04:36:29 +0000819 } DBLUnion;
820
821 union {
822 float F;
823 unsigned U;
824 } FLTUnion;
825
826 // Scan the module for floating point constants. If any FP constant is used
827 // in the function, we want to redirect it here so that we do not depend on
828 // the precision of the printed form, unless the printed form preserves
829 // precision.
830 //
Chris Lattner68758072004-05-09 06:20:51 +0000831 static unsigned FPCounter = 0;
832 for (constant_iterator I = constant_begin(&F), E = constant_end(&F);
833 I != E; ++I)
834 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(*I))
835 if (!isFPCSafeToPrint(FPC) && // Do not put in FPConstantMap if safe.
836 !FPConstantMap.count(FPC)) {
837 double Val = FPC->getValue();
838
839 FPConstantMap[FPC] = FPCounter; // Number the FP constants
840
841 if (FPC->getType() == Type::DoubleTy) {
842 DBLUnion.D = Val;
843 Out << "static const ConstantDoubleTy FPConstant" << FPCounter++
844 << " = 0x" << std::hex << DBLUnion.U << std::dec
845 << "ULL; /* " << Val << " */\n";
846 } else if (FPC->getType() == Type::FloatTy) {
847 FLTUnion.F = Val;
848 Out << "static const ConstantFloatTy FPConstant" << FPCounter++
849 << " = 0x" << std::hex << FLTUnion.U << std::dec
850 << "U; /* " << Val << " */\n";
851 } else
852 assert(0 && "Unknown float type!");
853 }
Chris Lattner9860e772003-10-12 04:36:29 +0000854
855 Out << "\n";
Chris Lattner68758072004-05-09 06:20:51 +0000856}
Chris Lattner9860e772003-10-12 04:36:29 +0000857
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000858
Chris Lattner2db41cd2002-09-20 15:12:13 +0000859/// printSymbolTable - Run through symbol table looking for type names. If a
860/// type name is found, emit it's declaration...
Chris Lattnera4c047e2002-09-20 14:56:54 +0000861///
Chris Lattner68758072004-05-09 06:20:51 +0000862void CWriter::printModuleTypes(const SymbolTable &ST) {
Chris Lattner2db41cd2002-09-20 15:12:13 +0000863 // If there are no type names, exit early.
864 if (ST.find(Type::TypeTy) == ST.end())
865 return;
866
867 // We are only interested in the type plane of the symbol table...
868 SymbolTable::type_const_iterator I = ST.type_begin(Type::TypeTy);
869 SymbolTable::type_const_iterator End = ST.type_end(Type::TypeTy);
870
Chris Lattner58d04d42002-09-20 15:18:30 +0000871 // Print out forward declarations for structure types before anything else!
872 Out << "/* Structure forward decls */\n";
873 for (; I != End; ++I)
Chris Lattner68758072004-05-09 06:20:51 +0000874 if (const Type *STy = dyn_cast<StructType>(I->second)) {
875 std::string Name = "struct l_" + Mangler::makeNameProper(I->first);
876 Out << Name << ";\n";
877 TypeNames.insert(std::make_pair(STy, Name));
878 }
Chris Lattner58d04d42002-09-20 15:18:30 +0000879
880 Out << "\n";
881
882 // Now we can print out typedefs...
883 Out << "/* Typedefs */\n";
Chris Lattner68758072004-05-09 06:20:51 +0000884 for (I = ST.type_begin(Type::TypeTy); I != End; ++I) {
885 const Type *Ty = cast<Type>(I->second);
886 std::string Name = "l_" + Mangler::makeNameProper(I->first);
887 Out << "typedef ";
888 printType(Out, Ty, Name);
889 Out << ";\n";
890 }
Chris Lattnerfa395ec2003-11-02 01:29:27 +0000891
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000892 Out << "\n";
893
Chris Lattner2c601a72002-09-20 15:05:40 +0000894 // Keep track of which structures have been printed so far...
895 std::set<const StructType *> StructPrinted;
896
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000897 // Loop over all structures then push them into the stack so they are
898 // printed in the correct order.
Chris Lattner2db41cd2002-09-20 15:12:13 +0000899 //
Chris Lattner58d04d42002-09-20 15:18:30 +0000900 Out << "/* Structure contents */\n";
Chris Lattner2db41cd2002-09-20 15:12:13 +0000901 for (I = ST.type_begin(Type::TypeTy); I != End; ++I)
902 if (const StructType *STy = dyn_cast<StructType>(I->second))
Chris Lattnerfa395ec2003-11-02 01:29:27 +0000903 // Only print out used types!
Chris Lattner68758072004-05-09 06:20:51 +0000904 printContainedStructs(STy, StructPrinted);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000905}
906
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000907// Push the struct onto the stack and recursively push all structs
908// this one depends on.
Chris Lattner2c601a72002-09-20 15:05:40 +0000909void CWriter::printContainedStructs(const Type *Ty,
910 std::set<const StructType*> &StructPrinted){
Misha Brukmanac25de32003-07-09 17:33:50 +0000911 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000912 //Check to see if we have already printed this struct
Chris Lattner2c601a72002-09-20 15:05:40 +0000913 if (StructPrinted.count(STy) == 0) {
914 // Print all contained types first...
Chris Lattnerd21cd802004-02-09 04:37:31 +0000915 for (StructType::element_iterator I = STy->element_begin(),
916 E = STy->element_end(); I != E; ++I) {
Chris Lattner2c601a72002-09-20 15:05:40 +0000917 const Type *Ty1 = I->get();
Chris Lattnera4c047e2002-09-20 14:56:54 +0000918 if (isa<StructType>(Ty1) || isa<ArrayType>(Ty1))
Chris Lattnerab2b3282003-05-29 15:12:27 +0000919 printContainedStructs(*I, StructPrinted);
Chris Lattnera4c047e2002-09-20 14:56:54 +0000920 }
921
Chris Lattner2c601a72002-09-20 15:05:40 +0000922 //Print structure type out..
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000923 StructPrinted.insert(STy);
Chris Lattneredd8ce12003-05-03 03:14:35 +0000924 std::string Name = TypeNames[STy];
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000925 printType(Out, STy, Name, true);
Chris Lattner58d04d42002-09-20 15:18:30 +0000926 Out << ";\n\n";
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000927 }
Chris Lattnera4c047e2002-09-20 14:56:54 +0000928
Chris Lattner2c601a72002-09-20 15:05:40 +0000929 // If it is an array, check contained types and continue
930 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)){
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000931 const Type *Ty1 = ATy->getElementType();
932 if (isa<StructType>(Ty1) || isa<ArrayType>(Ty1))
Chris Lattner2c601a72002-09-20 15:05:40 +0000933 printContainedStructs(Ty1, StructPrinted);
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000934 }
935}
936
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000937
Chris Lattner4cda8352002-08-20 16:55:48 +0000938void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
Joel Stanley54f60322003-06-25 04:52:09 +0000939 if (F->hasInternalLinkage()) Out << "static ";
Joel Stanley54f60322003-06-25 04:52:09 +0000940
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000941 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000942 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000943
Joel Stanley54f60322003-06-25 04:52:09 +0000944 std::stringstream FunctionInnards;
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000945
946 // Print out the name...
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000947 FunctionInnards << Mang->getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000948
Chris Lattner16c7bb22002-05-09 02:28:59 +0000949 if (!F->isExternal()) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000950 if (!F->aempty()) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000951 std::string ArgName;
Chris Lattner4cda8352002-08-20 16:55:48 +0000952 if (F->abegin()->hasName() || !Prototype)
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000953 ArgName = Mang->getValueName(F->abegin());
Joel Stanley54f60322003-06-25 04:52:09 +0000954 printType(FunctionInnards, F->afront().getType(), ArgName);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000955 for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
956 I != E; ++I) {
Joel Stanley54f60322003-06-25 04:52:09 +0000957 FunctionInnards << ", ";
Chris Lattner4cda8352002-08-20 16:55:48 +0000958 if (I->hasName() || !Prototype)
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000959 ArgName = Mang->getValueName(I);
Chris Lattner4cda8352002-08-20 16:55:48 +0000960 else
961 ArgName = "";
Joel Stanley54f60322003-06-25 04:52:09 +0000962 printType(FunctionInnards, I->getType(), ArgName);
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000963 }
964 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000965 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000966 // Loop over the arguments, printing them...
Chris Lattnerd5d89962004-02-09 04:14:01 +0000967 for (FunctionType::param_iterator I = FT->param_begin(),
968 E = FT->param_end(); I != E; ++I) {
969 if (I != FT->param_begin()) FunctionInnards << ", ";
Joel Stanley54f60322003-06-25 04:52:09 +0000970 printType(FunctionInnards, *I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000971 }
972 }
973
Chris Lattner1f8c4a12002-09-20 22:32:30 +0000974 // Finish printing arguments... if this is a vararg function, print the ...,
975 // unless there are no known types, in which case, we just emit ().
976 //
Chris Lattnerd5d89962004-02-09 04:14:01 +0000977 if (FT->isVarArg() && FT->getNumParams()) {
978 if (FT->getNumParams()) FunctionInnards << ", ";
Joel Stanley54f60322003-06-25 04:52:09 +0000979 FunctionInnards << "..."; // Output varargs portion of signature!
Chris Lattnerd5d89962004-02-09 04:14:01 +0000980 } else if (!FT->isVarArg() && FT->getNumParams() == 0) {
Chris Lattner4f7e1732003-11-26 00:09:17 +0000981 FunctionInnards << "void"; // ret() -> ret(void) in C.
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000982 }
Joel Stanley54f60322003-06-25 04:52:09 +0000983 FunctionInnards << ")";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000984 // Print out the return type and the entire signature for that matter
Joel Stanley54f60322003-06-25 04:52:09 +0000985 printType(Out, F->getReturnType(), FunctionInnards.str());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000986}
987
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000988void CWriter::printFunction(Function &F) {
989 printFunctionSignature(&F, false);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000990 Out << " {\n";
991
Chris Lattner497e19a2002-05-09 20:39:03 +0000992 // print local variable information for the function
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000993 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I)
Chris Lattner6ffe5512004-04-27 15:13:33 +0000994 if (const AllocaInst *AI = isDirectAlloca(&*I)) {
Chris Lattnera8ab89e2003-06-17 04:39:14 +0000995 Out << " ";
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000996 printType(Out, AI->getAllocatedType(), Mang->getValueName(AI));
Chris Lattnera8ab89e2003-06-17 04:39:14 +0000997 Out << "; /* Address exposed local */\n";
Chris Lattner6ffe5512004-04-27 15:13:33 +0000998 } else if (I->getType() != Type::VoidTy && !isInlinableInst(*I)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000999 Out << " ";
Chris Lattner6ffe5512004-04-27 15:13:33 +00001000 printType(Out, I->getType(), Mang->getValueName(&*I));
Chris Lattner16c7bb22002-05-09 02:28:59 +00001001 Out << ";\n";
Chris Lattnera8ab89e2003-06-17 04:39:14 +00001002
Chris Lattner84e66652003-05-03 07:11:00 +00001003 if (isa<PHINode>(*I)) { // Print out PHI node temporaries as well...
1004 Out << " ";
Chris Lattner6ffe5512004-04-27 15:13:33 +00001005 printType(Out, I->getType(),
1006 Mang->getValueName(&*I)+"__PHI_TEMPORARY");
Chris Lattner84e66652003-05-03 07:11:00 +00001007 Out << ";\n";
1008 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001009 }
Chris Lattnerd1cf1b42002-09-20 23:26:33 +00001010
1011 Out << "\n";
1012
Chris Lattner16c7bb22002-05-09 02:28:59 +00001013 // print the basic blocks
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001014 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattnercb3ad012004-05-09 20:41:32 +00001015 if (Loop *L = LI->getLoopFor(BB)) {
1016 if (L->getHeader() == BB && L->getParentLoop() == 0)
1017 printLoop(L);
1018 } else {
1019 printBasicBlock(BB);
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001020 }
Chris Lattner8c3c4bf2002-05-09 15:49:41 +00001021 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001022
Chris Lattner8c3c4bf2002-05-09 15:49:41 +00001023 Out << "}\n\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001024}
1025
Chris Lattnercb3ad012004-05-09 20:41:32 +00001026void CWriter::printLoop(Loop *L) {
1027 Out << " do { /* Syntactic loop '" << L->getHeader()->getName()
1028 << "' to make GCC happy */\n";
1029 for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i) {
1030 BasicBlock *BB = L->getBlocks()[i];
1031 Loop *BBLoop = LI->getLoopFor(BB);
1032 if (BBLoop == L)
1033 printBasicBlock(BB);
1034 else if (BB == BBLoop->getHeader() && BBLoop->getParentLoop() == L)
1035 printLoop(BBLoop);
1036 }
1037 Out << " } while (1); /* end of syntactic loop '"
1038 << L->getHeader()->getName() << "' */\n";
1039}
1040
1041void CWriter::printBasicBlock(BasicBlock *BB) {
1042
1043 // Don't print the label for the basic block if there are no uses, or if
1044 // the only terminator use is the predecessor basic block's terminator.
1045 // We have to scan the use list because PHI nodes use basic blocks too but
1046 // do not require a label to be generated.
1047 //
1048 bool NeedsLabel = false;
1049 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
1050 if (isGotoCodeNecessary(*PI, BB)) {
1051 NeedsLabel = true;
1052 break;
1053 }
1054
1055 if (NeedsLabel) Out << Mang->getValueName(BB) << ":\n";
1056
1057 // Output all of the instructions in the basic block...
1058 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E;
1059 ++II) {
1060 if (!isInlinableInst(*II) && !isDirectAlloca(II)) {
1061 if (II->getType() != Type::VoidTy)
1062 outputLValue(II);
1063 else
1064 Out << " ";
1065 visit(*II);
1066 Out << ";\n";
1067 }
1068 }
1069
1070 // Don't emit prefix or suffix for the terminator...
1071 visit(*BB->getTerminator());
1072}
1073
1074
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001075// Specific Instruction type classes... note that all of the casts are
Misha Brukman5560c9d2003-08-18 14:43:39 +00001076// necessary because we use the instruction classes as opaque types...
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001077//
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001078void CWriter::visitReturnInst(ReturnInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001079 // Don't output a void return if this is the last basic block in the function
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001080 if (I.getNumOperands() == 0 &&
1081 &*--I.getParent()->getParent()->end() == I.getParent() &&
1082 !I.getParent()->size() == 1) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001083 return;
Chris Lattner963b70b2002-05-21 18:05:19 +00001084 }
Chris Lattner44408262002-05-09 03:50:42 +00001085
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001086 Out << " return";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001087 if (I.getNumOperands()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001088 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001089 writeOperand(I.getOperand(0));
Chris Lattner16c7bb22002-05-09 02:28:59 +00001090 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001091 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001092}
1093
Chris Lattnera9f5e052003-04-22 20:19:52 +00001094void CWriter::visitSwitchInst(SwitchInst &SI) {
Chris Lattnera05e0ec2004-05-09 03:42:48 +00001095 printPHICopiesForSuccessors(SI.getParent(), 0);
1096
Chris Lattnera9f5e052003-04-22 20:19:52 +00001097 Out << " switch (";
1098 writeOperand(SI.getOperand(0));
Chris Lattnerf1acd962003-04-23 19:15:13 +00001099 Out << ") {\n default:\n";
1100 printBranchToBlock(SI.getParent(), SI.getDefaultDest(), 2);
Chris Lattnera9f5e052003-04-22 20:19:52 +00001101 Out << ";\n";
1102 for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) {
1103 Out << " case ";
1104 writeOperand(SI.getOperand(i));
1105 Out << ":\n";
1106 BasicBlock *Succ = cast<BasicBlock>(SI.getOperand(i+1));
1107 printBranchToBlock(SI.getParent(), Succ, 2);
1108 if (Succ == SI.getParent()->getNext())
1109 Out << " break;\n";
1110 }
1111 Out << " }\n";
1112}
1113
Chris Lattnercb3ad012004-05-09 20:41:32 +00001114bool CWriter::isGotoCodeNecessary(BasicBlock *From, BasicBlock *To) {
1115 /// FIXME: This should be reenabled, but loop reordering safe!!
1116 return true;
1117
1118 if (From->getNext() != To) // Not the direct successor, we need a goto
1119 return true;
1120
1121 //isa<SwitchInst>(From->getTerminator())
Chris Lattner9bda5f52003-06-28 17:53:05 +00001122
Chris Lattnera9f5e052003-04-22 20:19:52 +00001123
Chris Lattnercb3ad012004-05-09 20:41:32 +00001124 if (LI->getLoopFor(From) != LI->getLoopFor(To))
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001125 return true;
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001126 return false;
1127}
1128
Chris Lattnera05e0ec2004-05-09 03:42:48 +00001129void CWriter::printPHICopiesForSuccessors(BasicBlock *CurBlock,
1130 unsigned Indent) {
1131 for (succ_iterator SI = succ_begin(CurBlock), E = succ_end(CurBlock);
1132 SI != E; ++SI)
1133 for (BasicBlock::iterator I = SI->begin();
1134 PHINode *PN = dyn_cast<PHINode>(I); ++I) {
1135 // now we have to do the printing
1136 Out << std::string(Indent, ' ');
1137 Out << " " << Mang->getValueName(I) << "__PHI_TEMPORARY = ";
1138 writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBlock)));
1139 Out << "; /* for PHI node */\n";
1140 }
1141}
1142
1143
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001144void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
Chris Lattner9bda5f52003-06-28 17:53:05 +00001145 unsigned Indent) {
Chris Lattnercb3ad012004-05-09 20:41:32 +00001146 if (isGotoCodeNecessary(CurBB, Succ)) {
Chris Lattneredd8ce12003-05-03 03:14:35 +00001147 Out << std::string(Indent, ' ') << " goto ";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001148 writeOperand(Succ);
1149 Out << ";\n";
1150 }
1151}
1152
Misha Brukman37f92e22003-09-11 22:34:13 +00001153// Branch instruction printing - Avoid printing out a branch to a basic block
1154// that immediately succeeds the current one.
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001155//
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001156void CWriter::visitBranchInst(BranchInst &I) {
Chris Lattnera05e0ec2004-05-09 03:42:48 +00001157 printPHICopiesForSuccessors(I.getParent(), 0);
1158
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001159 if (I.isConditional()) {
Chris Lattner5ef6ffd2003-10-12 08:12:58 +00001160 if (isGotoCodeNecessary(I.getParent(), I.getSuccessor(0))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001161 Out << " if (";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001162 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001163 Out << ") {\n";
1164
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001165 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001166
Chris Lattner5ef6ffd2003-10-12 08:12:58 +00001167 if (isGotoCodeNecessary(I.getParent(), I.getSuccessor(1))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001168 Out << " } else {\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001169 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001170 }
1171 } else {
Misha Brukman5560c9d2003-08-18 14:43:39 +00001172 // First goto not necessary, assume second one is...
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001173 Out << " if (!";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001174 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001175 Out << ") {\n";
1176
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001177 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001178 }
1179
1180 Out << " }\n";
1181 } else {
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001182 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001183 }
1184 Out << "\n";
1185}
1186
Chris Lattner84e66652003-05-03 07:11:00 +00001187// PHI nodes get copied into temporary values at the end of predecessor basic
1188// blocks. We now need to copy these temporary values into the REAL value for
1189// the PHI.
1190void CWriter::visitPHINode(PHINode &I) {
1191 writeOperand(&I);
1192 Out << "__PHI_TEMPORARY";
1193}
1194
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001195
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001196void CWriter::visitBinaryOperator(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001197 // binary instructions, shift instructions, setCond instructions.
Chris Lattnerf5612b762003-04-23 19:09:22 +00001198 assert(!isa<PointerType>(I.getType()));
Brian Gaeke031a1122003-06-23 20:00:51 +00001199
1200 // We must cast the results of binary operations which might be promoted.
1201 bool needsCast = false;
1202 if ((I.getType() == Type::UByteTy) || (I.getType() == Type::SByteTy)
Brian Gaekee99f4cf2003-06-25 03:05:33 +00001203 || (I.getType() == Type::UShortTy) || (I.getType() == Type::ShortTy)
1204 || (I.getType() == Type::FloatTy)) {
Brian Gaeke031a1122003-06-23 20:00:51 +00001205 needsCast = true;
1206 Out << "((";
Chris Lattner7635ea42003-11-03 01:01:59 +00001207 printType(Out, I.getType());
Brian Gaeke031a1122003-06-23 20:00:51 +00001208 Out << ")(";
1209 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001210
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001211 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001212
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001213 switch (I.getOpcode()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001214 case Instruction::Add: Out << " + "; break;
1215 case Instruction::Sub: Out << " - "; break;
1216 case Instruction::Mul: Out << "*"; break;
1217 case Instruction::Div: Out << "/"; break;
1218 case Instruction::Rem: Out << "%"; break;
1219 case Instruction::And: Out << " & "; break;
1220 case Instruction::Or: Out << " | "; break;
1221 case Instruction::Xor: Out << " ^ "; break;
1222 case Instruction::SetEQ: Out << " == "; break;
1223 case Instruction::SetNE: Out << " != "; break;
1224 case Instruction::SetLE: Out << " <= "; break;
1225 case Instruction::SetGE: Out << " >= "; break;
1226 case Instruction::SetLT: Out << " < "; break;
1227 case Instruction::SetGT: Out << " > "; break;
1228 case Instruction::Shl : Out << " << "; break;
1229 case Instruction::Shr : Out << " >> "; break;
Chris Lattnere7f65d3b2002-06-30 16:07:20 +00001230 default: std::cerr << "Invalid operator type!" << I; abort();
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001231 }
1232
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001233 writeOperand(I.getOperand(1));
Brian Gaeke031a1122003-06-23 20:00:51 +00001234
1235 if (needsCast) {
1236 Out << "))";
1237 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001238}
1239
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001240void CWriter::visitCastInst(CastInst &I) {
Chris Lattnerd13bd222003-06-01 03:36:51 +00001241 if (I.getType() == Type::BoolTy) {
1242 Out << "(";
1243 writeOperand(I.getOperand(0));
1244 Out << " != 0)";
1245 return;
1246 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001247 Out << "(";
Chris Lattner7635ea42003-11-03 01:01:59 +00001248 printType(Out, I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001249 Out << ")";
Chris Lattnerf5612b762003-04-23 19:09:22 +00001250 if (isa<PointerType>(I.getType())&&I.getOperand(0)->getType()->isIntegral() ||
1251 isa<PointerType>(I.getOperand(0)->getType())&&I.getType()->isIntegral()) {
1252 // Avoid "cast to pointer from integer of different size" warnings
1253 Out << "(long)";
1254 }
Chris Lattnerd13bd222003-06-01 03:36:51 +00001255
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001256 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001257}
1258
Chris Lattner9f24a072004-03-12 05:52:14 +00001259void CWriter::visitSelectInst(SelectInst &I) {
1260 Out << "((";
1261 writeOperand(I.getCondition());
1262 Out << ") ? (";
1263 writeOperand(I.getTrueValue());
1264 Out << ") : (";
1265 writeOperand(I.getFalseValue());
1266 Out << "))";
1267}
1268
1269
Chris Lattnerc2421432004-05-09 04:30:20 +00001270void CWriter::lowerIntrinsics(Function &F) {
1271 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1272 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1273 if (CallInst *CI = dyn_cast<CallInst>(I++))
1274 if (Function *F = CI->getCalledFunction())
1275 switch (F->getIntrinsicID()) {
1276 case Intrinsic::not_intrinsic:
1277 case Intrinsic::vastart:
1278 case Intrinsic::vacopy:
1279 case Intrinsic::vaend:
1280 case Intrinsic::returnaddress:
1281 case Intrinsic::frameaddress:
1282 case Intrinsic::setjmp:
1283 case Intrinsic::longjmp:
1284 // We directly implement these intrinsics
1285 break;
1286 default:
1287 // All other intrinsic calls we must lower.
1288 Instruction *Before = CI->getPrev();
1289 IL.LowerIntrinsicCall(CI);
1290 if (Before) { // Move iterator to instruction after call
1291 I = Before; ++I;
1292 } else {
1293 I = BB->begin();
Chris Lattner4ef51372004-02-14 00:31:10 +00001294 }
Chris Lattnerc2421432004-05-09 04:30:20 +00001295 }
Chris Lattner4ef51372004-02-14 00:31:10 +00001296}
1297
1298
1299
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001300void CWriter::visitCallInst(CallInst &I) {
Chris Lattner18ac3c82003-05-08 18:41:45 +00001301 // Handle intrinsic function calls first...
1302 if (Function *F = I.getCalledFunction())
Brian Gaeked0fde302003-11-11 22:41:34 +00001303 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
Chris Lattner18ac3c82003-05-08 18:41:45 +00001304 switch (ID) {
Chris Lattner4ef51372004-02-14 00:31:10 +00001305 default: assert(0 && "Unknown LLVM intrinsic!");
Chris Lattner317201d2004-03-13 00:24:00 +00001306 case Intrinsic::vastart:
Chris Lattner4d45bd02003-10-18 05:57:43 +00001307 Out << "0; ";
1308
1309 Out << "va_start(*(va_list*)&" << Mang->getValueName(&I) << ", ";
Chris Lattner18ac3c82003-05-08 18:41:45 +00001310 // Output the last argument to the enclosing function...
Chris Lattner4065ef92003-10-23 18:39:22 +00001311 if (I.getParent()->getParent()->aempty()) {
1312 std::cerr << "The C backend does not currently support zero "
1313 << "argument varargs functions, such as '"
1314 << I.getParent()->getParent()->getName() << "'!\n";
1315 abort();
1316 }
Chris Lattner18ac3c82003-05-08 18:41:45 +00001317 writeOperand(&I.getParent()->getParent()->aback());
1318 Out << ")";
1319 return;
Chris Lattner317201d2004-03-13 00:24:00 +00001320 case Intrinsic::vaend:
Chris Lattner4d45bd02003-10-18 05:57:43 +00001321 Out << "va_end(*(va_list*)&";
Chris Lattner18ac3c82003-05-08 18:41:45 +00001322 writeOperand(I.getOperand(1));
1323 Out << ")";
1324 return;
Chris Lattner317201d2004-03-13 00:24:00 +00001325 case Intrinsic::vacopy:
Chris Lattner4d45bd02003-10-18 05:57:43 +00001326 Out << "0;";
1327 Out << "va_copy(*(va_list*)&" << Mang->getValueName(&I) << ", ";
1328 Out << "*(va_list*)&";
Chris Lattner18ac3c82003-05-08 18:41:45 +00001329 writeOperand(I.getOperand(1));
Chris Lattner18ac3c82003-05-08 18:41:45 +00001330 Out << ")";
1331 return;
Chris Lattnerfe1554a2004-02-14 02:55:36 +00001332 case Intrinsic::returnaddress:
1333 Out << "__builtin_return_address(";
1334 writeOperand(I.getOperand(1));
1335 Out << ")";
1336 return;
1337 case Intrinsic::frameaddress:
1338 Out << "__builtin_frame_address(";
1339 writeOperand(I.getOperand(1));
1340 Out << ")";
1341 return;
Chris Lattnere42cde22004-02-15 22:51:47 +00001342 case Intrinsic::setjmp:
1343 Out << "setjmp(*(jmp_buf*)";
1344 writeOperand(I.getOperand(1));
1345 Out << ")";
1346 return;
1347 case Intrinsic::longjmp:
1348 Out << "longjmp(*(jmp_buf*)";
1349 writeOperand(I.getOperand(1));
1350 Out << ", ";
1351 writeOperand(I.getOperand(2));
1352 Out << ")";
1353 return;
Chris Lattner18ac3c82003-05-08 18:41:45 +00001354 }
1355 }
Chris Lattner9bda5f52003-06-28 17:53:05 +00001356 visitCallSite(&I);
1357}
Chris Lattner18ac3c82003-05-08 18:41:45 +00001358
Chris Lattner9bda5f52003-06-28 17:53:05 +00001359void CWriter::visitCallSite(CallSite CS) {
1360 const PointerType *PTy = cast<PointerType>(CS.getCalledValue()->getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001361 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1362 const Type *RetTy = FTy->getReturnType();
1363
Chris Lattner9bda5f52003-06-28 17:53:05 +00001364 writeOperand(CS.getCalledValue());
Chris Lattnerfabc8802002-08-26 20:50:09 +00001365 Out << "(";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001366
Chris Lattner9bda5f52003-06-28 17:53:05 +00001367 if (CS.arg_begin() != CS.arg_end()) {
1368 CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
1369 writeOperand(*AI);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001370
Chris Lattner9bda5f52003-06-28 17:53:05 +00001371 for (++AI; AI != AE; ++AI) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001372 Out << ", ";
Chris Lattner9bda5f52003-06-28 17:53:05 +00001373 writeOperand(*AI);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001374 }
1375 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001376 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001377}
1378
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001379void CWriter::visitMallocInst(MallocInst &I) {
Chris Lattnerf31182a2004-02-13 23:18:48 +00001380 assert(0 && "lowerallocations pass didn't work!");
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001381}
1382
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001383void CWriter::visitAllocaInst(AllocaInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001384 Out << "(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001385 printType(Out, I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001386 Out << ") alloca(sizeof(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001387 printType(Out, I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001388 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001389 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001390 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001391 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001392 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001393 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001394}
1395
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001396void CWriter::visitFreeInst(FreeInst &I) {
Chris Lattnerf31182a2004-02-13 23:18:48 +00001397 assert(0 && "lowerallocations pass didn't work!");
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001398}
1399
Chris Lattner23e90702003-11-25 20:49:55 +00001400void CWriter::printIndexingExpression(Value *Ptr, gep_type_iterator I,
1401 gep_type_iterator E) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001402 bool HasImplicitAddress = false;
1403 // If accessing a global value with no indexing, avoid *(&GV) syndrome
1404 if (GlobalValue *V = dyn_cast<GlobalValue>(Ptr)) {
1405 HasImplicitAddress = true;
1406 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Ptr)) {
1407 HasImplicitAddress = true;
1408 Ptr = CPR->getValue(); // Get to the global...
Chris Lattnera8ab89e2003-06-17 04:39:14 +00001409 } else if (isDirectAlloca(Ptr)) {
1410 HasImplicitAddress = true;
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001411 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001412
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001413 if (I == E) {
1414 if (!HasImplicitAddress)
1415 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001416
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001417 writeOperandInternal(Ptr);
1418 return;
1419 }
1420
Chris Lattner23e90702003-11-25 20:49:55 +00001421 const Constant *CI = dyn_cast<Constant>(I.getOperand());
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001422 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
1423 Out << "(&";
1424
1425 writeOperandInternal(Ptr);
1426
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001427 if (HasImplicitAddress && (!CI || !CI->isNullValue())) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001428 Out << ")";
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001429 HasImplicitAddress = false; // HIA is only true if we haven't addressed yet
1430 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001431
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001432 assert(!HasImplicitAddress || (CI && CI->isNullValue()) &&
1433 "Can only have implicit address with direct accessing");
1434
1435 if (HasImplicitAddress) {
1436 ++I;
Chris Lattner23e90702003-11-25 20:49:55 +00001437 } else if (CI && CI->isNullValue()) {
1438 gep_type_iterator TmpI = I; ++TmpI;
1439
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001440 // Print out the -> operator if possible...
Chris Lattner23e90702003-11-25 20:49:55 +00001441 if (TmpI != E && isa<StructType>(*TmpI)) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001442 Out << (HasImplicitAddress ? "." : "->");
Chris Lattnerbc771c52003-11-25 23:44:40 +00001443 Out << "field" << cast<ConstantUInt>(TmpI.getOperand())->getValue();
Chris Lattner23e90702003-11-25 20:49:55 +00001444 I = ++TmpI;
1445 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001446 }
Chris Lattner5dfe7672002-08-22 22:48:55 +00001447
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001448 for (; I != E; ++I)
Chris Lattner23e90702003-11-25 20:49:55 +00001449 if (isa<StructType>(*I)) {
1450 Out << ".field" << cast<ConstantUInt>(I.getOperand())->getValue();
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001451 } else {
Chris Lattner23e90702003-11-25 20:49:55 +00001452 Out << "[";
1453 writeOperand(I.getOperand());
1454 Out << "]";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001455 }
1456}
1457
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001458void CWriter::visitLoadInst(LoadInst &I) {
Nick Hildenbrandtca626922002-10-02 21:14:33 +00001459 Out << "*";
Chris Lattner5dfe7672002-08-22 22:48:55 +00001460 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001461}
1462
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001463void CWriter::visitStoreInst(StoreInst &I) {
Nick Hildenbrandtca626922002-10-02 21:14:33 +00001464 Out << "*";
Chris Lattner5dfe7672002-08-22 22:48:55 +00001465 writeOperand(I.getPointerOperand());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001466 Out << " = ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001467 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001468}
1469
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001470void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
Nick Hildenbrandtca626922002-10-02 21:14:33 +00001471 Out << "&";
Chris Lattner23e90702003-11-25 20:49:55 +00001472 printIndexingExpression(I.getPointerOperand(), gep_type_begin(I),
1473 gep_type_end(I));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001474}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001475
Chris Lattner4d45bd02003-10-18 05:57:43 +00001476void CWriter::visitVANextInst(VANextInst &I) {
1477 Out << Mang->getValueName(I.getOperand(0));
1478 Out << "; va_arg(*(va_list*)&" << Mang->getValueName(&I) << ", ";
Chris Lattner7635ea42003-11-03 01:01:59 +00001479 printType(Out, I.getArgType());
Chris Lattner18ac3c82003-05-08 18:41:45 +00001480 Out << ")";
1481}
1482
Chris Lattner4d45bd02003-10-18 05:57:43 +00001483void CWriter::visitVAArgInst(VAArgInst &I) {
1484 Out << "0;\n";
1485 Out << "{ va_list Tmp; va_copy(Tmp, *(va_list*)&";
1486 writeOperand(I.getOperand(0));
1487 Out << ");\n " << Mang->getValueName(&I) << " = va_arg(Tmp, ";
Chris Lattner7635ea42003-11-03 01:01:59 +00001488 printType(Out, I.getType());
Chris Lattner4d45bd02003-10-18 05:57:43 +00001489 Out << ");\n va_end(Tmp); }";
1490}
1491
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001492//===----------------------------------------------------------------------===//
1493// External Interface declaration
1494//===----------------------------------------------------------------------===//
1495
Chris Lattnerf31182a2004-02-13 23:18:48 +00001496bool CTargetMachine::addPassesToEmitAssembly(PassManager &PM, std::ostream &o) {
1497 PM.add(createLowerAllocationsPass());
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001498 PM.add(createLowerInvokePass());
Chris Lattner68758072004-05-09 06:20:51 +00001499 PM.add(new CBackendNameAllUsedStructs());
Chris Lattner4ef51372004-02-14 00:31:10 +00001500 PM.add(new CWriter(o, getIntrinsicLowering()));
Chris Lattnerf31182a2004-02-13 23:18:48 +00001501 return false;
1502}
1503
1504TargetMachine *llvm::allocateCTargetMachine(const Module &M,
1505 IntrinsicLowering *IL) {
1506 return new CTargetMachine(M, IL);
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001507}