blob: 6e536f072988a515d5182c9cde4727792178a9ab [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 Lattner433e25a2004-05-20 20:25:50 +0000129 // Always inline setcc instructions, even if they are shared by multiple
130 // expressions. GCC generates horrible code if we don't.
131 if (isa<SetCondInst>(I)) return true;
132
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000133 // Must be an expression, must be used exactly once. If it is dead, we
134 // emit it inline where it would go.
Chris Lattnerfd059242003-10-15 16:48:29 +0000135 if (I.getType() == Type::VoidTy || !I.hasOneUse() ||
Nick Hildenbrandt2cf2cbc2002-11-06 20:07:54 +0000136 isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I) ||
Chris Lattner4d45bd02003-10-18 05:57:43 +0000137 isa<LoadInst>(I) || isa<VAArgInst>(I) || isa<VANextInst>(I))
Chris Lattnerd4e8d312003-07-23 20:45:31 +0000138 // Don't inline a load across a store or other bad things!
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000139 return false;
140
141 // Only inline instruction it it's use is in the same BB as the inst.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000142 return I.getParent() == cast<Instruction>(I.use_back())->getParent();
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000143 }
144
Chris Lattnera8ab89e2003-06-17 04:39:14 +0000145 // isDirectAlloca - Define fixed sized allocas in the entry block as direct
146 // variables which are accessed with the & operator. This causes GCC to
147 // generate significantly better code than to emit alloca calls directly.
148 //
149 static const AllocaInst *isDirectAlloca(const Value *V) {
150 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
151 if (!AI) return false;
152 if (AI->isArrayAllocation())
153 return 0; // FIXME: we can also inline fixed size array allocas!
Chris Lattner02a3be02003-09-20 14:39:18 +0000154 if (AI->getParent() != &AI->getParent()->getParent()->getEntryBlock())
Chris Lattnera8ab89e2003-06-17 04:39:14 +0000155 return 0;
156 return AI;
157 }
158
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000159 // Instruction visitation functions
160 friend class InstVisitor<CWriter>;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000161
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000162 void visitReturnInst(ReturnInst &I);
163 void visitBranchInst(BranchInst &I);
Chris Lattnera9f5e052003-04-22 20:19:52 +0000164 void visitSwitchInst(SwitchInst &I);
Chris Lattnercb3ad012004-05-09 20:41:32 +0000165 void visitInvokeInst(InvokeInst &I) {
166 assert(0 && "Lowerinvoke pass didn't work!");
167 }
168
169 void visitUnwindInst(UnwindInst &I) {
170 assert(0 && "Lowerinvoke pass didn't work!");
171 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000172
Chris Lattner84e66652003-05-03 07:11:00 +0000173 void visitPHINode(PHINode &I);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000174 void visitBinaryOperator(Instruction &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000175
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000176 void visitCastInst (CastInst &I);
Chris Lattner9f24a072004-03-12 05:52:14 +0000177 void visitSelectInst(SelectInst &I);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000178 void visitCallInst (CallInst &I);
Chris Lattner9bda5f52003-06-28 17:53:05 +0000179 void visitCallSite (CallSite CS);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000180 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000181
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000182 void visitMallocInst(MallocInst &I);
183 void visitAllocaInst(AllocaInst &I);
184 void visitFreeInst (FreeInst &I);
185 void visitLoadInst (LoadInst &I);
186 void visitStoreInst (StoreInst &I);
187 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner4d45bd02003-10-18 05:57:43 +0000188 void visitVANextInst(VANextInst &I);
189 void visitVAArgInst (VAArgInst &I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000190
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000191 void visitInstruction(Instruction &I) {
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000192 std::cerr << "C Writer does not know about " << I;
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000193 abort();
194 }
195
196 void outputLValue(Instruction *I) {
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000197 Out << " " << Mang->getValueName(I) << " = ";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000198 }
Chris Lattnercb3ad012004-05-09 20:41:32 +0000199
200 bool isGotoCodeNecessary(BasicBlock *From, BasicBlock *To);
Chris Lattnera05e0ec2004-05-09 03:42:48 +0000201 void printPHICopiesForSuccessors(BasicBlock *CurBlock,
202 unsigned Indent);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000203 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
204 unsigned Indent);
Chris Lattner23e90702003-11-25 20:49:55 +0000205 void printIndexingExpression(Value *Ptr, gep_type_iterator I,
206 gep_type_iterator E);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000207 };
Chris Lattner4ef51372004-02-14 00:31:10 +0000208}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000209
Chris Lattner68758072004-05-09 06:20:51 +0000210/// This method inserts names for any unnamed structure types that are used by
211/// the program, and removes names from structure types that are not used by the
212/// program.
213///
214bool CBackendNameAllUsedStructs::run(Module &M) {
215 // Get a set of types that are used by the program...
216 std::set<const Type *> UT = getAnalysis<FindUsedTypes>().getTypes();
217
218 // Loop over the module symbol table, removing types from UT that are
219 // already named, and removing names for structure types that are not used.
220 //
221 SymbolTable &MST = M.getSymbolTable();
222 if (MST.find(Type::TypeTy) != MST.end())
223 for (SymbolTable::type_iterator I = MST.type_begin(Type::TypeTy),
224 E = MST.type_end(Type::TypeTy); I != E; ) {
225 SymbolTable::type_iterator It = I++;
226 if (StructType *STy = dyn_cast<StructType>(It->second)) {
227 // If this is not used, remove it from the symbol table.
228 std::set<const Type *>::iterator UTI = UT.find(STy);
229 if (UTI == UT.end())
230 MST.remove(It->first, It->second);
231 else
232 UT.erase(UTI);
233 }
234 }
235
236 // UT now contains types that are not named. Loop over it, naming
237 // structure types.
238 //
239 bool Changed = false;
240 for (std::set<const Type *>::const_iterator I = UT.begin(), E = UT.end();
241 I != E; ++I)
242 if (const StructType *ST = dyn_cast<StructType>(*I)) {
243 ((Value*)ST)->setName("unnamed", &MST);
244 Changed = true;
245 }
246 return Changed;
247}
248
249
Chris Lattner83c57752002-08-19 22:17:53 +0000250// Pass the Type* and the variable name and this prints out the variable
251// declaration.
252//
Chris Lattneredd8ce12003-05-03 03:14:35 +0000253std::ostream &CWriter::printType(std::ostream &Out, const Type *Ty,
254 const std::string &NameSoFar,
Chris Lattner7635ea42003-11-03 01:01:59 +0000255 bool IgnoreName) {
Chris Lattner83c57752002-08-19 22:17:53 +0000256 if (Ty->isPrimitiveType())
257 switch (Ty->getPrimitiveID()) {
258 case Type::VoidTyID: return Out << "void " << NameSoFar;
259 case Type::BoolTyID: return Out << "bool " << NameSoFar;
260 case Type::UByteTyID: return Out << "unsigned char " << NameSoFar;
261 case Type::SByteTyID: return Out << "signed char " << NameSoFar;
262 case Type::UShortTyID: return Out << "unsigned short " << NameSoFar;
263 case Type::ShortTyID: return Out << "short " << NameSoFar;
264 case Type::UIntTyID: return Out << "unsigned " << NameSoFar;
265 case Type::IntTyID: return Out << "int " << NameSoFar;
266 case Type::ULongTyID: return Out << "unsigned long long " << NameSoFar;
267 case Type::LongTyID: return Out << "signed long long " << NameSoFar;
268 case Type::FloatTyID: return Out << "float " << NameSoFar;
269 case Type::DoubleTyID: return Out << "double " << NameSoFar;
270 default :
271 std::cerr << "Unknown primitive type: " << Ty << "\n";
272 abort();
273 }
274
275 // Check to see if the type is named.
Chris Lattner04b72c82002-10-16 00:08:22 +0000276 if (!IgnoreName || isa<OpaqueType>(Ty)) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000277 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner5864da02003-08-24 21:00:22 +0000278 if (I != TypeNames.end()) return Out << I->second << " " << NameSoFar;
Chris Lattner04b72c82002-10-16 00:08:22 +0000279 }
Chris Lattner83c57752002-08-19 22:17:53 +0000280
Chris Lattner83c57752002-08-19 22:17:53 +0000281 switch (Ty->getPrimitiveID()) {
282 case Type::FunctionTyID: {
283 const FunctionType *MTy = cast<FunctionType>(Ty);
Joel Stanley54f60322003-06-25 04:52:09 +0000284 std::stringstream FunctionInnards;
285 FunctionInnards << " (" << NameSoFar << ") (";
Chris Lattnerd5d89962004-02-09 04:14:01 +0000286 for (FunctionType::param_iterator I = MTy->param_begin(),
287 E = MTy->param_end(); I != E; ++I) {
288 if (I != MTy->param_begin())
Joel Stanley54f60322003-06-25 04:52:09 +0000289 FunctionInnards << ", ";
290 printType(FunctionInnards, *I, "");
Chris Lattner83c57752002-08-19 22:17:53 +0000291 }
292 if (MTy->isVarArg()) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000293 if (MTy->getNumParams())
Joel Stanley54f60322003-06-25 04:52:09 +0000294 FunctionInnards << ", ...";
Chris Lattnerd5d89962004-02-09 04:14:01 +0000295 } else if (!MTy->getNumParams()) {
Joel Stanley54f60322003-06-25 04:52:09 +0000296 FunctionInnards << "void";
Chris Lattner83c57752002-08-19 22:17:53 +0000297 }
Joel Stanley54f60322003-06-25 04:52:09 +0000298 FunctionInnards << ")";
299 std::string tstr = FunctionInnards.str();
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000300 printType(Out, MTy->getReturnType(), tstr);
301 return Out;
Chris Lattner83c57752002-08-19 22:17:53 +0000302 }
303 case Type::StructTyID: {
304 const StructType *STy = cast<StructType>(Ty);
305 Out << NameSoFar + " {\n";
306 unsigned Idx = 0;
Chris Lattnerd21cd802004-02-09 04:37:31 +0000307 for (StructType::element_iterator I = STy->element_begin(),
308 E = STy->element_end(); I != E; ++I) {
Chris Lattner83c57752002-08-19 22:17:53 +0000309 Out << " ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000310 printType(Out, *I, "field" + utostr(Idx++));
Chris Lattner83c57752002-08-19 22:17:53 +0000311 Out << ";\n";
312 }
313 return Out << "}";
314 }
315
316 case Type::PointerTyID: {
317 const PointerType *PTy = cast<PointerType>(Ty);
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000318 std::string ptrName = "*" + NameSoFar;
319
Chris Lattner8917d362003-11-03 04:31:54 +0000320 if (isa<ArrayType>(PTy->getElementType()))
321 ptrName = "(" + ptrName + ")";
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000322
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000323 return printType(Out, PTy->getElementType(), ptrName);
Misha Brukman8f9f9a22003-07-21 16:34:35 +0000324 }
Chris Lattner83c57752002-08-19 22:17:53 +0000325
326 case Type::ArrayTyID: {
327 const ArrayType *ATy = cast<ArrayType>(Ty);
328 unsigned NumElements = ATy->getNumElements();
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000329 return printType(Out, ATy->getElementType(),
Chris Lattner83c57752002-08-19 22:17:53 +0000330 NameSoFar + "[" + utostr(NumElements) + "]");
331 }
Chris Lattner04b72c82002-10-16 00:08:22 +0000332
333 case Type::OpaqueTyID: {
334 static int Count = 0;
Chris Lattneredd8ce12003-05-03 03:14:35 +0000335 std::string TyName = "struct opaque_" + itostr(Count++);
Chris Lattner04b72c82002-10-16 00:08:22 +0000336 assert(TypeNames.find(Ty) == TypeNames.end());
337 TypeNames[Ty] = TyName;
338 return Out << TyName << " " << NameSoFar;
339 }
Chris Lattner83c57752002-08-19 22:17:53 +0000340 default:
341 assert(0 && "Unhandled case in getTypeProps!");
342 abort();
343 }
344
345 return Out;
346}
347
Chris Lattner6d492922002-08-19 21:32:41 +0000348void CWriter::printConstantArray(ConstantArray *CPA) {
349
350 // As a special case, print the array as a string if it is an array of
351 // ubytes or an array of sbytes with positive values.
352 //
353 const Type *ETy = CPA->getType()->getElementType();
354 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
355
356 // Make sure the last character is a null char, as automatically added by C
Chris Lattner02da6c02003-06-16 12:09:09 +0000357 if (isString && (CPA->getNumOperands() == 0 ||
358 !cast<Constant>(*(CPA->op_end()-1))->isNullValue()))
Chris Lattner6d492922002-08-19 21:32:41 +0000359 isString = false;
360
361 if (isString) {
362 Out << "\"";
Chris Lattner02da6c02003-06-16 12:09:09 +0000363 // Keep track of whether the last number was a hexadecimal escape
364 bool LastWasHex = false;
365
Chris Lattner6d492922002-08-19 21:32:41 +0000366 // Do not include the last character, which we know is null
367 for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000368 unsigned char C = cast<ConstantInt>(CPA->getOperand(i))->getRawValue();
Chris Lattner6d492922002-08-19 21:32:41 +0000369
Chris Lattner02da6c02003-06-16 12:09:09 +0000370 // Print it out literally if it is a printable character. The only thing
371 // to be careful about is when the last letter output was a hex escape
372 // code, in which case we have to be careful not to print out hex digits
Chris Lattnerda920902003-06-16 12:21:19 +0000373 // explicitly (the C compiler thinks it is a continuation of the previous
374 // character, sheesh...)
Chris Lattner02da6c02003-06-16 12:09:09 +0000375 //
376 if (isprint(C) && (!LastWasHex || !isxdigit(C))) {
377 LastWasHex = false;
Nick Hildenbrandt088b4722002-11-06 21:40:23 +0000378 if (C == '"' || C == '\\')
379 Out << "\\" << C;
Nick Hildenbrandtc3dd2af2002-09-30 21:11:55 +0000380 else
381 Out << C;
Chris Lattner6d492922002-08-19 21:32:41 +0000382 } else {
Chris Lattner02da6c02003-06-16 12:09:09 +0000383 LastWasHex = false;
Chris Lattner6d492922002-08-19 21:32:41 +0000384 switch (C) {
385 case '\n': Out << "\\n"; break;
386 case '\t': Out << "\\t"; break;
387 case '\r': Out << "\\r"; break;
388 case '\v': Out << "\\v"; break;
389 case '\a': Out << "\\a"; break;
Nick Hildenbrandtc3dd2af2002-09-30 21:11:55 +0000390 case '\"': Out << "\\\""; break;
391 case '\'': Out << "\\\'"; break;
Chris Lattner6d492922002-08-19 21:32:41 +0000392 default:
393 Out << "\\x";
Chris Lattner02da6c02003-06-16 12:09:09 +0000394 Out << (char)(( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'));
395 Out << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
396 LastWasHex = true;
Chris Lattner6d492922002-08-19 21:32:41 +0000397 break;
398 }
399 }
400 }
401 Out << "\"";
402 } else {
403 Out << "{";
404 if (CPA->getNumOperands()) {
405 Out << " ";
406 printConstant(cast<Constant>(CPA->getOperand(0)));
407 for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
408 Out << ", ";
409 printConstant(cast<Constant>(CPA->getOperand(i)));
410 }
411 }
412 Out << " }";
413 }
414}
415
Chris Lattnerdd76aca2003-10-05 00:40:51 +0000416// isFPCSafeToPrint - Returns true if we may assume that CFP may be written out
417// textually as a double (rather than as a reference to a stack-allocated
418// variable). We decide this by converting CFP to a string and back into a
419// double, and then checking whether the conversion results in a bit-equal
420// double to the original value of CFP. This depends on us and the target C
421// compiler agreeing on the conversion process (which is pretty likely since we
422// only deal in IEEE FP).
423//
Chris Lattnercb3ad012004-05-09 20:41:32 +0000424static bool isFPCSafeToPrint(const ConstantFP *CFP) {
Chris Lattner4946a8c2003-10-13 17:13:53 +0000425#if HAVE_PRINTF_A
Chris Lattner5ef6ffd2003-10-12 08:12:58 +0000426 char Buffer[100];
427 sprintf(Buffer, "%a", CFP->getValue());
428
429 if (!strncmp(Buffer, "0x", 2) ||
430 !strncmp(Buffer, "-0x", 3) ||
431 !strncmp(Buffer, "+0x", 3))
432 return atof(Buffer) == CFP->getValue();
433 return false;
434#else
Brian Gaekeb471a232003-06-17 23:55:35 +0000435 std::string StrVal = ftostr(CFP->getValue());
Chris Lattner9860e772003-10-12 04:36:29 +0000436
437 while (StrVal[0] == ' ')
438 StrVal.erase(StrVal.begin());
439
Chris Lattnerdd76aca2003-10-05 00:40:51 +0000440 // Check to make sure that the stringized number is not some string like "Inf"
441 // or NaN. Check that the string matches the "[-+]?[0-9]" regex.
Brian Gaekeb471a232003-06-17 23:55:35 +0000442 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
443 ((StrVal[0] == '-' || StrVal[0] == '+') &&
444 (StrVal[1] >= '0' && StrVal[1] <= '9')))
445 // Reparse stringized version!
Chris Lattnerdd76aca2003-10-05 00:40:51 +0000446 return atof(StrVal.c_str()) == CFP->getValue();
Brian Gaekeb471a232003-06-17 23:55:35 +0000447 return false;
Chris Lattner5ef6ffd2003-10-12 08:12:58 +0000448#endif
Brian Gaekeb471a232003-06-17 23:55:35 +0000449}
Chris Lattner6d492922002-08-19 21:32:41 +0000450
451// printConstant - The LLVM Constant to C Constant converter.
452void CWriter::printConstant(Constant *CPV) {
453 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
454 switch (CE->getOpcode()) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000455 case Instruction::Cast:
456 Out << "((";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000457 printType(Out, CPV->getType());
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000458 Out << ")";
Chris Lattner84c0d5e2003-05-14 17:50:19 +0000459 printConstant(CE->getOperand(0));
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000460 Out << ")";
461 return;
462
463 case Instruction::GetElementPtr:
Nick Hildenbrandtd2eb3862002-10-03 20:47:24 +0000464 Out << "(&(";
Chris Lattner23e90702003-11-25 20:49:55 +0000465 printIndexingExpression(CE->getOperand(0), gep_type_begin(CPV),
466 gep_type_end(CPV));
Nick Hildenbrandtd2eb3862002-10-03 20:47:24 +0000467 Out << "))";
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000468 return;
Chris Lattner6683dbf2004-04-01 05:28:26 +0000469 case Instruction::Select:
470 Out << "(";
471 printConstant(CE->getOperand(0));
472 Out << "?";
473 printConstant(CE->getOperand(1));
474 Out << ":";
475 printConstant(CE->getOperand(2));
476 Out << ")";
477 return;
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000478 case Instruction::Add:
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000479 case Instruction::Sub:
Chris Lattner29255922003-08-14 19:19:53 +0000480 case Instruction::Mul:
481 case Instruction::Div:
482 case Instruction::Rem:
483 case Instruction::SetEQ:
484 case Instruction::SetNE:
485 case Instruction::SetLT:
486 case Instruction::SetLE:
487 case Instruction::SetGT:
488 case Instruction::SetGE:
Brian Gaeke0415b6c2003-11-22 05:02:56 +0000489 case Instruction::Shl:
490 case Instruction::Shr:
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000491 Out << "(";
Chris Lattner84c0d5e2003-05-14 17:50:19 +0000492 printConstant(CE->getOperand(0));
Chris Lattner29255922003-08-14 19:19:53 +0000493 switch (CE->getOpcode()) {
494 case Instruction::Add: Out << " + "; break;
495 case Instruction::Sub: Out << " - "; break;
496 case Instruction::Mul: Out << " * "; break;
497 case Instruction::Div: Out << " / "; break;
498 case Instruction::Rem: Out << " % "; break;
499 case Instruction::SetEQ: Out << " == "; break;
500 case Instruction::SetNE: Out << " != "; break;
501 case Instruction::SetLT: Out << " < "; break;
502 case Instruction::SetLE: Out << " <= "; break;
503 case Instruction::SetGT: Out << " > "; break;
504 case Instruction::SetGE: Out << " >= "; break;
Brian Gaeke0415b6c2003-11-22 05:02:56 +0000505 case Instruction::Shl: Out << " << "; break;
506 case Instruction::Shr: Out << " >> "; break;
Chris Lattner29255922003-08-14 19:19:53 +0000507 default: assert(0 && "Illegal opcode here!");
508 }
Chris Lattner84c0d5e2003-05-14 17:50:19 +0000509 printConstant(CE->getOperand(1));
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000510 Out << ")";
511 return;
512
Chris Lattner6d492922002-08-19 21:32:41 +0000513 default:
514 std::cerr << "CWriter Error: Unhandled constant expression: "
515 << CE << "\n";
516 abort();
517 }
518 }
519
520 switch (CPV->getType()->getPrimitiveID()) {
521 case Type::BoolTyID:
522 Out << (CPV == ConstantBool::False ? "0" : "1"); break;
523 case Type::SByteTyID:
524 case Type::ShortTyID:
Chris Lattner6d492922002-08-19 21:32:41 +0000525 Out << cast<ConstantSInt>(CPV)->getValue(); break;
Chris Lattner45343ea2003-05-12 15:39:31 +0000526 case Type::IntTyID:
527 if ((int)cast<ConstantSInt>(CPV)->getValue() == (int)0x80000000)
528 Out << "((int)0x80000000)"; // Handle MININT specially to avoid warning
529 else
530 Out << cast<ConstantSInt>(CPV)->getValue();
531 break;
532
Chris Lattner6d492922002-08-19 21:32:41 +0000533 case Type::LongTyID:
534 Out << cast<ConstantSInt>(CPV)->getValue() << "ll"; break;
535
536 case Type::UByteTyID:
537 case Type::UShortTyID:
538 Out << cast<ConstantUInt>(CPV)->getValue(); break;
539 case Type::UIntTyID:
540 Out << cast<ConstantUInt>(CPV)->getValue() << "u"; break;
541 case Type::ULongTyID:
542 Out << cast<ConstantUInt>(CPV)->getValue() << "ull"; break;
543
544 case Type::FloatTyID:
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000545 case Type::DoubleTyID: {
546 ConstantFP *FPC = cast<ConstantFP>(CPV);
Chris Lattneredd8ce12003-05-03 03:14:35 +0000547 std::map<const ConstantFP*, unsigned>::iterator I = FPConstantMap.find(FPC);
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000548 if (I != FPConstantMap.end()) {
549 // Because of FP precision problems we must load from a stack allocated
550 // value that holds the value in hex.
551 Out << "(*(" << (FPC->getType() == Type::FloatTy ? "float" : "double")
Chris Lattner9860e772003-10-12 04:36:29 +0000552 << "*)&FPConstant" << I->second << ")";
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000553 } else {
Chris Lattner4946a8c2003-10-13 17:13:53 +0000554#if HAVE_PRINTF_A
Chris Lattnerdd76aca2003-10-05 00:40:51 +0000555 // Print out the constant as a floating point number.
Chris Lattner5ef6ffd2003-10-12 08:12:58 +0000556 char Buffer[100];
557 sprintf(Buffer, "%a", FPC->getValue());
558 Out << Buffer << " /*" << FPC->getValue() << "*/ ";
559#else
Chris Lattnerdd76aca2003-10-05 00:40:51 +0000560 Out << ftostr(FPC->getValue());
Chris Lattner5ef6ffd2003-10-12 08:12:58 +0000561#endif
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000562 }
563 break;
564 }
Chris Lattner6d492922002-08-19 21:32:41 +0000565
566 case Type::ArrayTyID:
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000567 if (isa<ConstantAggregateZero>(CPV)) {
568 const ArrayType *AT = cast<ArrayType>(CPV->getType());
569 Out << "{";
570 if (AT->getNumElements()) {
571 Out << " ";
572 Constant *CZ = Constant::getNullValue(AT->getElementType());
573 printConstant(CZ);
574 for (unsigned i = 1, e = AT->getNumElements(); i != e; ++i) {
575 Out << ", ";
576 printConstant(CZ);
577 }
578 }
579 Out << " }";
580 } else {
581 printConstantArray(cast<ConstantArray>(CPV));
582 }
Chris Lattner6d492922002-08-19 21:32:41 +0000583 break;
584
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000585 case Type::StructTyID:
586 if (isa<ConstantAggregateZero>(CPV)) {
587 const StructType *ST = cast<StructType>(CPV->getType());
588 Out << "{";
589 if (ST->getNumElements()) {
590 Out << " ";
591 printConstant(Constant::getNullValue(ST->getElementType(0)));
592 for (unsigned i = 1, e = ST->getNumElements(); i != e; ++i) {
593 Out << ", ";
594 printConstant(Constant::getNullValue(ST->getElementType(i)));
595 }
Chris Lattner6d492922002-08-19 21:32:41 +0000596 }
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000597 Out << " }";
598 } else {
599 Out << "{";
600 if (CPV->getNumOperands()) {
601 Out << " ";
602 printConstant(cast<Constant>(CPV->getOperand(0)));
603 for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) {
604 Out << ", ";
605 printConstant(cast<Constant>(CPV->getOperand(i)));
606 }
607 }
608 Out << " }";
Chris Lattner6d492922002-08-19 21:32:41 +0000609 }
Chris Lattner6d492922002-08-19 21:32:41 +0000610 break;
Chris Lattner6d492922002-08-19 21:32:41 +0000611
612 case Type::PointerTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000613 if (isa<ConstantPointerNull>(CPV)) {
Chris Lattnercf135cb2003-06-02 03:10:53 +0000614 Out << "((";
615 printType(Out, CPV->getType());
616 Out << ")/*NULL*/0)";
Chris Lattner6d492922002-08-19 21:32:41 +0000617 break;
618 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
619 writeOperand(CPR->getValue());
620 break;
621 }
622 // FALL THROUGH
623 default:
624 std::cerr << "Unknown constant type: " << CPV << "\n";
625 abort();
626 }
627}
628
629void CWriter::writeOperandInternal(Value *Operand) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000630 if (Instruction *I = dyn_cast<Instruction>(Operand))
Chris Lattnera8ab89e2003-06-17 04:39:14 +0000631 if (isInlinableInst(*I) && !isDirectAlloca(I)) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000632 // Should we inline this instruction to build a tree?
633 Out << "(";
634 visit(*I);
635 Out << ")";
636 return;
637 }
638
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000639 if (Constant *CPV = dyn_cast<Constant>(Operand)) {
Chris Lattner6d492922002-08-19 21:32:41 +0000640 printConstant(CPV);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000641 } else {
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000642 Out << Mang->getValueName(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000643 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000644}
645
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000646void CWriter::writeOperand(Value *Operand) {
Chris Lattnera8ab89e2003-06-17 04:39:14 +0000647 if (isa<GlobalVariable>(Operand) || isDirectAlloca(Operand))
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000648 Out << "(&"; // Global variables are references as their addresses by llvm
649
650 writeOperandInternal(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000651
Chris Lattnera8ab89e2003-06-17 04:39:14 +0000652 if (isa<GlobalVariable>(Operand) || isDirectAlloca(Operand))
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000653 Out << ")";
654}
655
Chris Lattner41488192003-06-28 17:15:12 +0000656// generateCompilerSpecificCode - This is where we add conditional compilation
657// directives to cater to specific compilers as need be.
658//
Chris Lattnerc59c1182003-11-16 22:06:14 +0000659static void generateCompilerSpecificCode(std::ostream& Out) {
Chris Lattner41488192003-06-28 17:15:12 +0000660 // Alloca is hard to get, and we don't want to include stdlib.h here...
661 Out << "/* get a declaration for alloca */\n"
662 << "#ifdef sun\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000663 << "extern void *__builtin_alloca(unsigned long);\n"
664 << "#define alloca(x) __builtin_alloca(x)\n"
665 << "#else\n"
Brian Gaekea0145cc2003-06-16 23:57:13 +0000666 << "#ifndef __FreeBSD__\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000667 << "#include <alloca.h>\n"
Brian Gaekea0145cc2003-06-16 23:57:13 +0000668 << "#endif\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000669 << "#endif\n\n";
Chris Lattner41488192003-06-28 17:15:12 +0000670
671 // We output GCC specific attributes to preserve 'linkonce'ness on globals.
672 // If we aren't being compiled with GCC, just drop these attributes.
Chris Lattner9bda5f52003-06-28 17:53:05 +0000673 Out << "#ifndef __GNUC__ /* Can only support \"linkonce\" vars with GCC */\n"
Chris Lattner41488192003-06-28 17:15:12 +0000674 << "#define __attribute__(X)\n"
Brian Gaeke27f7a712003-12-11 00:24:36 +0000675 << "#endif\n\n";
676
677#if 0
678 // At some point, we should support "external weak" vs. "weak" linkages.
679 // On Mac OS X, "external weak" is spelled "__attribute__((weak_import))".
680 Out << "#if defined(__GNUC__) && defined(__APPLE_CC__)\n"
681 << "#define __EXTERNAL_WEAK__ __attribute__((weak_import))\n"
682 << "#elif defined(__GNUC__)\n"
683 << "#define __EXTERNAL_WEAK__ __attribute__((weak))\n"
684 << "#else\n"
685 << "#define __EXTERNAL_WEAK__\n"
686 << "#endif\n\n";
687#endif
688
689 // For now, turn off the weak linkage attribute on Mac OS X. (See above.)
690 Out << "#if defined(__GNUC__) && defined(__APPLE_CC__)\n"
691 << "#define __ATTRIBUTE_WEAK__\n"
692 << "#elif defined(__GNUC__)\n"
693 << "#define __ATTRIBUTE_WEAK__ __attribute__((weak))\n"
694 << "#else\n"
695 << "#define __ATTRIBUTE_WEAK__\n"
696 << "#endif\n\n";
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000697}
698
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000699bool CWriter::doInitialization(Module &M) {
700 // Initialize
701 TheModule = &M;
Chris Lattnerc2421432004-05-09 04:30:20 +0000702
703 IL.AddPrototypes(M);
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000704
705 // Ensure that all structure types have names...
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000706 Mang = new Mangler(M);
Chris Lattnerc59c1182003-11-16 22:06:14 +0000707
Chris Lattner16c7bb22002-05-09 02:28:59 +0000708 // get declaration for alloca
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000709 Out << "/* Provide Declarations */\n";
Chris Lattnerc59c1182003-11-16 22:06:14 +0000710 Out << "#include <stdarg.h>\n"; // Varargs support
711 Out << "#include <setjmp.h>\n"; // Unwind support
Chris Lattner41488192003-06-28 17:15:12 +0000712 generateCompilerSpecificCode(Out);
Chris Lattnerc59c1182003-11-16 22:06:14 +0000713
Chris Lattnercf135cb2003-06-02 03:10:53 +0000714 // Provide a definition for `bool' if not compiling with a C++ compiler.
715 Out << "\n"
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000716 << "#ifndef __cplusplus\ntypedef unsigned char bool;\n#endif\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000717
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000718 << "\n\n/* Support for floating point constants */\n"
719 << "typedef unsigned long long ConstantDoubleTy;\n"
Chris Lattnereb6f8c72002-11-07 19:43:59 +0000720 << "typedef unsigned int ConstantFloatTy;\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000721
Chris Lattnera4d4a852002-08-19 21:48:40 +0000722 << "\n\n/* Global Declarations */\n";
723
724 // First output all the declarations for the program, because C requires
725 // Functions & globals to be declared before they are used.
726 //
Chris Lattner16c7bb22002-05-09 02:28:59 +0000727
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000728 // Loop over the symbol table, emitting all named constants...
Chris Lattner68758072004-05-09 06:20:51 +0000729 printModuleTypes(M.getSymbolTable());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000730
Chris Lattnera4d4a852002-08-19 21:48:40 +0000731 // Global variable declarations...
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000732 if (!M.gempty()) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000733 Out << "\n/* External Global Variable Declarations */\n";
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000734 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000735 if (I->hasExternalLinkage()) {
736 Out << "extern ";
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000737 printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000738 Out << ";\n";
739 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000740 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000741 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000742
Chris Lattnera4d4a852002-08-19 21:48:40 +0000743 // Function declarations
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000744 if (!M.empty()) {
Chris Lattnera4d4a852002-08-19 21:48:40 +0000745 Out << "\n/* Function Declarations */\n";
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000746 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner4ef51372004-02-14 00:31:10 +0000747 // Don't print declarations for intrinsic functions.
Chris Lattner4589ed92004-05-09 16:03:29 +0000748 if (!I->getIntrinsicID() &&
749 I->getName() != "setjmp" && I->getName() != "longjmp") {
Nick Hildenbrandta1a64f82002-11-18 22:21:52 +0000750 printFunctionSignature(I, true);
Brian Gaeke27f7a712003-12-11 00:24:36 +0000751 if (I->hasWeakLinkage()) Out << " __ATTRIBUTE_WEAK__";
John Criswell3799eec2004-02-26 22:20:58 +0000752 if (I->hasLinkOnceLinkage()) Out << " __ATTRIBUTE_WEAK__";
Nick Hildenbrandta1a64f82002-11-18 22:21:52 +0000753 Out << ";\n";
754 }
Chris Lattner4cda8352002-08-20 16:55:48 +0000755 }
Chris Lattnera4d4a852002-08-19 21:48:40 +0000756 }
757
Joel Stanley54f60322003-06-25 04:52:09 +0000758 // Output the global variable declarations
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000759 if (!M.gempty()) {
Joel Stanley54f60322003-06-25 04:52:09 +0000760 Out << "\n\n/* Global Variable Declarations */\n";
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000761 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +0000762 if (!I->isExternal()) {
763 Out << "extern ";
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000764 printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
Chris Lattner2580d4f2003-11-03 17:32:38 +0000765
766 if (I->hasLinkOnceLinkage())
767 Out << " __attribute__((common))";
768 else if (I->hasWeakLinkage())
Brian Gaeke27f7a712003-12-11 00:24:36 +0000769 Out << " __ATTRIBUTE_WEAK__";
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +0000770 Out << ";\n";
771 }
772 }
773
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000774 // Output the global variable definitions and contents...
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000775 if (!M.gempty()) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +0000776 Out << "\n\n/* Global Variable Definitions and Initialization */\n";
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000777 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
Chris Lattnere8e035b2002-10-16 20:08:47 +0000778 if (!I->isExternal()) {
779 if (I->hasInternalLinkage())
780 Out << "static ";
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000781 printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
Chris Lattner76d9f1b2003-06-28 17:08:36 +0000782 if (I->hasLinkOnceLinkage())
783 Out << " __attribute__((common))";
Chris Lattner72ac148d2003-10-16 18:29:00 +0000784 else if (I->hasWeakLinkage())
Brian Gaeke27f7a712003-12-11 00:24:36 +0000785 Out << " __ATTRIBUTE_WEAK__";
Chris Lattner5ea326a2003-11-03 17:35:00 +0000786
787 // If the initializer is not null, emit the initializer. If it is null,
788 // we try to avoid emitting large amounts of zeros. The problem with
789 // this, however, occurs when the variable has weak linkage. In this
790 // case, the assembler will complain about the variable being both weak
791 // and common, so we disable this optimization.
Chris Lattnerf358c5a2004-02-20 05:49:22 +0000792 if (!I->getInitializer()->isNullValue()) {
Chris Lattner940b08d2003-06-06 07:10:24 +0000793 Out << " = " ;
794 writeOperand(I->getInitializer());
Chris Lattnerf358c5a2004-02-20 05:49:22 +0000795 } else if (I->hasWeakLinkage()) {
796 // We have to specify an initializer, but it doesn't have to be
797 // complete. If the value is an aggregate, print out { 0 }, and let
798 // the compiler figure out the rest of the zeros.
799 Out << " = " ;
800 if (isa<StructType>(I->getInitializer()->getType()) ||
801 isa<ArrayType>(I->getInitializer()->getType())) {
802 Out << "{ 0 }";
803 } else {
804 // Just print it out normally.
805 writeOperand(I->getInitializer());
806 }
Chris Lattner940b08d2003-06-06 07:10:24 +0000807 }
Chris Lattnere8e035b2002-10-16 20:08:47 +0000808 Out << ";\n";
Chris Lattnera4d4a852002-08-19 21:48:40 +0000809 }
Chris Lattnera4d4a852002-08-19 21:48:40 +0000810 }
811
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000812 if (!M.empty())
Chris Lattnera4d4a852002-08-19 21:48:40 +0000813 Out << "\n\n/* Function Bodies */\n";
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000814 return false;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000815}
816
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000817
Chris Lattner9860e772003-10-12 04:36:29 +0000818/// Output all floating point constants that cannot be printed accurately...
Chris Lattner68758072004-05-09 06:20:51 +0000819void CWriter::printFloatingPointConstants(Function &F) {
Chris Lattner9860e772003-10-12 04:36:29 +0000820 union {
821 double D;
Chris Lattnerc2421432004-05-09 04:30:20 +0000822 uint64_t U;
Chris Lattner9860e772003-10-12 04:36:29 +0000823 } DBLUnion;
824
825 union {
826 float F;
827 unsigned U;
828 } FLTUnion;
829
830 // Scan the module for floating point constants. If any FP constant is used
831 // in the function, we want to redirect it here so that we do not depend on
832 // the precision of the printed form, unless the printed form preserves
833 // precision.
834 //
Chris Lattner68758072004-05-09 06:20:51 +0000835 static unsigned FPCounter = 0;
836 for (constant_iterator I = constant_begin(&F), E = constant_end(&F);
837 I != E; ++I)
838 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(*I))
839 if (!isFPCSafeToPrint(FPC) && // Do not put in FPConstantMap if safe.
840 !FPConstantMap.count(FPC)) {
841 double Val = FPC->getValue();
842
843 FPConstantMap[FPC] = FPCounter; // Number the FP constants
844
845 if (FPC->getType() == Type::DoubleTy) {
846 DBLUnion.D = Val;
847 Out << "static const ConstantDoubleTy FPConstant" << FPCounter++
848 << " = 0x" << std::hex << DBLUnion.U << std::dec
849 << "ULL; /* " << Val << " */\n";
850 } else if (FPC->getType() == Type::FloatTy) {
851 FLTUnion.F = Val;
852 Out << "static const ConstantFloatTy FPConstant" << FPCounter++
853 << " = 0x" << std::hex << FLTUnion.U << std::dec
854 << "U; /* " << Val << " */\n";
855 } else
856 assert(0 && "Unknown float type!");
857 }
Chris Lattner9860e772003-10-12 04:36:29 +0000858
859 Out << "\n";
Chris Lattner68758072004-05-09 06:20:51 +0000860}
Chris Lattner9860e772003-10-12 04:36:29 +0000861
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000862
Chris Lattner2db41cd2002-09-20 15:12:13 +0000863/// printSymbolTable - Run through symbol table looking for type names. If a
864/// type name is found, emit it's declaration...
Chris Lattnera4c047e2002-09-20 14:56:54 +0000865///
Chris Lattner68758072004-05-09 06:20:51 +0000866void CWriter::printModuleTypes(const SymbolTable &ST) {
Chris Lattner2db41cd2002-09-20 15:12:13 +0000867 // If there are no type names, exit early.
868 if (ST.find(Type::TypeTy) == ST.end())
869 return;
870
871 // We are only interested in the type plane of the symbol table...
872 SymbolTable::type_const_iterator I = ST.type_begin(Type::TypeTy);
873 SymbolTable::type_const_iterator End = ST.type_end(Type::TypeTy);
874
Chris Lattner58d04d42002-09-20 15:18:30 +0000875 // Print out forward declarations for structure types before anything else!
876 Out << "/* Structure forward decls */\n";
877 for (; I != End; ++I)
Chris Lattner68758072004-05-09 06:20:51 +0000878 if (const Type *STy = dyn_cast<StructType>(I->second)) {
879 std::string Name = "struct l_" + Mangler::makeNameProper(I->first);
880 Out << Name << ";\n";
881 TypeNames.insert(std::make_pair(STy, Name));
882 }
Chris Lattner58d04d42002-09-20 15:18:30 +0000883
884 Out << "\n";
885
886 // Now we can print out typedefs...
887 Out << "/* Typedefs */\n";
Chris Lattner68758072004-05-09 06:20:51 +0000888 for (I = ST.type_begin(Type::TypeTy); I != End; ++I) {
889 const Type *Ty = cast<Type>(I->second);
890 std::string Name = "l_" + Mangler::makeNameProper(I->first);
891 Out << "typedef ";
892 printType(Out, Ty, Name);
893 Out << ";\n";
894 }
Chris Lattnerfa395ec2003-11-02 01:29:27 +0000895
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000896 Out << "\n";
897
Chris Lattner2c601a72002-09-20 15:05:40 +0000898 // Keep track of which structures have been printed so far...
899 std::set<const StructType *> StructPrinted;
900
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000901 // Loop over all structures then push them into the stack so they are
902 // printed in the correct order.
Chris Lattner2db41cd2002-09-20 15:12:13 +0000903 //
Chris Lattner58d04d42002-09-20 15:18:30 +0000904 Out << "/* Structure contents */\n";
Chris Lattner2db41cd2002-09-20 15:12:13 +0000905 for (I = ST.type_begin(Type::TypeTy); I != End; ++I)
906 if (const StructType *STy = dyn_cast<StructType>(I->second))
Chris Lattnerfa395ec2003-11-02 01:29:27 +0000907 // Only print out used types!
Chris Lattner68758072004-05-09 06:20:51 +0000908 printContainedStructs(STy, StructPrinted);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000909}
910
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000911// Push the struct onto the stack and recursively push all structs
912// this one depends on.
Chris Lattner2c601a72002-09-20 15:05:40 +0000913void CWriter::printContainedStructs(const Type *Ty,
914 std::set<const StructType*> &StructPrinted){
Misha Brukmanac25de32003-07-09 17:33:50 +0000915 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000916 //Check to see if we have already printed this struct
Chris Lattner2c601a72002-09-20 15:05:40 +0000917 if (StructPrinted.count(STy) == 0) {
918 // Print all contained types first...
Chris Lattnerd21cd802004-02-09 04:37:31 +0000919 for (StructType::element_iterator I = STy->element_begin(),
920 E = STy->element_end(); I != E; ++I) {
Chris Lattner2c601a72002-09-20 15:05:40 +0000921 const Type *Ty1 = I->get();
Chris Lattnera4c047e2002-09-20 14:56:54 +0000922 if (isa<StructType>(Ty1) || isa<ArrayType>(Ty1))
Chris Lattnerab2b3282003-05-29 15:12:27 +0000923 printContainedStructs(*I, StructPrinted);
Chris Lattnera4c047e2002-09-20 14:56:54 +0000924 }
925
Chris Lattner2c601a72002-09-20 15:05:40 +0000926 //Print structure type out..
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000927 StructPrinted.insert(STy);
Chris Lattneredd8ce12003-05-03 03:14:35 +0000928 std::string Name = TypeNames[STy];
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000929 printType(Out, STy, Name, true);
Chris Lattner58d04d42002-09-20 15:18:30 +0000930 Out << ";\n\n";
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000931 }
Chris Lattnera4c047e2002-09-20 14:56:54 +0000932
Chris Lattner2c601a72002-09-20 15:05:40 +0000933 // If it is an array, check contained types and continue
934 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)){
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000935 const Type *Ty1 = ATy->getElementType();
936 if (isa<StructType>(Ty1) || isa<ArrayType>(Ty1))
Chris Lattner2c601a72002-09-20 15:05:40 +0000937 printContainedStructs(Ty1, StructPrinted);
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +0000938 }
939}
940
Chris Lattner3ef6dc72002-05-09 14:40:11 +0000941
Chris Lattner4cda8352002-08-20 16:55:48 +0000942void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
Joel Stanley54f60322003-06-25 04:52:09 +0000943 if (F->hasInternalLinkage()) Out << "static ";
Joel Stanley54f60322003-06-25 04:52:09 +0000944
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000945 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000946 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000947
Joel Stanley54f60322003-06-25 04:52:09 +0000948 std::stringstream FunctionInnards;
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000949
950 // Print out the name...
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000951 FunctionInnards << Mang->getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000952
Chris Lattner16c7bb22002-05-09 02:28:59 +0000953 if (!F->isExternal()) {
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000954 if (!F->aempty()) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000955 std::string ArgName;
Chris Lattner4cda8352002-08-20 16:55:48 +0000956 if (F->abegin()->hasName() || !Prototype)
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000957 ArgName = Mang->getValueName(F->abegin());
Joel Stanley54f60322003-06-25 04:52:09 +0000958 printType(FunctionInnards, F->afront().getType(), ArgName);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000959 for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
960 I != E; ++I) {
Joel Stanley54f60322003-06-25 04:52:09 +0000961 FunctionInnards << ", ";
Chris Lattner4cda8352002-08-20 16:55:48 +0000962 if (I->hasName() || !Prototype)
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000963 ArgName = Mang->getValueName(I);
Chris Lattner4cda8352002-08-20 16:55:48 +0000964 else
965 ArgName = "";
Joel Stanley54f60322003-06-25 04:52:09 +0000966 printType(FunctionInnards, I->getType(), ArgName);
Chris Lattnerdeed7a52002-05-09 15:18:52 +0000967 }
968 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000969 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000970 // Loop over the arguments, printing them...
Chris Lattnerd5d89962004-02-09 04:14:01 +0000971 for (FunctionType::param_iterator I = FT->param_begin(),
972 E = FT->param_end(); I != E; ++I) {
973 if (I != FT->param_begin()) FunctionInnards << ", ";
Joel Stanley54f60322003-06-25 04:52:09 +0000974 printType(FunctionInnards, *I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000975 }
976 }
977
Chris Lattner1f8c4a12002-09-20 22:32:30 +0000978 // Finish printing arguments... if this is a vararg function, print the ...,
979 // unless there are no known types, in which case, we just emit ().
980 //
Chris Lattnerd5d89962004-02-09 04:14:01 +0000981 if (FT->isVarArg() && FT->getNumParams()) {
982 if (FT->getNumParams()) FunctionInnards << ", ";
Joel Stanley54f60322003-06-25 04:52:09 +0000983 FunctionInnards << "..."; // Output varargs portion of signature!
Chris Lattnerd5d89962004-02-09 04:14:01 +0000984 } else if (!FT->isVarArg() && FT->getNumParams() == 0) {
Chris Lattner4f7e1732003-11-26 00:09:17 +0000985 FunctionInnards << "void"; // ret() -> ret(void) in C.
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000986 }
Joel Stanley54f60322003-06-25 04:52:09 +0000987 FunctionInnards << ")";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000988 // Print out the return type and the entire signature for that matter
Joel Stanley54f60322003-06-25 04:52:09 +0000989 printType(Out, F->getReturnType(), FunctionInnards.str());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000990}
991
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000992void CWriter::printFunction(Function &F) {
993 printFunctionSignature(&F, false);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000994 Out << " {\n";
995
Chris Lattner497e19a2002-05-09 20:39:03 +0000996 // print local variable information for the function
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000997 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I)
Chris Lattner6ffe5512004-04-27 15:13:33 +0000998 if (const AllocaInst *AI = isDirectAlloca(&*I)) {
Chris Lattnera8ab89e2003-06-17 04:39:14 +0000999 Out << " ";
Brian Gaeked9fb37a2003-07-24 20:20:44 +00001000 printType(Out, AI->getAllocatedType(), Mang->getValueName(AI));
Chris Lattnera8ab89e2003-06-17 04:39:14 +00001001 Out << "; /* Address exposed local */\n";
Chris Lattner6ffe5512004-04-27 15:13:33 +00001002 } else if (I->getType() != Type::VoidTy && !isInlinableInst(*I)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +00001003 Out << " ";
Chris Lattner6ffe5512004-04-27 15:13:33 +00001004 printType(Out, I->getType(), Mang->getValueName(&*I));
Chris Lattner16c7bb22002-05-09 02:28:59 +00001005 Out << ";\n";
Chris Lattnera8ab89e2003-06-17 04:39:14 +00001006
Chris Lattner84e66652003-05-03 07:11:00 +00001007 if (isa<PHINode>(*I)) { // Print out PHI node temporaries as well...
1008 Out << " ";
Chris Lattner6ffe5512004-04-27 15:13:33 +00001009 printType(Out, I->getType(),
1010 Mang->getValueName(&*I)+"__PHI_TEMPORARY");
Chris Lattner84e66652003-05-03 07:11:00 +00001011 Out << ";\n";
1012 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001013 }
Chris Lattnerd1cf1b42002-09-20 23:26:33 +00001014
1015 Out << "\n";
1016
Chris Lattner16c7bb22002-05-09 02:28:59 +00001017 // print the basic blocks
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001018 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattnercb3ad012004-05-09 20:41:32 +00001019 if (Loop *L = LI->getLoopFor(BB)) {
1020 if (L->getHeader() == BB && L->getParentLoop() == 0)
1021 printLoop(L);
1022 } else {
1023 printBasicBlock(BB);
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001024 }
Chris Lattner8c3c4bf2002-05-09 15:49:41 +00001025 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001026
Chris Lattner8c3c4bf2002-05-09 15:49:41 +00001027 Out << "}\n\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001028}
1029
Chris Lattnercb3ad012004-05-09 20:41:32 +00001030void CWriter::printLoop(Loop *L) {
1031 Out << " do { /* Syntactic loop '" << L->getHeader()->getName()
1032 << "' to make GCC happy */\n";
1033 for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i) {
1034 BasicBlock *BB = L->getBlocks()[i];
1035 Loop *BBLoop = LI->getLoopFor(BB);
1036 if (BBLoop == L)
1037 printBasicBlock(BB);
1038 else if (BB == BBLoop->getHeader() && BBLoop->getParentLoop() == L)
1039 printLoop(BBLoop);
1040 }
1041 Out << " } while (1); /* end of syntactic loop '"
1042 << L->getHeader()->getName() << "' */\n";
1043}
1044
1045void CWriter::printBasicBlock(BasicBlock *BB) {
1046
1047 // Don't print the label for the basic block if there are no uses, or if
1048 // the only terminator use is the predecessor basic block's terminator.
1049 // We have to scan the use list because PHI nodes use basic blocks too but
1050 // do not require a label to be generated.
1051 //
1052 bool NeedsLabel = false;
1053 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
1054 if (isGotoCodeNecessary(*PI, BB)) {
1055 NeedsLabel = true;
1056 break;
1057 }
1058
1059 if (NeedsLabel) Out << Mang->getValueName(BB) << ":\n";
1060
1061 // Output all of the instructions in the basic block...
1062 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E;
1063 ++II) {
1064 if (!isInlinableInst(*II) && !isDirectAlloca(II)) {
1065 if (II->getType() != Type::VoidTy)
1066 outputLValue(II);
1067 else
1068 Out << " ";
1069 visit(*II);
1070 Out << ";\n";
1071 }
1072 }
1073
1074 // Don't emit prefix or suffix for the terminator...
1075 visit(*BB->getTerminator());
1076}
1077
1078
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001079// Specific Instruction type classes... note that all of the casts are
Misha Brukman5560c9d2003-08-18 14:43:39 +00001080// necessary because we use the instruction classes as opaque types...
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001081//
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001082void CWriter::visitReturnInst(ReturnInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001083 // Don't output a void return if this is the last basic block in the function
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001084 if (I.getNumOperands() == 0 &&
1085 &*--I.getParent()->getParent()->end() == I.getParent() &&
1086 !I.getParent()->size() == 1) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001087 return;
Chris Lattner963b70b2002-05-21 18:05:19 +00001088 }
Chris Lattner44408262002-05-09 03:50:42 +00001089
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001090 Out << " return";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001091 if (I.getNumOperands()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001092 Out << " ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001093 writeOperand(I.getOperand(0));
Chris Lattner16c7bb22002-05-09 02:28:59 +00001094 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001095 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001096}
1097
Chris Lattnera9f5e052003-04-22 20:19:52 +00001098void CWriter::visitSwitchInst(SwitchInst &SI) {
Chris Lattnera05e0ec2004-05-09 03:42:48 +00001099 printPHICopiesForSuccessors(SI.getParent(), 0);
1100
Chris Lattnera9f5e052003-04-22 20:19:52 +00001101 Out << " switch (";
1102 writeOperand(SI.getOperand(0));
Chris Lattnerf1acd962003-04-23 19:15:13 +00001103 Out << ") {\n default:\n";
1104 printBranchToBlock(SI.getParent(), SI.getDefaultDest(), 2);
Chris Lattnera9f5e052003-04-22 20:19:52 +00001105 Out << ";\n";
1106 for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) {
1107 Out << " case ";
1108 writeOperand(SI.getOperand(i));
1109 Out << ":\n";
1110 BasicBlock *Succ = cast<BasicBlock>(SI.getOperand(i+1));
1111 printBranchToBlock(SI.getParent(), Succ, 2);
1112 if (Succ == SI.getParent()->getNext())
1113 Out << " break;\n";
1114 }
1115 Out << " }\n";
1116}
1117
Chris Lattnercb3ad012004-05-09 20:41:32 +00001118bool CWriter::isGotoCodeNecessary(BasicBlock *From, BasicBlock *To) {
1119 /// FIXME: This should be reenabled, but loop reordering safe!!
1120 return true;
1121
1122 if (From->getNext() != To) // Not the direct successor, we need a goto
1123 return true;
1124
1125 //isa<SwitchInst>(From->getTerminator())
Chris Lattner9bda5f52003-06-28 17:53:05 +00001126
Chris Lattnera9f5e052003-04-22 20:19:52 +00001127
Chris Lattnercb3ad012004-05-09 20:41:32 +00001128 if (LI->getLoopFor(From) != LI->getLoopFor(To))
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001129 return true;
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001130 return false;
1131}
1132
Chris Lattnera05e0ec2004-05-09 03:42:48 +00001133void CWriter::printPHICopiesForSuccessors(BasicBlock *CurBlock,
1134 unsigned Indent) {
1135 for (succ_iterator SI = succ_begin(CurBlock), E = succ_end(CurBlock);
1136 SI != E; ++SI)
1137 for (BasicBlock::iterator I = SI->begin();
1138 PHINode *PN = dyn_cast<PHINode>(I); ++I) {
1139 // now we have to do the printing
1140 Out << std::string(Indent, ' ');
1141 Out << " " << Mang->getValueName(I) << "__PHI_TEMPORARY = ";
1142 writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBlock)));
1143 Out << "; /* for PHI node */\n";
1144 }
1145}
1146
1147
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001148void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
Chris Lattner9bda5f52003-06-28 17:53:05 +00001149 unsigned Indent) {
Chris Lattnercb3ad012004-05-09 20:41:32 +00001150 if (isGotoCodeNecessary(CurBB, Succ)) {
Chris Lattneredd8ce12003-05-03 03:14:35 +00001151 Out << std::string(Indent, ' ') << " goto ";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001152 writeOperand(Succ);
1153 Out << ";\n";
1154 }
1155}
1156
Misha Brukman37f92e22003-09-11 22:34:13 +00001157// Branch instruction printing - Avoid printing out a branch to a basic block
1158// that immediately succeeds the current one.
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001159//
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001160void CWriter::visitBranchInst(BranchInst &I) {
Chris Lattnera05e0ec2004-05-09 03:42:48 +00001161 printPHICopiesForSuccessors(I.getParent(), 0);
1162
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001163 if (I.isConditional()) {
Chris Lattner5ef6ffd2003-10-12 08:12:58 +00001164 if (isGotoCodeNecessary(I.getParent(), I.getSuccessor(0))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001165 Out << " if (";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001166 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001167 Out << ") {\n";
1168
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001169 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001170
Chris Lattner5ef6ffd2003-10-12 08:12:58 +00001171 if (isGotoCodeNecessary(I.getParent(), I.getSuccessor(1))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001172 Out << " } else {\n";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001173 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001174 }
1175 } else {
Misha Brukman5560c9d2003-08-18 14:43:39 +00001176 // First goto not necessary, assume second one is...
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001177 Out << " if (!";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001178 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001179 Out << ") {\n";
1180
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001181 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001182 }
1183
1184 Out << " }\n";
1185 } else {
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001186 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001187 }
1188 Out << "\n";
1189}
1190
Chris Lattner84e66652003-05-03 07:11:00 +00001191// PHI nodes get copied into temporary values at the end of predecessor basic
1192// blocks. We now need to copy these temporary values into the REAL value for
1193// the PHI.
1194void CWriter::visitPHINode(PHINode &I) {
1195 writeOperand(&I);
1196 Out << "__PHI_TEMPORARY";
1197}
1198
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001199
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001200void CWriter::visitBinaryOperator(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001201 // binary instructions, shift instructions, setCond instructions.
Chris Lattnerf5612b762003-04-23 19:09:22 +00001202 assert(!isa<PointerType>(I.getType()));
Brian Gaeke031a1122003-06-23 20:00:51 +00001203
1204 // We must cast the results of binary operations which might be promoted.
1205 bool needsCast = false;
1206 if ((I.getType() == Type::UByteTy) || (I.getType() == Type::SByteTy)
Brian Gaekee99f4cf2003-06-25 03:05:33 +00001207 || (I.getType() == Type::UShortTy) || (I.getType() == Type::ShortTy)
1208 || (I.getType() == Type::FloatTy)) {
Brian Gaeke031a1122003-06-23 20:00:51 +00001209 needsCast = true;
1210 Out << "((";
Chris Lattner7635ea42003-11-03 01:01:59 +00001211 printType(Out, I.getType());
Brian Gaeke031a1122003-06-23 20:00:51 +00001212 Out << ")(";
1213 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001214
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001215 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001216
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001217 switch (I.getOpcode()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001218 case Instruction::Add: Out << " + "; break;
1219 case Instruction::Sub: Out << " - "; break;
1220 case Instruction::Mul: Out << "*"; break;
1221 case Instruction::Div: Out << "/"; break;
1222 case Instruction::Rem: Out << "%"; break;
1223 case Instruction::And: Out << " & "; break;
1224 case Instruction::Or: Out << " | "; break;
1225 case Instruction::Xor: Out << " ^ "; break;
1226 case Instruction::SetEQ: Out << " == "; break;
1227 case Instruction::SetNE: Out << " != "; break;
1228 case Instruction::SetLE: Out << " <= "; break;
1229 case Instruction::SetGE: Out << " >= "; break;
1230 case Instruction::SetLT: Out << " < "; break;
1231 case Instruction::SetGT: Out << " > "; break;
1232 case Instruction::Shl : Out << " << "; break;
1233 case Instruction::Shr : Out << " >> "; break;
Chris Lattnere7f65d3b2002-06-30 16:07:20 +00001234 default: std::cerr << "Invalid operator type!" << I; abort();
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001235 }
1236
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001237 writeOperand(I.getOperand(1));
Brian Gaeke031a1122003-06-23 20:00:51 +00001238
1239 if (needsCast) {
1240 Out << "))";
1241 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001242}
1243
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001244void CWriter::visitCastInst(CastInst &I) {
Chris Lattnerd13bd222003-06-01 03:36:51 +00001245 if (I.getType() == Type::BoolTy) {
1246 Out << "(";
1247 writeOperand(I.getOperand(0));
1248 Out << " != 0)";
1249 return;
1250 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001251 Out << "(";
Chris Lattner7635ea42003-11-03 01:01:59 +00001252 printType(Out, I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001253 Out << ")";
Chris Lattnerf5612b762003-04-23 19:09:22 +00001254 if (isa<PointerType>(I.getType())&&I.getOperand(0)->getType()->isIntegral() ||
1255 isa<PointerType>(I.getOperand(0)->getType())&&I.getType()->isIntegral()) {
1256 // Avoid "cast to pointer from integer of different size" warnings
1257 Out << "(long)";
1258 }
Chris Lattnerd13bd222003-06-01 03:36:51 +00001259
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001260 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001261}
1262
Chris Lattner9f24a072004-03-12 05:52:14 +00001263void CWriter::visitSelectInst(SelectInst &I) {
1264 Out << "((";
1265 writeOperand(I.getCondition());
1266 Out << ") ? (";
1267 writeOperand(I.getTrueValue());
1268 Out << ") : (";
1269 writeOperand(I.getFalseValue());
1270 Out << "))";
1271}
1272
1273
Chris Lattnerc2421432004-05-09 04:30:20 +00001274void CWriter::lowerIntrinsics(Function &F) {
1275 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1276 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1277 if (CallInst *CI = dyn_cast<CallInst>(I++))
1278 if (Function *F = CI->getCalledFunction())
1279 switch (F->getIntrinsicID()) {
1280 case Intrinsic::not_intrinsic:
1281 case Intrinsic::vastart:
1282 case Intrinsic::vacopy:
1283 case Intrinsic::vaend:
1284 case Intrinsic::returnaddress:
1285 case Intrinsic::frameaddress:
1286 case Intrinsic::setjmp:
1287 case Intrinsic::longjmp:
1288 // We directly implement these intrinsics
1289 break;
1290 default:
1291 // All other intrinsic calls we must lower.
1292 Instruction *Before = CI->getPrev();
1293 IL.LowerIntrinsicCall(CI);
1294 if (Before) { // Move iterator to instruction after call
1295 I = Before; ++I;
1296 } else {
1297 I = BB->begin();
Chris Lattner4ef51372004-02-14 00:31:10 +00001298 }
Chris Lattnerc2421432004-05-09 04:30:20 +00001299 }
Chris Lattner4ef51372004-02-14 00:31:10 +00001300}
1301
1302
1303
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001304void CWriter::visitCallInst(CallInst &I) {
Chris Lattner18ac3c82003-05-08 18:41:45 +00001305 // Handle intrinsic function calls first...
1306 if (Function *F = I.getCalledFunction())
Brian Gaeked0fde302003-11-11 22:41:34 +00001307 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
Chris Lattner18ac3c82003-05-08 18:41:45 +00001308 switch (ID) {
Chris Lattner4ef51372004-02-14 00:31:10 +00001309 default: assert(0 && "Unknown LLVM intrinsic!");
Chris Lattner317201d2004-03-13 00:24:00 +00001310 case Intrinsic::vastart:
Chris Lattner4d45bd02003-10-18 05:57:43 +00001311 Out << "0; ";
1312
1313 Out << "va_start(*(va_list*)&" << Mang->getValueName(&I) << ", ";
Chris Lattner18ac3c82003-05-08 18:41:45 +00001314 // Output the last argument to the enclosing function...
Chris Lattner4065ef92003-10-23 18:39:22 +00001315 if (I.getParent()->getParent()->aempty()) {
1316 std::cerr << "The C backend does not currently support zero "
1317 << "argument varargs functions, such as '"
1318 << I.getParent()->getParent()->getName() << "'!\n";
1319 abort();
1320 }
Chris Lattner18ac3c82003-05-08 18:41:45 +00001321 writeOperand(&I.getParent()->getParent()->aback());
1322 Out << ")";
1323 return;
Chris Lattner317201d2004-03-13 00:24:00 +00001324 case Intrinsic::vaend:
Chris Lattner4d45bd02003-10-18 05:57:43 +00001325 Out << "va_end(*(va_list*)&";
Chris Lattner18ac3c82003-05-08 18:41:45 +00001326 writeOperand(I.getOperand(1));
1327 Out << ")";
1328 return;
Chris Lattner317201d2004-03-13 00:24:00 +00001329 case Intrinsic::vacopy:
Chris Lattner4d45bd02003-10-18 05:57:43 +00001330 Out << "0;";
1331 Out << "va_copy(*(va_list*)&" << Mang->getValueName(&I) << ", ";
1332 Out << "*(va_list*)&";
Chris Lattner18ac3c82003-05-08 18:41:45 +00001333 writeOperand(I.getOperand(1));
Chris Lattner18ac3c82003-05-08 18:41:45 +00001334 Out << ")";
1335 return;
Chris Lattnerfe1554a2004-02-14 02:55:36 +00001336 case Intrinsic::returnaddress:
1337 Out << "__builtin_return_address(";
1338 writeOperand(I.getOperand(1));
1339 Out << ")";
1340 return;
1341 case Intrinsic::frameaddress:
1342 Out << "__builtin_frame_address(";
1343 writeOperand(I.getOperand(1));
1344 Out << ")";
1345 return;
Chris Lattnere42cde22004-02-15 22:51:47 +00001346 case Intrinsic::setjmp:
1347 Out << "setjmp(*(jmp_buf*)";
1348 writeOperand(I.getOperand(1));
1349 Out << ")";
1350 return;
1351 case Intrinsic::longjmp:
1352 Out << "longjmp(*(jmp_buf*)";
1353 writeOperand(I.getOperand(1));
1354 Out << ", ";
1355 writeOperand(I.getOperand(2));
1356 Out << ")";
1357 return;
Chris Lattner18ac3c82003-05-08 18:41:45 +00001358 }
1359 }
Chris Lattner9bda5f52003-06-28 17:53:05 +00001360 visitCallSite(&I);
1361}
Chris Lattner18ac3c82003-05-08 18:41:45 +00001362
Chris Lattner9bda5f52003-06-28 17:53:05 +00001363void CWriter::visitCallSite(CallSite CS) {
1364 const PointerType *PTy = cast<PointerType>(CS.getCalledValue()->getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001365 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1366 const Type *RetTy = FTy->getReturnType();
1367
Chris Lattner9bda5f52003-06-28 17:53:05 +00001368 writeOperand(CS.getCalledValue());
Chris Lattnerfabc8802002-08-26 20:50:09 +00001369 Out << "(";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001370
Chris Lattner9bda5f52003-06-28 17:53:05 +00001371 if (CS.arg_begin() != CS.arg_end()) {
1372 CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
1373 writeOperand(*AI);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001374
Chris Lattner9bda5f52003-06-28 17:53:05 +00001375 for (++AI; AI != AE; ++AI) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001376 Out << ", ";
Chris Lattner9bda5f52003-06-28 17:53:05 +00001377 writeOperand(*AI);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001378 }
1379 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001380 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001381}
1382
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001383void CWriter::visitMallocInst(MallocInst &I) {
Chris Lattnerf31182a2004-02-13 23:18:48 +00001384 assert(0 && "lowerallocations pass didn't work!");
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001385}
1386
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001387void CWriter::visitAllocaInst(AllocaInst &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001388 Out << "(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001389 printType(Out, I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001390 Out << ") alloca(sizeof(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001391 printType(Out, I.getType()->getElementType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001392 Out << ")";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001393 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001394 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001395 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001396 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001397 Out << ")";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001398}
1399
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001400void CWriter::visitFreeInst(FreeInst &I) {
Chris Lattnerf31182a2004-02-13 23:18:48 +00001401 assert(0 && "lowerallocations pass didn't work!");
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001402}
1403
Chris Lattner23e90702003-11-25 20:49:55 +00001404void CWriter::printIndexingExpression(Value *Ptr, gep_type_iterator I,
1405 gep_type_iterator E) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001406 bool HasImplicitAddress = false;
1407 // If accessing a global value with no indexing, avoid *(&GV) syndrome
1408 if (GlobalValue *V = dyn_cast<GlobalValue>(Ptr)) {
1409 HasImplicitAddress = true;
1410 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Ptr)) {
1411 HasImplicitAddress = true;
1412 Ptr = CPR->getValue(); // Get to the global...
Chris Lattnera8ab89e2003-06-17 04:39:14 +00001413 } else if (isDirectAlloca(Ptr)) {
1414 HasImplicitAddress = true;
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001415 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001416
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001417 if (I == E) {
1418 if (!HasImplicitAddress)
1419 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001420
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001421 writeOperandInternal(Ptr);
1422 return;
1423 }
1424
Chris Lattner23e90702003-11-25 20:49:55 +00001425 const Constant *CI = dyn_cast<Constant>(I.getOperand());
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001426 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
1427 Out << "(&";
1428
1429 writeOperandInternal(Ptr);
1430
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001431 if (HasImplicitAddress && (!CI || !CI->isNullValue())) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001432 Out << ")";
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001433 HasImplicitAddress = false; // HIA is only true if we haven't addressed yet
1434 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001435
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001436 assert(!HasImplicitAddress || (CI && CI->isNullValue()) &&
1437 "Can only have implicit address with direct accessing");
1438
1439 if (HasImplicitAddress) {
1440 ++I;
Chris Lattner23e90702003-11-25 20:49:55 +00001441 } else if (CI && CI->isNullValue()) {
1442 gep_type_iterator TmpI = I; ++TmpI;
1443
Nick Hildenbrandte548f002002-09-25 20:29:26 +00001444 // Print out the -> operator if possible...
Chris Lattner23e90702003-11-25 20:49:55 +00001445 if (TmpI != E && isa<StructType>(*TmpI)) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00001446 Out << (HasImplicitAddress ? "." : "->");
Chris Lattnerbc771c52003-11-25 23:44:40 +00001447 Out << "field" << cast<ConstantUInt>(TmpI.getOperand())->getValue();
Chris Lattner23e90702003-11-25 20:49:55 +00001448 I = ++TmpI;
1449 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001450 }
Chris Lattner5dfe7672002-08-22 22:48:55 +00001451
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001452 for (; I != E; ++I)
Chris Lattner23e90702003-11-25 20:49:55 +00001453 if (isa<StructType>(*I)) {
1454 Out << ".field" << cast<ConstantUInt>(I.getOperand())->getValue();
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001455 } else {
Chris Lattner23e90702003-11-25 20:49:55 +00001456 Out << "[";
1457 writeOperand(I.getOperand());
1458 Out << "]";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001459 }
1460}
1461
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001462void CWriter::visitLoadInst(LoadInst &I) {
Nick Hildenbrandtca626922002-10-02 21:14:33 +00001463 Out << "*";
Chris Lattner5dfe7672002-08-22 22:48:55 +00001464 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001465}
1466
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001467void CWriter::visitStoreInst(StoreInst &I) {
Nick Hildenbrandtca626922002-10-02 21:14:33 +00001468 Out << "*";
Chris Lattner5dfe7672002-08-22 22:48:55 +00001469 writeOperand(I.getPointerOperand());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001470 Out << " = ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001471 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001472}
1473
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001474void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
Nick Hildenbrandtca626922002-10-02 21:14:33 +00001475 Out << "&";
Chris Lattner23e90702003-11-25 20:49:55 +00001476 printIndexingExpression(I.getPointerOperand(), gep_type_begin(I),
1477 gep_type_end(I));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001478}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001479
Chris Lattner4d45bd02003-10-18 05:57:43 +00001480void CWriter::visitVANextInst(VANextInst &I) {
1481 Out << Mang->getValueName(I.getOperand(0));
1482 Out << "; va_arg(*(va_list*)&" << Mang->getValueName(&I) << ", ";
Chris Lattner7635ea42003-11-03 01:01:59 +00001483 printType(Out, I.getArgType());
Chris Lattner18ac3c82003-05-08 18:41:45 +00001484 Out << ")";
1485}
1486
Chris Lattner4d45bd02003-10-18 05:57:43 +00001487void CWriter::visitVAArgInst(VAArgInst &I) {
1488 Out << "0;\n";
1489 Out << "{ va_list Tmp; va_copy(Tmp, *(va_list*)&";
1490 writeOperand(I.getOperand(0));
1491 Out << ");\n " << Mang->getValueName(&I) << " = va_arg(Tmp, ";
Chris Lattner7635ea42003-11-03 01:01:59 +00001492 printType(Out, I.getType());
Chris Lattner4d45bd02003-10-18 05:57:43 +00001493 Out << ");\n va_end(Tmp); }";
1494}
1495
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001496//===----------------------------------------------------------------------===//
1497// External Interface declaration
1498//===----------------------------------------------------------------------===//
1499
Chris Lattnerf31182a2004-02-13 23:18:48 +00001500bool CTargetMachine::addPassesToEmitAssembly(PassManager &PM, std::ostream &o) {
1501 PM.add(createLowerAllocationsPass());
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001502 PM.add(createLowerInvokePass());
Chris Lattner68758072004-05-09 06:20:51 +00001503 PM.add(new CBackendNameAllUsedStructs());
Chris Lattner4ef51372004-02-14 00:31:10 +00001504 PM.add(new CWriter(o, getIntrinsicLowering()));
Chris Lattnerf31182a2004-02-13 23:18:48 +00001505 return false;
1506}
1507
1508TargetMachine *llvm::allocateCTargetMachine(const Module &M,
1509 IntrinsicLowering *IL) {
1510 return new CTargetMachine(M, IL);
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001511}