blob: eb7b4e3813d97f3097cb981acfaf128d865090ba [file] [log] [blame]
Chris Lattner3af3ba82002-05-09 21:31:18 +00001//===-- Writer.cpp - Library for converting LLVM code to C ----------------===//
Misha Brukmand6a29a52005-04-20 16:05:03 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmand6a29a52005-04-20 16:05:03 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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"
Chris Lattner0c54d892006-05-23 23:39:48 +000016#include "llvm/CallingConv.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"
Jim Laskey580418e2006-03-23 18:08:29 +000025#include "llvm/IntrinsicInst.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 Lattner30483732004-06-20 07:49:54 +000029#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattner94f4f8e2004-02-13 23:00:29 +000030#include "llvm/Transforms/Scalar.h"
Chris Lattnerd36c9702004-07-11 02:48:49 +000031#include "llvm/Target/TargetMachineRegistry.h"
Chris Lattner23e90702003-11-25 20:49:55 +000032#include "llvm/Support/CallSite.h"
Chris Lattnera05e0ec2004-05-09 03:42:48 +000033#include "llvm/Support/CFG.h"
Chris Lattner23e90702003-11-25 20:49:55 +000034#include "llvm/Support/GetElementPtrTypeIterator.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000035#include "llvm/Support/InstVisitor.h"
Brian Gaeke49a178b2003-07-25 20:21:06 +000036#include "llvm/Support/Mangler.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000037#include "llvm/Support/MathExtras.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000038#include "llvm/ADT/StringExtras.h"
Chris Lattner67c2d182005-03-18 16:12:37 +000039#include "llvm/ADT/STLExtras.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000040#include "llvm/Support/MathExtras.h"
41#include "llvm/Config/config.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000042#include <algorithm>
Reid Spencer954da372004-07-04 12:19:56 +000043#include <iostream>
Duraid Madinad2dec7d2005-12-27 10:40:34 +000044#include <ios>
Nick Hildenbrandt98502372002-11-18 20:55:50 +000045#include <sstream>
Chris Lattner897bf0d2004-02-13 06:18:21 +000046using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000047
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000048namespace {
Chris Lattnerd36c9702004-07-11 02:48:49 +000049 // Register the target.
Chris Lattner71d24aa2004-07-11 03:27:42 +000050 RegisterTarget<CTargetMachine> X("c", " C backend");
Chris Lattnerd36c9702004-07-11 02:48:49 +000051
Chris Lattnerf9a05322006-02-13 22:22:42 +000052 /// CBackendNameAllUsedStructsAndMergeFunctions - This pass inserts names for
53 /// any unnamed structure types that are used by the program, and merges
54 /// external functions with the same name.
Chris Lattner68758072004-05-09 06:20:51 +000055 ///
Chris Lattnerf9a05322006-02-13 22:22:42 +000056 class CBackendNameAllUsedStructsAndMergeFunctions : public ModulePass {
Chris Lattner68758072004-05-09 06:20:51 +000057 void getAnalysisUsage(AnalysisUsage &AU) const {
58 AU.addRequired<FindUsedTypes>();
59 }
60
61 virtual const char *getPassName() const {
62 return "C backend type canonicalizer";
63 }
64
Chris Lattnerb12914b2004-09-20 04:48:05 +000065 virtual bool runOnModule(Module &M);
Chris Lattner68758072004-05-09 06:20:51 +000066 };
Misha Brukmand6a29a52005-04-20 16:05:03 +000067
Chris Lattner68758072004-05-09 06:20:51 +000068 /// CWriter - This class is the main chunk of code that converts an LLVM
69 /// module to a C translation unit.
70 class CWriter : public FunctionPass, public InstVisitor<CWriter> {
Misha Brukmand6a29a52005-04-20 16:05:03 +000071 std::ostream &Out;
Chris Lattnerbc641b92006-03-23 05:43:16 +000072 DefaultIntrinsicLowering IL;
Brian Gaeked9fb37a2003-07-24 20:20:44 +000073 Mangler *Mang;
Chris Lattnercb3ad012004-05-09 20:41:32 +000074 LoopInfo *LI;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000075 const Module *TheModule;
Chris Lattneredd8ce12003-05-03 03:14:35 +000076 std::map<const Type *, std::string> TypeNames;
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +000077
Chris Lattneredd8ce12003-05-03 03:14:35 +000078 std::map<const ConstantFP *, unsigned> FPConstantMap;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000079 public:
Chris Lattnerbc641b92006-03-23 05:43:16 +000080 CWriter(std::ostream &o) : Out(o) {}
Chris Lattner0e9f93e2002-08-31 00:29:16 +000081
Chris Lattner94f4f8e2004-02-13 23:00:29 +000082 virtual const char *getPassName() const { return "C backend"; }
Chris Lattner0e9f93e2002-08-31 00:29:16 +000083
Chris Lattnercb3ad012004-05-09 20:41:32 +000084 void getAnalysisUsage(AnalysisUsage &AU) const {
85 AU.addRequired<LoopInfo>();
86 AU.setPreservesAll();
87 }
88
Chris Lattner68758072004-05-09 06:20:51 +000089 virtual bool doInitialization(Module &M);
Chris Lattner0e9f93e2002-08-31 00:29:16 +000090
Chris Lattner68758072004-05-09 06:20:51 +000091 bool runOnFunction(Function &F) {
Chris Lattnercb3ad012004-05-09 20:41:32 +000092 LI = &getAnalysis<LoopInfo>();
93
Chris Lattner3150e2d2004-12-05 06:49:44 +000094 // Get rid of intrinsics we can't handle.
95 lowerIntrinsics(F);
96
Chris Lattner68758072004-05-09 06:20:51 +000097 // Output all floating point constants that cannot be printed accurately.
98 printFloatingPointConstants(F);
Chris Lattner3150e2d2004-12-05 06:49:44 +000099
100 // Ensure that no local symbols conflict with global symbols.
101 F.renameLocalSymbols();
102
Chris Lattner68758072004-05-09 06:20:51 +0000103 printFunction(F);
104 FPConstantMap.clear();
105 return false;
106 }
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000107
Chris Lattner68758072004-05-09 06:20:51 +0000108 virtual bool doFinalization(Module &M) {
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000109 // Free memory...
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000110 delete Mang;
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000111 TypeNames.clear();
Chris Lattnercb3ad012004-05-09 20:41:32 +0000112 return false;
Chris Lattner0e9f93e2002-08-31 00:29:16 +0000113 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000114
Chris Lattneredd8ce12003-05-03 03:14:35 +0000115 std::ostream &printType(std::ostream &Out, const Type *Ty,
116 const std::string &VariableName = "",
Chris Lattner7635ea42003-11-03 01:01:59 +0000117 bool IgnoreName = false);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000118
Chris Lattner0c54d892006-05-23 23:39:48 +0000119 void printStructReturnPointerFunctionType(std::ostream &Out,
120 const PointerType *Ty);
121
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000122 void writeOperand(Value *Operand);
123 void writeOperandInternal(Value *Operand);
Reid Spencer1628cec2006-10-26 06:15:43 +0000124 void writeOperandWithCast(Value* Operand, unsigned Opcode);
125 bool writeInstructionCast(const Instruction &I);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000126
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000127 private :
Chris Lattnerc2421432004-05-09 04:30:20 +0000128 void lowerIntrinsics(Function &F);
Chris Lattner4ef51372004-02-14 00:31:10 +0000129
Chris Lattner8c3c4bf2002-05-09 15:49:41 +0000130 void printModule(Module *M);
Chris Lattner68758072004-05-09 06:20:51 +0000131 void printModuleTypes(const SymbolTable &ST);
Chris Lattner2c601a72002-09-20 15:05:40 +0000132 void printContainedStructs(const Type *Ty, std::set<const StructType *> &);
Chris Lattner68758072004-05-09 06:20:51 +0000133 void printFloatingPointConstants(Function &F);
Chris Lattner4cda8352002-08-20 16:55:48 +0000134 void printFunctionSignature(const Function *F, bool Prototype);
135
Chris Lattner94f4f8e2004-02-13 23:00:29 +0000136 void printFunction(Function &);
Chris Lattnercb3ad012004-05-09 20:41:32 +0000137 void printBasicBlock(BasicBlock *BB);
138 void printLoop(Loop *L);
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000139
Chris Lattner6d492922002-08-19 21:32:41 +0000140 void printConstant(Constant *CPV);
Reid Spencer1628cec2006-10-26 06:15:43 +0000141 void printConstantWithCast(Constant *CPV, unsigned Opcode);
142 bool printConstExprCast(const ConstantExpr *CE);
Chris Lattner6d492922002-08-19 21:32:41 +0000143 void printConstantArray(ConstantArray *CPA);
Robert Bocchinob78e8382006-01-20 20:43:57 +0000144 void printConstantPacked(ConstantPacked *CP);
Chris Lattner6d492922002-08-19 21:32:41 +0000145
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000146 // isInlinableInst - Attempt to inline instructions into their uses to build
147 // trees as much as possible. To do this, we have to consistently decide
148 // what is acceptable to inline, so that variable declarations don't get
149 // printed and an extra copy of the expr is not emitted.
150 //
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000151 static bool isInlinableInst(const Instruction &I) {
Chris Lattner433e25a2004-05-20 20:25:50 +0000152 // Always inline setcc instructions, even if they are shared by multiple
153 // expressions. GCC generates horrible code if we don't.
154 if (isa<SetCondInst>(I)) return true;
155
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000156 // Must be an expression, must be used exactly once. If it is dead, we
157 // emit it inline where it would go.
Chris Lattnerfd059242003-10-15 16:48:29 +0000158 if (I.getType() == Type::VoidTy || !I.hasOneUse() ||
Misha Brukmand6a29a52005-04-20 16:05:03 +0000159 isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I) ||
Andrew Lenharth558bc882005-06-18 18:34:52 +0000160 isa<LoadInst>(I) || isa<VAArgInst>(I))
Chris Lattnerd4e8d312003-07-23 20:45:31 +0000161 // Don't inline a load across a store or other bad things!
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000162 return false;
163
164 // Only inline instruction it it's use is in the same BB as the inst.
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000165 return I.getParent() == cast<Instruction>(I.use_back())->getParent();
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000166 }
167
Chris Lattnera8ab89e2003-06-17 04:39:14 +0000168 // isDirectAlloca - Define fixed sized allocas in the entry block as direct
169 // variables which are accessed with the & operator. This causes GCC to
170 // generate significantly better code than to emit alloca calls directly.
171 //
172 static const AllocaInst *isDirectAlloca(const Value *V) {
173 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
174 if (!AI) return false;
175 if (AI->isArrayAllocation())
176 return 0; // FIXME: we can also inline fixed size array allocas!
Chris Lattner02a3be02003-09-20 14:39:18 +0000177 if (AI->getParent() != &AI->getParent()->getParent()->getEntryBlock())
Chris Lattnera8ab89e2003-06-17 04:39:14 +0000178 return 0;
179 return AI;
180 }
181
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000182 // Instruction visitation functions
183 friend class InstVisitor<CWriter>;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000184
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000185 void visitReturnInst(ReturnInst &I);
186 void visitBranchInst(BranchInst &I);
Chris Lattnera9f5e052003-04-22 20:19:52 +0000187 void visitSwitchInst(SwitchInst &I);
Chris Lattnercb3ad012004-05-09 20:41:32 +0000188 void visitInvokeInst(InvokeInst &I) {
189 assert(0 && "Lowerinvoke pass didn't work!");
190 }
191
192 void visitUnwindInst(UnwindInst &I) {
193 assert(0 && "Lowerinvoke pass didn't work!");
194 }
Chris Lattnera9d12c02004-10-16 18:12:13 +0000195 void visitUnreachableInst(UnreachableInst &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000196
Chris Lattner84e66652003-05-03 07:11:00 +0000197 void visitPHINode(PHINode &I);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000198 void visitBinaryOperator(Instruction &I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000199
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000200 void visitCastInst (CastInst &I);
Chris Lattner9f24a072004-03-12 05:52:14 +0000201 void visitSelectInst(SelectInst &I);
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000202 void visitCallInst (CallInst &I);
203 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000204
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000205 void visitMallocInst(MallocInst &I);
206 void visitAllocaInst(AllocaInst &I);
207 void visitFreeInst (FreeInst &I);
208 void visitLoadInst (LoadInst &I);
209 void visitStoreInst (StoreInst &I);
210 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner4d45bd02003-10-18 05:57:43 +0000211 void visitVAArgInst (VAArgInst &I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000212
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000213 void visitInstruction(Instruction &I) {
Chris Lattnere7f65d3b2002-06-30 16:07:20 +0000214 std::cerr << "C Writer does not know about " << I;
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000215 abort();
216 }
217
218 void outputLValue(Instruction *I) {
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000219 Out << " " << Mang->getValueName(I) << " = ";
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000220 }
Chris Lattnercb3ad012004-05-09 20:41:32 +0000221
222 bool isGotoCodeNecessary(BasicBlock *From, BasicBlock *To);
John Criswell57bbfce2004-10-20 14:38:39 +0000223 void printPHICopiesForSuccessor(BasicBlock *CurBlock,
224 BasicBlock *Successor, unsigned Indent);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000225 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
226 unsigned Indent);
Chris Lattner23e90702003-11-25 20:49:55 +0000227 void printIndexingExpression(Value *Ptr, gep_type_iterator I,
228 gep_type_iterator E);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000229 };
Chris Lattner4ef51372004-02-14 00:31:10 +0000230}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000231
Chris Lattner68758072004-05-09 06:20:51 +0000232/// This method inserts names for any unnamed structure types that are used by
233/// the program, and removes names from structure types that are not used by the
234/// program.
235///
Chris Lattnerf9a05322006-02-13 22:22:42 +0000236bool CBackendNameAllUsedStructsAndMergeFunctions::runOnModule(Module &M) {
Chris Lattner68758072004-05-09 06:20:51 +0000237 // Get a set of types that are used by the program...
238 std::set<const Type *> UT = getAnalysis<FindUsedTypes>().getTypes();
Misha Brukmand6a29a52005-04-20 16:05:03 +0000239
Chris Lattner68758072004-05-09 06:20:51 +0000240 // Loop over the module symbol table, removing types from UT that are
Chris Lattnerdbf69f12005-03-08 16:19:59 +0000241 // already named, and removing names for types that are not used.
Chris Lattner68758072004-05-09 06:20:51 +0000242 //
243 SymbolTable &MST = M.getSymbolTable();
Reid Spencer9231ac82004-05-25 08:53:40 +0000244 for (SymbolTable::type_iterator TI = MST.type_begin(), TE = MST.type_end();
245 TI != TE; ) {
246 SymbolTable::type_iterator I = TI++;
Chris Lattnerdbf69f12005-03-08 16:19:59 +0000247
248 // If this is not used, remove it from the symbol table.
249 std::set<const Type *>::iterator UTI = UT.find(I->second);
250 if (UTI == UT.end())
251 MST.remove(I);
252 else
253 UT.erase(UTI); // Only keep one name for this type.
Reid Spencer9231ac82004-05-25 08:53:40 +0000254 }
Chris Lattner68758072004-05-09 06:20:51 +0000255
256 // UT now contains types that are not named. Loop over it, naming
257 // structure types.
258 //
259 bool Changed = false;
Chris Lattner9d1af972004-05-28 05:47:27 +0000260 unsigned RenameCounter = 0;
Chris Lattner68758072004-05-09 06:20:51 +0000261 for (std::set<const Type *>::const_iterator I = UT.begin(), E = UT.end();
262 I != E; ++I)
263 if (const StructType *ST = dyn_cast<StructType>(*I)) {
Chris Lattner9d1af972004-05-28 05:47:27 +0000264 while (M.addTypeName("unnamed"+utostr(RenameCounter), ST))
265 ++RenameCounter;
Chris Lattner68758072004-05-09 06:20:51 +0000266 Changed = true;
267 }
Chris Lattnerf9a05322006-02-13 22:22:42 +0000268
269
270 // Loop over all external functions and globals. If we have two with
271 // identical names, merge them.
272 // FIXME: This code should disappear when we don't allow values with the same
273 // names when they have different types!
274 std::map<std::string, GlobalValue*> ExtSymbols;
275 for (Module::iterator I = M.begin(), E = M.end(); I != E;) {
276 Function *GV = I++;
277 if (GV->isExternal() && GV->hasName()) {
278 std::pair<std::map<std::string, GlobalValue*>::iterator, bool> X
279 = ExtSymbols.insert(std::make_pair(GV->getName(), GV));
280 if (!X.second) {
281 // Found a conflict, replace this global with the previous one.
282 GlobalValue *OldGV = X.first->second;
283 GV->replaceAllUsesWith(ConstantExpr::getCast(OldGV, GV->getType()));
284 GV->eraseFromParent();
285 Changed = true;
286 }
287 }
288 }
289 // Do the same for globals.
290 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
291 I != E;) {
292 GlobalVariable *GV = I++;
293 if (GV->isExternal() && GV->hasName()) {
294 std::pair<std::map<std::string, GlobalValue*>::iterator, bool> X
295 = ExtSymbols.insert(std::make_pair(GV->getName(), GV));
296 if (!X.second) {
297 // Found a conflict, replace this global with the previous one.
298 GlobalValue *OldGV = X.first->second;
299 GV->replaceAllUsesWith(ConstantExpr::getCast(OldGV, GV->getType()));
300 GV->eraseFromParent();
301 Changed = true;
302 }
303 }
304 }
305
Chris Lattner68758072004-05-09 06:20:51 +0000306 return Changed;
307}
308
Chris Lattner0c54d892006-05-23 23:39:48 +0000309/// printStructReturnPointerFunctionType - This is like printType for a struct
310/// return type, except, instead of printing the type as void (*)(Struct*, ...)
311/// print it as "Struct (*)(...)", for struct return functions.
312void CWriter::printStructReturnPointerFunctionType(std::ostream &Out,
313 const PointerType *TheTy) {
314 const FunctionType *FTy = cast<FunctionType>(TheTy->getElementType());
315 std::stringstream FunctionInnards;
316 FunctionInnards << " (*) (";
317 bool PrintedType = false;
318
319 FunctionType::param_iterator I = FTy->param_begin(), E = FTy->param_end();
320 const Type *RetTy = cast<PointerType>(I->get())->getElementType();
321 for (++I; I != E; ++I) {
322 if (PrintedType)
323 FunctionInnards << ", ";
324 printType(FunctionInnards, *I, "");
325 PrintedType = true;
326 }
327 if (FTy->isVarArg()) {
328 if (PrintedType)
329 FunctionInnards << ", ...";
330 } else if (!PrintedType) {
331 FunctionInnards << "void";
332 }
333 FunctionInnards << ')';
334 std::string tstr = FunctionInnards.str();
335 printType(Out, RetTy, tstr);
336}
337
Chris Lattner68758072004-05-09 06:20:51 +0000338
Chris Lattner83c57752002-08-19 22:17:53 +0000339// Pass the Type* and the variable name and this prints out the variable
340// declaration.
341//
Chris Lattneredd8ce12003-05-03 03:14:35 +0000342std::ostream &CWriter::printType(std::ostream &Out, const Type *Ty,
343 const std::string &NameSoFar,
Chris Lattner7635ea42003-11-03 01:01:59 +0000344 bool IgnoreName) {
Chris Lattner83c57752002-08-19 22:17:53 +0000345 if (Ty->isPrimitiveType())
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000346 switch (Ty->getTypeID()) {
Chris Lattner83c57752002-08-19 22:17:53 +0000347 case Type::VoidTyID: return Out << "void " << NameSoFar;
348 case Type::BoolTyID: return Out << "bool " << NameSoFar;
349 case Type::UByteTyID: return Out << "unsigned char " << NameSoFar;
350 case Type::SByteTyID: return Out << "signed char " << NameSoFar;
351 case Type::UShortTyID: return Out << "unsigned short " << NameSoFar;
352 case Type::ShortTyID: return Out << "short " << NameSoFar;
353 case Type::UIntTyID: return Out << "unsigned " << NameSoFar;
354 case Type::IntTyID: return Out << "int " << NameSoFar;
355 case Type::ULongTyID: return Out << "unsigned long long " << NameSoFar;
356 case Type::LongTyID: return Out << "signed long long " << NameSoFar;
357 case Type::FloatTyID: return Out << "float " << NameSoFar;
358 case Type::DoubleTyID: return Out << "double " << NameSoFar;
359 default :
Chris Lattner76e2df22004-07-15 02:14:30 +0000360 std::cerr << "Unknown primitive type: " << *Ty << "\n";
Chris Lattner83c57752002-08-19 22:17:53 +0000361 abort();
362 }
Misha Brukmand6a29a52005-04-20 16:05:03 +0000363
Chris Lattner83c57752002-08-19 22:17:53 +0000364 // Check to see if the type is named.
Chris Lattner04b72c82002-10-16 00:08:22 +0000365 if (!IgnoreName || isa<OpaqueType>(Ty)) {
Chris Lattneredd8ce12003-05-03 03:14:35 +0000366 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000367 if (I != TypeNames.end()) return Out << I->second << ' ' << NameSoFar;
Chris Lattner04b72c82002-10-16 00:08:22 +0000368 }
Chris Lattner83c57752002-08-19 22:17:53 +0000369
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000370 switch (Ty->getTypeID()) {
Chris Lattner83c57752002-08-19 22:17:53 +0000371 case Type::FunctionTyID: {
Chris Lattner0c54d892006-05-23 23:39:48 +0000372 const FunctionType *FTy = cast<FunctionType>(Ty);
Misha Brukmand6a29a52005-04-20 16:05:03 +0000373 std::stringstream FunctionInnards;
Joel Stanley54f60322003-06-25 04:52:09 +0000374 FunctionInnards << " (" << NameSoFar << ") (";
Chris Lattner0c54d892006-05-23 23:39:48 +0000375 for (FunctionType::param_iterator I = FTy->param_begin(),
376 E = FTy->param_end(); I != E; ++I) {
377 if (I != FTy->param_begin())
Joel Stanley54f60322003-06-25 04:52:09 +0000378 FunctionInnards << ", ";
379 printType(FunctionInnards, *I, "");
Chris Lattner83c57752002-08-19 22:17:53 +0000380 }
Chris Lattner0c54d892006-05-23 23:39:48 +0000381 if (FTy->isVarArg()) {
382 if (FTy->getNumParams())
Reid Spencer9231ac82004-05-25 08:53:40 +0000383 FunctionInnards << ", ...";
Chris Lattner0c54d892006-05-23 23:39:48 +0000384 } else if (!FTy->getNumParams()) {
Joel Stanley54f60322003-06-25 04:52:09 +0000385 FunctionInnards << "void";
Chris Lattner83c57752002-08-19 22:17:53 +0000386 }
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000387 FunctionInnards << ')';
Joel Stanley54f60322003-06-25 04:52:09 +0000388 std::string tstr = FunctionInnards.str();
Chris Lattner0c54d892006-05-23 23:39:48 +0000389 printType(Out, FTy->getReturnType(), tstr);
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000390 return Out;
Chris Lattner83c57752002-08-19 22:17:53 +0000391 }
392 case Type::StructTyID: {
393 const StructType *STy = cast<StructType>(Ty);
394 Out << NameSoFar + " {\n";
395 unsigned Idx = 0;
Chris Lattnerd21cd802004-02-09 04:37:31 +0000396 for (StructType::element_iterator I = STy->element_begin(),
397 E = STy->element_end(); I != E; ++I) {
Chris Lattner83c57752002-08-19 22:17:53 +0000398 Out << " ";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000399 printType(Out, *I, "field" + utostr(Idx++));
Chris Lattner83c57752002-08-19 22:17:53 +0000400 Out << ";\n";
401 }
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000402 return Out << '}';
Misha Brukmand6a29a52005-04-20 16:05:03 +0000403 }
Chris Lattner83c57752002-08-19 22:17:53 +0000404
405 case Type::PointerTyID: {
406 const PointerType *PTy = cast<PointerType>(Ty);
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000407 std::string ptrName = "*" + NameSoFar;
408
Robert Bocchinob78e8382006-01-20 20:43:57 +0000409 if (isa<ArrayType>(PTy->getElementType()) ||
410 isa<PackedType>(PTy->getElementType()))
Chris Lattner8917d362003-11-03 04:31:54 +0000411 ptrName = "(" + ptrName + ")";
Vikram S. Adve969c4ad2002-08-25 20:00:08 +0000412
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000413 return printType(Out, PTy->getElementType(), ptrName);
Misha Brukman8f9f9a22003-07-21 16:34:35 +0000414 }
Chris Lattner83c57752002-08-19 22:17:53 +0000415
416 case Type::ArrayTyID: {
417 const ArrayType *ATy = cast<ArrayType>(Ty);
418 unsigned NumElements = ATy->getNumElements();
Chris Lattner3e3b6f72004-12-15 23:13:15 +0000419 if (NumElements == 0) NumElements = 1;
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000420 return printType(Out, ATy->getElementType(),
Chris Lattner83c57752002-08-19 22:17:53 +0000421 NameSoFar + "[" + utostr(NumElements) + "]");
422 }
Chris Lattner04b72c82002-10-16 00:08:22 +0000423
Robert Bocchinob78e8382006-01-20 20:43:57 +0000424 case Type::PackedTyID: {
425 const PackedType *PTy = cast<PackedType>(Ty);
426 unsigned NumElements = PTy->getNumElements();
427 if (NumElements == 0) NumElements = 1;
428 return printType(Out, PTy->getElementType(),
429 NameSoFar + "[" + utostr(NumElements) + "]");
430 }
431
Chris Lattner04b72c82002-10-16 00:08:22 +0000432 case Type::OpaqueTyID: {
433 static int Count = 0;
Chris Lattneredd8ce12003-05-03 03:14:35 +0000434 std::string TyName = "struct opaque_" + itostr(Count++);
Chris Lattner04b72c82002-10-16 00:08:22 +0000435 assert(TypeNames.find(Ty) == TypeNames.end());
436 TypeNames[Ty] = TyName;
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000437 return Out << TyName << ' ' << NameSoFar;
Chris Lattner04b72c82002-10-16 00:08:22 +0000438 }
Chris Lattner83c57752002-08-19 22:17:53 +0000439 default:
440 assert(0 && "Unhandled case in getTypeProps!");
441 abort();
442 }
443
444 return Out;
445}
446
Chris Lattner6d492922002-08-19 21:32:41 +0000447void CWriter::printConstantArray(ConstantArray *CPA) {
448
449 // As a special case, print the array as a string if it is an array of
450 // ubytes or an array of sbytes with positive values.
Misha Brukmand6a29a52005-04-20 16:05:03 +0000451 //
Chris Lattner6d492922002-08-19 21:32:41 +0000452 const Type *ETy = CPA->getType()->getElementType();
453 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
454
455 // Make sure the last character is a null char, as automatically added by C
Chris Lattner02da6c02003-06-16 12:09:09 +0000456 if (isString && (CPA->getNumOperands() == 0 ||
457 !cast<Constant>(*(CPA->op_end()-1))->isNullValue()))
Chris Lattner6d492922002-08-19 21:32:41 +0000458 isString = false;
Misha Brukmand6a29a52005-04-20 16:05:03 +0000459
Chris Lattner6d492922002-08-19 21:32:41 +0000460 if (isString) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000461 Out << '\"';
Chris Lattner02da6c02003-06-16 12:09:09 +0000462 // Keep track of whether the last number was a hexadecimal escape
463 bool LastWasHex = false;
464
Chris Lattner6d492922002-08-19 21:32:41 +0000465 // Do not include the last character, which we know is null
466 for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000467 unsigned char C = cast<ConstantInt>(CPA->getOperand(i))->getZExtValue();
Misha Brukmand6a29a52005-04-20 16:05:03 +0000468
Chris Lattner02da6c02003-06-16 12:09:09 +0000469 // Print it out literally if it is a printable character. The only thing
470 // to be careful about is when the last letter output was a hex escape
471 // code, in which case we have to be careful not to print out hex digits
Chris Lattnerda920902003-06-16 12:21:19 +0000472 // explicitly (the C compiler thinks it is a continuation of the previous
473 // character, sheesh...)
Chris Lattner02da6c02003-06-16 12:09:09 +0000474 //
475 if (isprint(C) && (!LastWasHex || !isxdigit(C))) {
476 LastWasHex = false;
Nick Hildenbrandt088b4722002-11-06 21:40:23 +0000477 if (C == '"' || C == '\\')
478 Out << "\\" << C;
Nick Hildenbrandtc3dd2af2002-09-30 21:11:55 +0000479 else
480 Out << C;
Chris Lattner6d492922002-08-19 21:32:41 +0000481 } else {
Chris Lattner02da6c02003-06-16 12:09:09 +0000482 LastWasHex = false;
Chris Lattner6d492922002-08-19 21:32:41 +0000483 switch (C) {
484 case '\n': Out << "\\n"; break;
485 case '\t': Out << "\\t"; break;
486 case '\r': Out << "\\r"; break;
487 case '\v': Out << "\\v"; break;
488 case '\a': Out << "\\a"; break;
Nick Hildenbrandtc3dd2af2002-09-30 21:11:55 +0000489 case '\"': Out << "\\\""; break;
Misha Brukmand6a29a52005-04-20 16:05:03 +0000490 case '\'': Out << "\\\'"; break;
Chris Lattner6d492922002-08-19 21:32:41 +0000491 default:
492 Out << "\\x";
Chris Lattner02da6c02003-06-16 12:09:09 +0000493 Out << (char)(( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'));
494 Out << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
495 LastWasHex = true;
Chris Lattner6d492922002-08-19 21:32:41 +0000496 break;
497 }
498 }
499 }
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000500 Out << '\"';
Chris Lattner6d492922002-08-19 21:32:41 +0000501 } else {
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000502 Out << '{';
Chris Lattner6d492922002-08-19 21:32:41 +0000503 if (CPA->getNumOperands()) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000504 Out << ' ';
Chris Lattner6d492922002-08-19 21:32:41 +0000505 printConstant(cast<Constant>(CPA->getOperand(0)));
506 for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
507 Out << ", ";
508 printConstant(cast<Constant>(CPA->getOperand(i)));
509 }
510 }
511 Out << " }";
512 }
513}
514
Robert Bocchinob78e8382006-01-20 20:43:57 +0000515void CWriter::printConstantPacked(ConstantPacked *CP) {
516 Out << '{';
517 if (CP->getNumOperands()) {
518 Out << ' ';
519 printConstant(cast<Constant>(CP->getOperand(0)));
520 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
521 Out << ", ";
522 printConstant(cast<Constant>(CP->getOperand(i)));
523 }
524 }
525 Out << " }";
526}
527
Chris Lattnerdd76aca2003-10-05 00:40:51 +0000528// isFPCSafeToPrint - Returns true if we may assume that CFP may be written out
529// textually as a double (rather than as a reference to a stack-allocated
530// variable). We decide this by converting CFP to a string and back into a
531// double, and then checking whether the conversion results in a bit-equal
532// double to the original value of CFP. This depends on us and the target C
533// compiler agreeing on the conversion process (which is pretty likely since we
534// only deal in IEEE FP).
535//
Chris Lattnercb3ad012004-05-09 20:41:32 +0000536static bool isFPCSafeToPrint(const ConstantFP *CFP) {
Chris Lattner4946a8c2003-10-13 17:13:53 +0000537#if HAVE_PRINTF_A
Chris Lattner5ef6ffd2003-10-12 08:12:58 +0000538 char Buffer[100];
539 sprintf(Buffer, "%a", CFP->getValue());
540
541 if (!strncmp(Buffer, "0x", 2) ||
542 !strncmp(Buffer, "-0x", 3) ||
543 !strncmp(Buffer, "+0x", 3))
544 return atof(Buffer) == CFP->getValue();
545 return false;
546#else
Brian Gaekeb471a232003-06-17 23:55:35 +0000547 std::string StrVal = ftostr(CFP->getValue());
Chris Lattner9860e772003-10-12 04:36:29 +0000548
549 while (StrVal[0] == ' ')
550 StrVal.erase(StrVal.begin());
551
Chris Lattnerdd76aca2003-10-05 00:40:51 +0000552 // Check to make sure that the stringized number is not some string like "Inf"
553 // or NaN. Check that the string matches the "[-+]?[0-9]" regex.
Brian Gaekeb471a232003-06-17 23:55:35 +0000554 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
555 ((StrVal[0] == '-' || StrVal[0] == '+') &&
556 (StrVal[1] >= '0' && StrVal[1] <= '9')))
557 // Reparse stringized version!
Chris Lattnerdd76aca2003-10-05 00:40:51 +0000558 return atof(StrVal.c_str()) == CFP->getValue();
Brian Gaekeb471a232003-06-17 23:55:35 +0000559 return false;
Chris Lattner5ef6ffd2003-10-12 08:12:58 +0000560#endif
Brian Gaekeb471a232003-06-17 23:55:35 +0000561}
Chris Lattner6d492922002-08-19 21:32:41 +0000562
563// printConstant - The LLVM Constant to C Constant converter.
564void CWriter::printConstant(Constant *CPV) {
565 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
566 switch (CE->getOpcode()) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000567 case Instruction::Cast:
568 Out << "((";
Nick Hildenbrandt98502372002-11-18 20:55:50 +0000569 printType(Out, CPV->getType());
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000570 Out << ')';
Chris Lattner84c0d5e2003-05-14 17:50:19 +0000571 printConstant(CE->getOperand(0));
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000572 Out << ')';
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000573 return;
574
575 case Instruction::GetElementPtr:
Nick Hildenbrandtd2eb3862002-10-03 20:47:24 +0000576 Out << "(&(";
Chris Lattner23e90702003-11-25 20:49:55 +0000577 printIndexingExpression(CE->getOperand(0), gep_type_begin(CPV),
578 gep_type_end(CPV));
Nick Hildenbrandtd2eb3862002-10-03 20:47:24 +0000579 Out << "))";
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000580 return;
Chris Lattner6683dbf2004-04-01 05:28:26 +0000581 case Instruction::Select:
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000582 Out << '(';
Chris Lattner6683dbf2004-04-01 05:28:26 +0000583 printConstant(CE->getOperand(0));
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000584 Out << '?';
Chris Lattner6683dbf2004-04-01 05:28:26 +0000585 printConstant(CE->getOperand(1));
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000586 Out << ':';
Chris Lattner6683dbf2004-04-01 05:28:26 +0000587 printConstant(CE->getOperand(2));
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000588 Out << ')';
Chris Lattner6683dbf2004-04-01 05:28:26 +0000589 return;
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000590 case Instruction::Add:
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000591 case Instruction::Sub:
Chris Lattner29255922003-08-14 19:19:53 +0000592 case Instruction::Mul:
Reid Spencer1628cec2006-10-26 06:15:43 +0000593 case Instruction::SDiv:
594 case Instruction::UDiv:
595 case Instruction::FDiv:
Chris Lattner29255922003-08-14 19:19:53 +0000596 case Instruction::Rem:
Chris Lattnerf376e5e2004-12-29 04:00:09 +0000597 case Instruction::And:
598 case Instruction::Or:
599 case Instruction::Xor:
Chris Lattner29255922003-08-14 19:19:53 +0000600 case Instruction::SetEQ:
601 case Instruction::SetNE:
602 case Instruction::SetLT:
603 case Instruction::SetLE:
604 case Instruction::SetGT:
605 case Instruction::SetGE:
Brian Gaeke0415b6c2003-11-22 05:02:56 +0000606 case Instruction::Shl:
607 case Instruction::Shr:
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000608 Out << '(';
Reid Spencer1628cec2006-10-26 06:15:43 +0000609 bool NeedsClosingParens = printConstExprCast(CE);
610 printConstantWithCast(CE->getOperand(0), CE->getOpcode());
Chris Lattner29255922003-08-14 19:19:53 +0000611 switch (CE->getOpcode()) {
612 case Instruction::Add: Out << " + "; break;
613 case Instruction::Sub: Out << " - "; break;
614 case Instruction::Mul: Out << " * "; break;
Reid Spencer1628cec2006-10-26 06:15:43 +0000615 case Instruction::UDiv:
616 case Instruction::SDiv:
617 case Instruction::FDiv: Out << " / "; break;
Chris Lattner29255922003-08-14 19:19:53 +0000618 case Instruction::Rem: Out << " % "; break;
Chris Lattnerf376e5e2004-12-29 04:00:09 +0000619 case Instruction::And: Out << " & "; break;
620 case Instruction::Or: Out << " | "; break;
621 case Instruction::Xor: Out << " ^ "; break;
Chris Lattner29255922003-08-14 19:19:53 +0000622 case Instruction::SetEQ: Out << " == "; break;
623 case Instruction::SetNE: Out << " != "; break;
624 case Instruction::SetLT: Out << " < "; break;
625 case Instruction::SetLE: Out << " <= "; break;
626 case Instruction::SetGT: Out << " > "; break;
627 case Instruction::SetGE: Out << " >= "; break;
Brian Gaeke0415b6c2003-11-22 05:02:56 +0000628 case Instruction::Shl: Out << " << "; break;
629 case Instruction::Shr: Out << " >> "; break;
Chris Lattner29255922003-08-14 19:19:53 +0000630 default: assert(0 && "Illegal opcode here!");
631 }
Reid Spencer1628cec2006-10-26 06:15:43 +0000632 printConstantWithCast(CE->getOperand(1), CE->getOpcode());
633 if (NeedsClosingParens)
634 Out << "))";
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000635 Out << ')';
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000636 return;
637
Chris Lattner6d492922002-08-19 21:32:41 +0000638 default:
639 std::cerr << "CWriter Error: Unhandled constant expression: "
Chris Lattner76e2df22004-07-15 02:14:30 +0000640 << *CE << "\n";
Chris Lattner6d492922002-08-19 21:32:41 +0000641 abort();
642 }
Chris Lattnera9d12c02004-10-16 18:12:13 +0000643 } else if (isa<UndefValue>(CPV) && CPV->getType()->isFirstClassType()) {
Chris Lattner665825e2004-10-17 17:48:59 +0000644 Out << "((";
645 printType(Out, CPV->getType());
646 Out << ")/*UNDEF*/0)";
Chris Lattnera9d12c02004-10-16 18:12:13 +0000647 return;
Chris Lattner6d492922002-08-19 21:32:41 +0000648 }
649
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000650 switch (CPV->getType()->getTypeID()) {
Chris Lattner6d492922002-08-19 21:32:41 +0000651 case Type::BoolTyID:
Chris Lattner33ce7772006-09-28 23:19:29 +0000652 Out << (cast<ConstantBool>(CPV)->getValue() ? '1' : '0');
653 break;
Chris Lattner6d492922002-08-19 21:32:41 +0000654 case Type::SByteTyID:
655 case Type::ShortTyID:
Reid Spencerb83eb642006-10-20 07:07:24 +0000656 Out << cast<ConstantInt>(CPV)->getSExtValue();
Chris Lattner33ce7772006-09-28 23:19:29 +0000657 break;
Chris Lattner45343ea2003-05-12 15:39:31 +0000658 case Type::IntTyID:
Reid Spencerb83eb642006-10-20 07:07:24 +0000659 if ((int)cast<ConstantInt>(CPV)->getSExtValue() == (int)0x80000000)
Chris Lattner40e7c352004-11-30 21:33:58 +0000660 Out << "((int)0x80000000U)"; // Handle MININT specially to avoid warning
Chris Lattner45343ea2003-05-12 15:39:31 +0000661 else
Reid Spencerb83eb642006-10-20 07:07:24 +0000662 Out << cast<ConstantInt>(CPV)->getSExtValue();
Chris Lattner45343ea2003-05-12 15:39:31 +0000663 break;
664
Chris Lattner6d492922002-08-19 21:32:41 +0000665 case Type::LongTyID:
Reid Spencerb83eb642006-10-20 07:07:24 +0000666 if (cast<ConstantInt>(CPV)->isMinValue())
Chris Lattner40e7c352004-11-30 21:33:58 +0000667 Out << "(/*INT64_MIN*/(-9223372036854775807LL)-1)";
668 else
Reid Spencerb83eb642006-10-20 07:07:24 +0000669 Out << cast<ConstantInt>(CPV)->getSExtValue() << "ll";
Chris Lattner33ce7772006-09-28 23:19:29 +0000670 break;
Chris Lattner6d492922002-08-19 21:32:41 +0000671
672 case Type::UByteTyID:
673 case Type::UShortTyID:
Reid Spencerb83eb642006-10-20 07:07:24 +0000674 Out << cast<ConstantInt>(CPV)->getZExtValue();
Chris Lattner33ce7772006-09-28 23:19:29 +0000675 break;
Chris Lattner6d492922002-08-19 21:32:41 +0000676 case Type::UIntTyID:
Reid Spencerb83eb642006-10-20 07:07:24 +0000677 Out << cast<ConstantInt>(CPV)->getZExtValue() << 'u';
Chris Lattner33ce7772006-09-28 23:19:29 +0000678 break;
Chris Lattner6d492922002-08-19 21:32:41 +0000679 case Type::ULongTyID:
Reid Spencerb83eb642006-10-20 07:07:24 +0000680 Out << cast<ConstantInt>(CPV)->getZExtValue() << "ull";
Chris Lattner33ce7772006-09-28 23:19:29 +0000681 break;
Chris Lattner6d492922002-08-19 21:32:41 +0000682
683 case Type::FloatTyID:
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000684 case Type::DoubleTyID: {
685 ConstantFP *FPC = cast<ConstantFP>(CPV);
Chris Lattneredd8ce12003-05-03 03:14:35 +0000686 std::map<const ConstantFP*, unsigned>::iterator I = FPConstantMap.find(FPC);
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000687 if (I != FPConstantMap.end()) {
688 // Because of FP precision problems we must load from a stack allocated
689 // value that holds the value in hex.
690 Out << "(*(" << (FPC->getType() == Type::FloatTy ? "float" : "double")
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000691 << "*)&FPConstant" << I->second << ')';
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000692 } else {
Brian Gaeke043c0bb2004-07-21 03:15:26 +0000693 if (IsNAN(FPC->getValue())) {
694 // The value is NaN
Misha Brukmand6a29a52005-04-20 16:05:03 +0000695
Brian Gaeke8a702e82004-08-25 19:00:42 +0000696 // The prefix for a quiet NaN is 0x7FF8. For a signalling NaN,
697 // it's 0x7ff4.
698 const unsigned long QuietNaN = 0x7ff8UL;
699 const unsigned long SignalNaN = 0x7ff4UL;
700
701 // We need to grab the first part of the FP #
Brian Gaeke8a702e82004-08-25 19:00:42 +0000702 char Buffer[100];
703
Jim Laskeycb6682f2005-08-17 19:34:49 +0000704 uint64_t ll = DoubleToBits(FPC->getValue());
Reid Spencer2bc320d2006-05-31 22:26:11 +0000705 sprintf(Buffer, "0x%llx", static_cast<long long>(ll));
Brian Gaeke8a702e82004-08-25 19:00:42 +0000706
707 std::string Num(&Buffer[0], &Buffer[6]);
708 unsigned long Val = strtoul(Num.c_str(), 0, 16);
709
Brian Gaeke043c0bb2004-07-21 03:15:26 +0000710 if (FPC->getType() == Type::FloatTy)
Brian Gaeke8a702e82004-08-25 19:00:42 +0000711 Out << "LLVM_NAN" << (Val == QuietNaN ? "" : "S") << "F(\""
712 << Buffer << "\") /*nan*/ ";
Brian Gaeke043c0bb2004-07-21 03:15:26 +0000713 else
Brian Gaeke8a702e82004-08-25 19:00:42 +0000714 Out << "LLVM_NAN" << (Val == QuietNaN ? "" : "S") << "(\""
715 << Buffer << "\") /*nan*/ ";
Brian Gaeke043c0bb2004-07-21 03:15:26 +0000716 } else if (IsInf(FPC->getValue())) {
717 // The value is Inf
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000718 if (FPC->getValue() < 0) Out << '-';
Brian Gaeke8a702e82004-08-25 19:00:42 +0000719 Out << "LLVM_INF" << (FPC->getType() == Type::FloatTy ? "F" : "")
720 << " /*inf*/ ";
Brian Gaeke043c0bb2004-07-21 03:15:26 +0000721 } else {
Brian Gaeke07b52b32004-08-25 19:37:26 +0000722 std::string Num;
723#if HAVE_PRINTF_A
724 // Print out the constant as a floating point number.
725 char Buffer[100];
726 sprintf(Buffer, "%a", FPC->getValue());
727 Num = Buffer;
728#else
729 Num = ftostr(FPC->getValue());
730#endif
Brian Gaeke043c0bb2004-07-21 03:15:26 +0000731 Out << Num;
732 }
Chris Lattnerd1cf1b42002-09-20 23:26:33 +0000733 }
734 break;
735 }
Chris Lattner6d492922002-08-19 21:32:41 +0000736
737 case Type::ArrayTyID:
Chris Lattnera9d12c02004-10-16 18:12:13 +0000738 if (isa<ConstantAggregateZero>(CPV) || isa<UndefValue>(CPV)) {
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000739 const ArrayType *AT = cast<ArrayType>(CPV->getType());
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000740 Out << '{';
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000741 if (AT->getNumElements()) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000742 Out << ' ';
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000743 Constant *CZ = Constant::getNullValue(AT->getElementType());
744 printConstant(CZ);
745 for (unsigned i = 1, e = AT->getNumElements(); i != e; ++i) {
746 Out << ", ";
747 printConstant(CZ);
748 }
749 }
750 Out << " }";
751 } else {
752 printConstantArray(cast<ConstantArray>(CPV));
753 }
Chris Lattner6d492922002-08-19 21:32:41 +0000754 break;
755
Robert Bocchinob78e8382006-01-20 20:43:57 +0000756 case Type::PackedTyID:
757 if (isa<ConstantAggregateZero>(CPV) || isa<UndefValue>(CPV)) {
758 const PackedType *AT = cast<PackedType>(CPV->getType());
759 Out << '{';
760 if (AT->getNumElements()) {
761 Out << ' ';
762 Constant *CZ = Constant::getNullValue(AT->getElementType());
763 printConstant(CZ);
764 for (unsigned i = 1, e = AT->getNumElements(); i != e; ++i) {
765 Out << ", ";
766 printConstant(CZ);
767 }
768 }
769 Out << " }";
770 } else {
771 printConstantPacked(cast<ConstantPacked>(CPV));
772 }
773 break;
774
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000775 case Type::StructTyID:
Chris Lattnera9d12c02004-10-16 18:12:13 +0000776 if (isa<ConstantAggregateZero>(CPV) || isa<UndefValue>(CPV)) {
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000777 const StructType *ST = cast<StructType>(CPV->getType());
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000778 Out << '{';
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000779 if (ST->getNumElements()) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000780 Out << ' ';
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000781 printConstant(Constant::getNullValue(ST->getElementType(0)));
782 for (unsigned i = 1, e = ST->getNumElements(); i != e; ++i) {
783 Out << ", ";
784 printConstant(Constant::getNullValue(ST->getElementType(i)));
785 }
Chris Lattner6d492922002-08-19 21:32:41 +0000786 }
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000787 Out << " }";
788 } else {
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000789 Out << '{';
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000790 if (CPV->getNumOperands()) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000791 Out << ' ';
Chris Lattnercfb0fd22004-02-15 05:54:27 +0000792 printConstant(cast<Constant>(CPV->getOperand(0)));
793 for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) {
794 Out << ", ";
795 printConstant(cast<Constant>(CPV->getOperand(i)));
796 }
797 }
798 Out << " }";
Chris Lattner6d492922002-08-19 21:32:41 +0000799 }
Chris Lattner6d492922002-08-19 21:32:41 +0000800 break;
Chris Lattner6d492922002-08-19 21:32:41 +0000801
802 case Type::PointerTyID:
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000803 if (isa<ConstantPointerNull>(CPV)) {
Chris Lattnercf135cb2003-06-02 03:10:53 +0000804 Out << "((";
805 printType(Out, CPV->getType());
806 Out << ")/*NULL*/0)";
Chris Lattner6d492922002-08-19 21:32:41 +0000807 break;
Reid Spencer518310c2004-07-18 00:44:37 +0000808 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(CPV)) {
809 writeOperand(GV);
Chris Lattner6d492922002-08-19 21:32:41 +0000810 break;
811 }
812 // FALL THROUGH
813 default:
Chris Lattner76e2df22004-07-15 02:14:30 +0000814 std::cerr << "Unknown constant type: " << *CPV << "\n";
Chris Lattner6d492922002-08-19 21:32:41 +0000815 abort();
816 }
817}
818
Reid Spencer1628cec2006-10-26 06:15:43 +0000819// Some constant expressions need to be casted back to the original types
820// because their operands were casted to the expected type. This function takes
821// care of detecting that case and printing the cast for the ConstantExpr.
822bool CWriter::printConstExprCast(const ConstantExpr* CE) {
823 bool Result = false;
824 const Type* Ty = CE->getOperand(0)->getType();
825 switch (CE->getOpcode()) {
826 case Instruction::UDiv: Result = Ty->isSigned(); break;
827 case Instruction::SDiv: Result = Ty->isUnsigned(); break;
828 default: break;
829 }
830 if (Result) {
831 Out << "((";
832 printType(Out, Ty);
833 Out << ")(";
834 }
835 return Result;
836}
837
838// Print a constant assuming that it is the operand for a given Opcode. The
839// opcodes that care about sign need to cast their operands to the expected
840// type before the operation proceeds. This function does the casting.
841void CWriter::printConstantWithCast(Constant* CPV, unsigned Opcode) {
842
843 // Extract the operand's type, we'll need it.
844 const Type* OpTy = CPV->getType();
845
846 // Indicate whether to do the cast or not.
847 bool shouldCast = false;
848
849 // Based on the Opcode for which this Constant is being written, determine
850 // the new type to which the operand should be casted by setting the value
851 // of OpTy. If we change OpTy, also set shouldCast to true.
852 switch (Opcode) {
853 default:
854 // for most instructions, it doesn't matter
855 break;
856 case Instruction::UDiv:
857 // For UDiv to have unsigned operands
858 if (OpTy->isSigned()) {
859 OpTy = OpTy->getUnsignedVersion();
860 shouldCast = true;
861 }
862 break;
863 case Instruction::SDiv:
864 if (OpTy->isUnsigned()) {
865 OpTy = OpTy->getSignedVersion();
866 shouldCast = true;
867 }
868 break;
869 }
870
871 // Write out the casted constnat if we should, otherwise just write the
872 // operand.
873 if (shouldCast) {
874 Out << "((";
875 printType(Out, OpTy);
876 Out << ")";
877 printConstant(CPV);
878 Out << ")";
879 } else
880 writeOperand(CPV);
881
882}
883
Chris Lattner6d492922002-08-19 21:32:41 +0000884void CWriter::writeOperandInternal(Value *Operand) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000885 if (Instruction *I = dyn_cast<Instruction>(Operand))
Chris Lattnera8ab89e2003-06-17 04:39:14 +0000886 if (isInlinableInst(*I) && !isDirectAlloca(I)) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000887 // Should we inline this instruction to build a tree?
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000888 Out << '(';
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000889 visit(*I);
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000890 Out << ')';
Chris Lattnerdf35a1c2002-08-19 23:09:46 +0000891 return;
892 }
Misha Brukmand6a29a52005-04-20 16:05:03 +0000893
Reid Spencer518310c2004-07-18 00:44:37 +0000894 Constant* CPV = dyn_cast<Constant>(Operand);
895 if (CPV && !isa<GlobalValue>(CPV)) {
Misha Brukmand6a29a52005-04-20 16:05:03 +0000896 printConstant(CPV);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000897 } else {
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000898 Out << Mang->getValueName(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000899 }
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000900}
901
Chris Lattnerbb03efd2002-06-25 15:57:03 +0000902void CWriter::writeOperand(Value *Operand) {
Chris Lattnera8ab89e2003-06-17 04:39:14 +0000903 if (isa<GlobalVariable>(Operand) || isDirectAlloca(Operand))
Chris Lattnerd0c668c2002-05-09 21:18:38 +0000904 Out << "(&"; // Global variables are references as their addresses by llvm
905
906 writeOperandInternal(Operand);
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000907
Chris Lattnera8ab89e2003-06-17 04:39:14 +0000908 if (isa<GlobalVariable>(Operand) || isDirectAlloca(Operand))
Misha Brukmanb70aaa62005-02-14 18:52:35 +0000909 Out << ')';
Chris Lattner4fbf26d2002-05-09 20:53:56 +0000910}
911
Reid Spencer1628cec2006-10-26 06:15:43 +0000912// Some instructions need to have their result value casted back to the
913// original types because their operands were casted to the expected type.
914// This function takes care of detecting that case and printing the cast
915// for the Instruction.
916bool CWriter::writeInstructionCast(const Instruction &I) {
917 bool Result = false;
918 const Type* Ty = I.getOperand(0)->getType();
919 switch (I.getOpcode()) {
920 case Instruction::UDiv: Result = Ty->isSigned(); break;
921 case Instruction::SDiv: Result = Ty->isUnsigned(); break;
922 default: break;
923 }
924 if (Result) {
925 Out << "((";
926 printType(Out, Ty);
927 Out << ")(";
928 }
929 return Result;
930}
931
932// Write the operand with a cast to another type based on the Opcode being used.
933// This will be used in cases where an instruction has specific type
934// requirements (usually signedness) for its operands.
935void CWriter::writeOperandWithCast(Value* Operand, unsigned Opcode) {
936
937 // Extract the operand's type, we'll need it.
938 const Type* OpTy = Operand->getType();
939
940 // Indicate whether to do the cast or not.
941 bool shouldCast = false;
942
943 // Based on the Opcode for which this Operand is being written, determine
944 // the new type to which the operand should be casted by setting the value
945 // of OpTy. If we change OpTy, also set shouldCast to true.
946 switch (Opcode) {
947 default:
948 // for most instructions, it doesn't matter
949 break;
950 case Instruction::UDiv:
951 // For UDiv to have unsigned operands
952 if (OpTy->isSigned()) {
953 OpTy = OpTy->getUnsignedVersion();
954 shouldCast = true;
955 }
956 break;
957 case Instruction::SDiv:
958 if (OpTy->isUnsigned()) {
959 OpTy = OpTy->getSignedVersion();
960 shouldCast = true;
961 }
962 break;
963 }
964
965 // Write out the casted operand if we should, otherwise just write the
966 // operand.
967 if (shouldCast) {
968 Out << "((";
969 printType(Out, OpTy);
970 Out << ")";
971 writeOperand(Operand);
972 Out << ")";
973 } else
974 writeOperand(Operand);
975
976}
977
Chris Lattner41488192003-06-28 17:15:12 +0000978// generateCompilerSpecificCode - This is where we add conditional compilation
979// directives to cater to specific compilers as need be.
980//
Chris Lattnerc59c1182003-11-16 22:06:14 +0000981static void generateCompilerSpecificCode(std::ostream& Out) {
Chris Lattner9a571ba2006-03-07 22:58:23 +0000982 // Alloca is hard to get, and we don't want to include stdlib.h here.
Chris Lattner41488192003-06-28 17:15:12 +0000983 Out << "/* get a declaration for alloca */\n"
Chris Lattnerd9477602006-06-02 18:54:01 +0000984 << "#if defined(__CYGWIN__) || defined(__MINGW32__)\n"
Reid Spencera8411a62005-01-23 04:32:47 +0000985 << "extern void *_alloca(unsigned long);\n"
986 << "#define alloca(x) _alloca(x)\n"
987 << "#elif defined(__APPLE__)\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +0000988 << "extern void *__builtin_alloca(unsigned long);\n"
989 << "#define alloca(x) __builtin_alloca(x)\n"
Brian Gaekece630052004-12-10 05:44:45 +0000990 << "#elif defined(__sun__)\n"
991 << "#if defined(__sparcv9)\n"
992 << "extern void *__builtin_alloca(unsigned long);\n"
993 << "#else\n"
994 << "extern void *__builtin_alloca(unsigned int);\n"
995 << "#endif\n"
996 << "#define alloca(x) __builtin_alloca(x)\n"
Jeff Cohen3c280bf2006-04-17 17:55:41 +0000997 << "#elif defined(__FreeBSD__) || defined(__OpenBSD__)\n"
Chris Lattner60e66742004-10-06 04:21:52 +0000998 << "#define alloca(x) __builtin_alloca(x)\n"
Jeff Cohend01f65a2005-01-06 04:21:49 +0000999 << "#elif !defined(_MSC_VER)\n"
Joel Stanleyecd78ac2003-02-12 20:45:00 +00001000 << "#include <alloca.h>\n"
1001 << "#endif\n\n";
Chris Lattner41488192003-06-28 17:15:12 +00001002
1003 // We output GCC specific attributes to preserve 'linkonce'ness on globals.
1004 // If we aren't being compiled with GCC, just drop these attributes.
Chris Lattner9bda5f52003-06-28 17:53:05 +00001005 Out << "#ifndef __GNUC__ /* Can only support \"linkonce\" vars with GCC */\n"
Chris Lattner41488192003-06-28 17:15:12 +00001006 << "#define __attribute__(X)\n"
Brian Gaeke27f7a712003-12-11 00:24:36 +00001007 << "#endif\n\n";
1008
1009#if 0
1010 // At some point, we should support "external weak" vs. "weak" linkages.
1011 // On Mac OS X, "external weak" is spelled "__attribute__((weak_import))".
1012 Out << "#if defined(__GNUC__) && defined(__APPLE_CC__)\n"
1013 << "#define __EXTERNAL_WEAK__ __attribute__((weak_import))\n"
1014 << "#elif defined(__GNUC__)\n"
1015 << "#define __EXTERNAL_WEAK__ __attribute__((weak))\n"
1016 << "#else\n"
1017 << "#define __EXTERNAL_WEAK__\n"
1018 << "#endif\n\n";
1019#endif
1020
1021 // For now, turn off the weak linkage attribute on Mac OS X. (See above.)
1022 Out << "#if defined(__GNUC__) && defined(__APPLE_CC__)\n"
1023 << "#define __ATTRIBUTE_WEAK__\n"
1024 << "#elif defined(__GNUC__)\n"
1025 << "#define __ATTRIBUTE_WEAK__ __attribute__((weak))\n"
1026 << "#else\n"
1027 << "#define __ATTRIBUTE_WEAK__\n"
1028 << "#endif\n\n";
Brian Gaeke043c0bb2004-07-21 03:15:26 +00001029
1030 // Define NaN and Inf as GCC builtins if using GCC, as 0 otherwise
1031 // From the GCC documentation:
Misha Brukmand6a29a52005-04-20 16:05:03 +00001032 //
Brian Gaeke043c0bb2004-07-21 03:15:26 +00001033 // double __builtin_nan (const char *str)
1034 //
1035 // This is an implementation of the ISO C99 function nan.
1036 //
1037 // Since ISO C99 defines this function in terms of strtod, which we do
1038 // not implement, a description of the parsing is in order. The string is
1039 // parsed as by strtol; that is, the base is recognized by leading 0 or
1040 // 0x prefixes. The number parsed is placed in the significand such that
1041 // the least significant bit of the number is at the least significant
1042 // bit of the significand. The number is truncated to fit the significand
1043 // field provided. The significand is forced to be a quiet NaN.
1044 //
1045 // This function, if given a string literal, is evaluated early enough
1046 // that it is considered a compile-time constant.
1047 //
1048 // float __builtin_nanf (const char *str)
1049 //
1050 // Similar to __builtin_nan, except the return type is float.
1051 //
1052 // double __builtin_inf (void)
1053 //
1054 // Similar to __builtin_huge_val, except a warning is generated if the
1055 // target floating-point format does not support infinities. This
1056 // function is suitable for implementing the ISO C99 macro INFINITY.
1057 //
1058 // float __builtin_inff (void)
1059 //
1060 // Similar to __builtin_inf, except the return type is float.
1061 Out << "#ifdef __GNUC__\n"
Brian Gaeke8a702e82004-08-25 19:00:42 +00001062 << "#define LLVM_NAN(NanStr) __builtin_nan(NanStr) /* Double */\n"
1063 << "#define LLVM_NANF(NanStr) __builtin_nanf(NanStr) /* Float */\n"
1064 << "#define LLVM_NANS(NanStr) __builtin_nans(NanStr) /* Double */\n"
1065 << "#define LLVM_NANSF(NanStr) __builtin_nansf(NanStr) /* Float */\n"
1066 << "#define LLVM_INF __builtin_inf() /* Double */\n"
1067 << "#define LLVM_INFF __builtin_inff() /* Float */\n"
Chris Lattnerf9a05322006-02-13 22:22:42 +00001068 << "#define LLVM_PREFETCH(addr,rw,locality) "
1069 "__builtin_prefetch(addr,rw,locality)\n"
Chris Lattner9a571ba2006-03-07 22:58:23 +00001070 << "#define __ATTRIBUTE_CTOR__ __attribute__((constructor))\n"
1071 << "#define __ATTRIBUTE_DTOR__ __attribute__((destructor))\n"
Chris Lattnerdacbe7b2006-07-28 20:58:47 +00001072 << "#define LLVM_ASM __asm__\n"
Brian Gaeke043c0bb2004-07-21 03:15:26 +00001073 << "#else\n"
Brian Gaeke8a702e82004-08-25 19:00:42 +00001074 << "#define LLVM_NAN(NanStr) ((double)0.0) /* Double */\n"
1075 << "#define LLVM_NANF(NanStr) 0.0F /* Float */\n"
1076 << "#define LLVM_NANS(NanStr) ((double)0.0) /* Double */\n"
1077 << "#define LLVM_NANSF(NanStr) 0.0F /* Float */\n"
1078 << "#define LLVM_INF ((double)0.0) /* Double */\n"
1079 << "#define LLVM_INFF 0.0F /* Float */\n"
Chris Lattner50a8a172005-05-06 06:58:42 +00001080 << "#define LLVM_PREFETCH(addr,rw,locality) /* PREFETCH */\n"
Chris Lattner9a571ba2006-03-07 22:58:23 +00001081 << "#define __ATTRIBUTE_CTOR__\n"
1082 << "#define __ATTRIBUTE_DTOR__\n"
Chris Lattnerdacbe7b2006-07-28 20:58:47 +00001083 << "#define LLVM_ASM(X)\n"
Chris Lattner50a8a172005-05-06 06:58:42 +00001084 << "#endif\n\n";
1085
1086 // Output target-specific code that should be inserted into main.
1087 Out << "#define CODE_FOR_MAIN() /* Any target-specific code for main()*/\n";
1088 // On X86, set the FP control word to 64-bits of precision instead of 80 bits.
1089 Out << "#if defined(__GNUC__) && !defined(__llvm__)\n"
Evan Chenge3db4da2006-06-20 22:11:12 +00001090 << "#if defined(i386) || defined(__i386__) || defined(__i386) || "
1091 << "defined(__x86_64__)\n"
Chris Lattner50a8a172005-05-06 06:58:42 +00001092 << "#undef CODE_FOR_MAIN\n"
1093 << "#define CODE_FOR_MAIN() \\\n"
1094 << " {short F;__asm__ (\"fnstcw %0\" : \"=m\" (*&F)); \\\n"
1095 << " F=(F&~0x300)|0x200;__asm__(\"fldcw %0\"::\"m\"(*&F));}\n"
1096 << "#endif\n#endif\n";
1097
Joel Stanleyecd78ac2003-02-12 20:45:00 +00001098}
1099
Chris Lattner9a571ba2006-03-07 22:58:23 +00001100/// FindStaticTors - Given a static ctor/dtor list, unpack its contents into
1101/// the StaticTors set.
1102static void FindStaticTors(GlobalVariable *GV, std::set<Function*> &StaticTors){
1103 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
1104 if (!InitList) return;
1105
1106 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
1107 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
1108 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
1109
1110 if (CS->getOperand(1)->isNullValue())
1111 return; // Found a null terminator, exit printing.
1112 Constant *FP = CS->getOperand(1);
1113 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
1114 if (CE->getOpcode() == Instruction::Cast)
1115 FP = CE->getOperand(0);
1116 if (Function *F = dyn_cast<Function>(FP))
1117 StaticTors.insert(F);
1118 }
1119}
1120
1121enum SpecialGlobalClass {
1122 NotSpecial = 0,
1123 GlobalCtors, GlobalDtors,
1124 NotPrinted
1125};
1126
1127/// getGlobalVariableClass - If this is a global that is specially recognized
1128/// by LLVM, return a code that indicates how we should handle it.
1129static SpecialGlobalClass getGlobalVariableClass(const GlobalVariable *GV) {
1130 // If this is a global ctors/dtors list, handle it now.
1131 if (GV->hasAppendingLinkage() && GV->use_empty()) {
1132 if (GV->getName() == "llvm.global_ctors")
1133 return GlobalCtors;
1134 else if (GV->getName() == "llvm.global_dtors")
1135 return GlobalDtors;
1136 }
1137
1138 // Otherwise, it it is other metadata, don't print it. This catches things
1139 // like debug information.
1140 if (GV->getSection() == "llvm.metadata")
1141 return NotPrinted;
1142
1143 return NotSpecial;
1144}
1145
1146
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001147bool CWriter::doInitialization(Module &M) {
1148 // Initialize
1149 TheModule = &M;
Chris Lattnerc2421432004-05-09 04:30:20 +00001150
1151 IL.AddPrototypes(M);
Misha Brukmand6a29a52005-04-20 16:05:03 +00001152
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001153 // Ensure that all structure types have names...
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001154 Mang = new Mangler(M);
Chris Lattnerba9c6432005-11-10 21:39:29 +00001155 Mang->markCharUnacceptable('.');
Chris Lattnerc59c1182003-11-16 22:06:14 +00001156
Chris Lattner9a571ba2006-03-07 22:58:23 +00001157 // Keep track of which functions are static ctors/dtors so they can have
1158 // an attribute added to their prototypes.
1159 std::set<Function*> StaticCtors, StaticDtors;
1160 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
1161 I != E; ++I) {
1162 switch (getGlobalVariableClass(I)) {
1163 default: break;
1164 case GlobalCtors:
1165 FindStaticTors(I, StaticCtors);
1166 break;
1167 case GlobalDtors:
1168 FindStaticTors(I, StaticDtors);
1169 break;
1170 }
1171 }
1172
Chris Lattner16c7bb22002-05-09 02:28:59 +00001173 // get declaration for alloca
Joel Stanleyecd78ac2003-02-12 20:45:00 +00001174 Out << "/* Provide Declarations */\n";
Chris Lattnerc59c1182003-11-16 22:06:14 +00001175 Out << "#include <stdarg.h>\n"; // Varargs support
1176 Out << "#include <setjmp.h>\n"; // Unwind support
Chris Lattner41488192003-06-28 17:15:12 +00001177 generateCompilerSpecificCode(Out);
Chris Lattnerc59c1182003-11-16 22:06:14 +00001178
Chris Lattnercf135cb2003-06-02 03:10:53 +00001179 // Provide a definition for `bool' if not compiling with a C++ compiler.
1180 Out << "\n"
Vikram S. Adve969c4ad2002-08-25 20:00:08 +00001181 << "#ifndef __cplusplus\ntypedef unsigned char bool;\n#endif\n"
Misha Brukmand6a29a52005-04-20 16:05:03 +00001182
Chris Lattnerd1cf1b42002-09-20 23:26:33 +00001183 << "\n\n/* Support for floating point constants */\n"
1184 << "typedef unsigned long long ConstantDoubleTy;\n"
Chris Lattnereb6f8c72002-11-07 19:43:59 +00001185 << "typedef unsigned int ConstantFloatTy;\n"
Misha Brukmand6a29a52005-04-20 16:05:03 +00001186
Chris Lattnera4d4a852002-08-19 21:48:40 +00001187 << "\n\n/* Global Declarations */\n";
1188
1189 // First output all the declarations for the program, because C requires
1190 // Functions & globals to be declared before they are used.
1191 //
Chris Lattner16c7bb22002-05-09 02:28:59 +00001192
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001193 // Loop over the symbol table, emitting all named constants...
Chris Lattner68758072004-05-09 06:20:51 +00001194 printModuleTypes(M.getSymbolTable());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001195
Chris Lattnera4d4a852002-08-19 21:48:40 +00001196 // Global variable declarations...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001197 if (!M.global_empty()) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +00001198 Out << "\n/* External Global Variable Declarations */\n";
Chris Lattnerf9a05322006-02-13 22:22:42 +00001199 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
1200 I != E; ++I) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +00001201 if (I->hasExternalLinkage()) {
1202 Out << "extern ";
Brian Gaeked9fb37a2003-07-24 20:20:44 +00001203 printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
Vikram S. Adve913bfbc2002-09-17 11:50:38 +00001204 Out << ";\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001205 } else if (I->hasDLLImportLinkage()) {
1206 Out << "__declspec(dllimport) ";
1207 printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
1208 Out << ";\n";
1209 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +00001210 }
Chris Lattnerdeed7a52002-05-09 15:18:52 +00001211 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001212
Chris Lattnera4d4a852002-08-19 21:48:40 +00001213 // Function declarations
Chris Lattner9a571ba2006-03-07 22:58:23 +00001214 Out << "\n/* Function Declarations */\n";
Chris Lattner57da2522005-08-23 20:22:50 +00001215 Out << "double fmod(double, double);\n"; // Support for FP rem
1216 Out << "float fmodf(float, float);\n";
1217
Chris Lattner9a571ba2006-03-07 22:58:23 +00001218 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1219 // Don't print declarations for intrinsic functions.
Reid Spencer2cb46e12006-10-22 09:58:21 +00001220 if (!I->getIntrinsicID() && I->getName() != "setjmp" &&
1221 I->getName() != "longjmp" && I->getName() != "_setjmp") {
Chris Lattner9a571ba2006-03-07 22:58:23 +00001222 printFunctionSignature(I, true);
1223 if (I->hasWeakLinkage() || I->hasLinkOnceLinkage())
1224 Out << " __ATTRIBUTE_WEAK__";
1225 if (StaticCtors.count(I))
1226 Out << " __ATTRIBUTE_CTOR__";
1227 if (StaticDtors.count(I))
1228 Out << " __ATTRIBUTE_DTOR__";
Chris Lattnerdacbe7b2006-07-28 20:58:47 +00001229
1230 if (I->hasName() && I->getName()[0] == 1)
1231 Out << " LLVM_ASM(\"" << I->getName().c_str()+1 << "\")";
1232
Chris Lattner9a571ba2006-03-07 22:58:23 +00001233 Out << ";\n";
Chris Lattner4cda8352002-08-20 16:55:48 +00001234 }
Chris Lattnera4d4a852002-08-19 21:48:40 +00001235 }
1236
Joel Stanley54f60322003-06-25 04:52:09 +00001237 // Output the global variable declarations
Chris Lattnere4d5c442005-03-15 04:54:21 +00001238 if (!M.global_empty()) {
Joel Stanley54f60322003-06-25 04:52:09 +00001239 Out << "\n\n/* Global Variable Declarations */\n";
Chris Lattnerf9a05322006-02-13 22:22:42 +00001240 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
1241 I != E; ++I)
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +00001242 if (!I->isExternal()) {
Chris Lattner9a571ba2006-03-07 22:58:23 +00001243 // Ignore special globals, such as debug info.
1244 if (getGlobalVariableClass(I))
1245 continue;
1246
Chris Lattnercc16a8e2004-12-03 17:19:10 +00001247 if (I->hasInternalLinkage())
1248 Out << "static ";
1249 else
1250 Out << "extern ";
Brian Gaeked9fb37a2003-07-24 20:20:44 +00001251 printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
Chris Lattner2580d4f2003-11-03 17:32:38 +00001252
1253 if (I->hasLinkOnceLinkage())
1254 Out << " __attribute__((common))";
1255 else if (I->hasWeakLinkage())
Brian Gaeke27f7a712003-12-11 00:24:36 +00001256 Out << " __ATTRIBUTE_WEAK__";
Nick Hildenbrandt50de36a2002-10-28 19:05:12 +00001257 Out << ";\n";
1258 }
1259 }
1260
Vikram S. Adve913bfbc2002-09-17 11:50:38 +00001261 // Output the global variable definitions and contents...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001262 if (!M.global_empty()) {
Vikram S. Adve913bfbc2002-09-17 11:50:38 +00001263 Out << "\n\n/* Global Variable Definitions and Initialization */\n";
Chris Lattnerf9a05322006-02-13 22:22:42 +00001264 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
1265 I != E; ++I)
Chris Lattnere8e035b2002-10-16 20:08:47 +00001266 if (!I->isExternal()) {
Chris Lattner9a571ba2006-03-07 22:58:23 +00001267 // Ignore special globals, such as debug info.
1268 if (getGlobalVariableClass(I))
1269 continue;
1270
Chris Lattnere8e035b2002-10-16 20:08:47 +00001271 if (I->hasInternalLinkage())
1272 Out << "static ";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001273 else if (I->hasDLLImportLinkage())
1274 Out << "__declspec(dllimport) ";
1275 else if (I->hasDLLExportLinkage())
1276 Out << "__declspec(dllexport) ";
1277
Brian Gaeked9fb37a2003-07-24 20:20:44 +00001278 printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
Chris Lattner76d9f1b2003-06-28 17:08:36 +00001279 if (I->hasLinkOnceLinkage())
1280 Out << " __attribute__((common))";
Chris Lattner72ac148d2003-10-16 18:29:00 +00001281 else if (I->hasWeakLinkage())
Brian Gaeke27f7a712003-12-11 00:24:36 +00001282 Out << " __ATTRIBUTE_WEAK__";
Chris Lattner5ea326a2003-11-03 17:35:00 +00001283
1284 // If the initializer is not null, emit the initializer. If it is null,
1285 // we try to avoid emitting large amounts of zeros. The problem with
1286 // this, however, occurs when the variable has weak linkage. In this
1287 // case, the assembler will complain about the variable being both weak
1288 // and common, so we disable this optimization.
Chris Lattnerf358c5a2004-02-20 05:49:22 +00001289 if (!I->getInitializer()->isNullValue()) {
Chris Lattner940b08d2003-06-06 07:10:24 +00001290 Out << " = " ;
1291 writeOperand(I->getInitializer());
Chris Lattnerf358c5a2004-02-20 05:49:22 +00001292 } else if (I->hasWeakLinkage()) {
1293 // We have to specify an initializer, but it doesn't have to be
1294 // complete. If the value is an aggregate, print out { 0 }, and let
1295 // the compiler figure out the rest of the zeros.
1296 Out << " = " ;
1297 if (isa<StructType>(I->getInitializer()->getType()) ||
Robert Bocchinob78e8382006-01-20 20:43:57 +00001298 isa<ArrayType>(I->getInitializer()->getType()) ||
1299 isa<PackedType>(I->getInitializer()->getType())) {
Chris Lattnerf358c5a2004-02-20 05:49:22 +00001300 Out << "{ 0 }";
1301 } else {
1302 // Just print it out normally.
1303 writeOperand(I->getInitializer());
1304 }
Chris Lattner940b08d2003-06-06 07:10:24 +00001305 }
Chris Lattnere8e035b2002-10-16 20:08:47 +00001306 Out << ";\n";
Chris Lattnera4d4a852002-08-19 21:48:40 +00001307 }
Chris Lattnera4d4a852002-08-19 21:48:40 +00001308 }
1309
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001310 if (!M.empty())
Chris Lattnera4d4a852002-08-19 21:48:40 +00001311 Out << "\n\n/* Function Bodies */\n";
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001312 return false;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001313}
1314
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001315
Chris Lattner9860e772003-10-12 04:36:29 +00001316/// Output all floating point constants that cannot be printed accurately...
Chris Lattner68758072004-05-09 06:20:51 +00001317void CWriter::printFloatingPointConstants(Function &F) {
Chris Lattner9860e772003-10-12 04:36:29 +00001318 // Scan the module for floating point constants. If any FP constant is used
1319 // in the function, we want to redirect it here so that we do not depend on
1320 // the precision of the printed form, unless the printed form preserves
1321 // precision.
1322 //
Chris Lattner68758072004-05-09 06:20:51 +00001323 static unsigned FPCounter = 0;
1324 for (constant_iterator I = constant_begin(&F), E = constant_end(&F);
1325 I != E; ++I)
1326 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(*I))
1327 if (!isFPCSafeToPrint(FPC) && // Do not put in FPConstantMap if safe.
1328 !FPConstantMap.count(FPC)) {
1329 double Val = FPC->getValue();
Misha Brukmand6a29a52005-04-20 16:05:03 +00001330
Chris Lattner68758072004-05-09 06:20:51 +00001331 FPConstantMap[FPC] = FPCounter; // Number the FP constants
Misha Brukmand6a29a52005-04-20 16:05:03 +00001332
Chris Lattner68758072004-05-09 06:20:51 +00001333 if (FPC->getType() == Type::DoubleTy) {
Chris Lattner68758072004-05-09 06:20:51 +00001334 Out << "static const ConstantDoubleTy FPConstant" << FPCounter++
Jim Laskeycb6682f2005-08-17 19:34:49 +00001335 << " = 0x" << std::hex << DoubleToBits(Val) << std::dec
Chris Lattner68758072004-05-09 06:20:51 +00001336 << "ULL; /* " << Val << " */\n";
1337 } else if (FPC->getType() == Type::FloatTy) {
Chris Lattner68758072004-05-09 06:20:51 +00001338 Out << "static const ConstantFloatTy FPConstant" << FPCounter++
Jim Laskeycb6682f2005-08-17 19:34:49 +00001339 << " = 0x" << std::hex << FloatToBits(Val) << std::dec
Chris Lattner68758072004-05-09 06:20:51 +00001340 << "U; /* " << Val << " */\n";
1341 } else
1342 assert(0 && "Unknown float type!");
1343 }
Misha Brukmand6a29a52005-04-20 16:05:03 +00001344
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001345 Out << '\n';
Chris Lattner68758072004-05-09 06:20:51 +00001346}
Chris Lattner9860e772003-10-12 04:36:29 +00001347
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001348
Chris Lattner2db41cd2002-09-20 15:12:13 +00001349/// printSymbolTable - Run through symbol table looking for type names. If a
Robert Bocchinob78e8382006-01-20 20:43:57 +00001350/// type name is found, emit its declaration...
Chris Lattnera4c047e2002-09-20 14:56:54 +00001351///
Chris Lattner68758072004-05-09 06:20:51 +00001352void CWriter::printModuleTypes(const SymbolTable &ST) {
Chris Lattnerb15fde22005-03-06 02:28:23 +00001353 // We are only interested in the type plane of the symbol table.
Reid Spencer9231ac82004-05-25 08:53:40 +00001354 SymbolTable::type_const_iterator I = ST.type_begin();
1355 SymbolTable::type_const_iterator End = ST.type_end();
Chris Lattnerb15fde22005-03-06 02:28:23 +00001356
1357 // If there are no type names, exit early.
1358 if (I == End) return;
Misha Brukmand6a29a52005-04-20 16:05:03 +00001359
Chris Lattner58d04d42002-09-20 15:18:30 +00001360 // Print out forward declarations for structure types before anything else!
1361 Out << "/* Structure forward decls */\n";
1362 for (; I != End; ++I)
Chris Lattner68758072004-05-09 06:20:51 +00001363 if (const Type *STy = dyn_cast<StructType>(I->second)) {
Chris Lattner36c975c2005-11-10 18:53:25 +00001364 std::string Name = "struct l_" + Mang->makeNameProper(I->first);
Chris Lattner68758072004-05-09 06:20:51 +00001365 Out << Name << ";\n";
1366 TypeNames.insert(std::make_pair(STy, Name));
1367 }
Chris Lattner58d04d42002-09-20 15:18:30 +00001368
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001369 Out << '\n';
Chris Lattner58d04d42002-09-20 15:18:30 +00001370
1371 // Now we can print out typedefs...
1372 Out << "/* Typedefs */\n";
Reid Spencer9231ac82004-05-25 08:53:40 +00001373 for (I = ST.type_begin(); I != End; ++I) {
Chris Lattner68758072004-05-09 06:20:51 +00001374 const Type *Ty = cast<Type>(I->second);
Chris Lattner36c975c2005-11-10 18:53:25 +00001375 std::string Name = "l_" + Mang->makeNameProper(I->first);
Chris Lattner68758072004-05-09 06:20:51 +00001376 Out << "typedef ";
1377 printType(Out, Ty, Name);
1378 Out << ";\n";
1379 }
Misha Brukmand6a29a52005-04-20 16:05:03 +00001380
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001381 Out << '\n';
Chris Lattner3ef6dc72002-05-09 14:40:11 +00001382
Chris Lattner2c601a72002-09-20 15:05:40 +00001383 // Keep track of which structures have been printed so far...
1384 std::set<const StructType *> StructPrinted;
1385
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +00001386 // Loop over all structures then push them into the stack so they are
1387 // printed in the correct order.
Chris Lattner2db41cd2002-09-20 15:12:13 +00001388 //
Chris Lattner58d04d42002-09-20 15:18:30 +00001389 Out << "/* Structure contents */\n";
Reid Spencer9231ac82004-05-25 08:53:40 +00001390 for (I = ST.type_begin(); I != End; ++I)
Chris Lattner2db41cd2002-09-20 15:12:13 +00001391 if (const StructType *STy = dyn_cast<StructType>(I->second))
Chris Lattnerfa395ec2003-11-02 01:29:27 +00001392 // Only print out used types!
Chris Lattner68758072004-05-09 06:20:51 +00001393 printContainedStructs(STy, StructPrinted);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001394}
1395
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +00001396// Push the struct onto the stack and recursively push all structs
1397// this one depends on.
Robert Bocchinob78e8382006-01-20 20:43:57 +00001398//
1399// TODO: Make this work properly with packed types
1400//
Chris Lattner2c601a72002-09-20 15:05:40 +00001401void CWriter::printContainedStructs(const Type *Ty,
1402 std::set<const StructType*> &StructPrinted){
Chris Lattner14d9b202006-01-20 18:57:03 +00001403 // Don't walk through pointers.
1404 if (isa<PointerType>(Ty) || Ty->isPrimitiveType()) return;
1405
1406 // Print all contained types first.
1407 for (Type::subtype_iterator I = Ty->subtype_begin(),
1408 E = Ty->subtype_end(); I != E; ++I)
1409 printContainedStructs(*I, StructPrinted);
1410
Misha Brukmanac25de32003-07-09 17:33:50 +00001411 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattner14d9b202006-01-20 18:57:03 +00001412 // Check to see if we have already printed this struct.
1413 if (StructPrinted.insert(STy).second) {
1414 // Print structure type out.
Misha Brukmand6a29a52005-04-20 16:05:03 +00001415 std::string Name = TypeNames[STy];
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001416 printType(Out, STy, Name, true);
Chris Lattner58d04d42002-09-20 15:18:30 +00001417 Out << ";\n\n";
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +00001418 }
Nick Hildenbrandt67aa2e22002-09-14 21:36:24 +00001419 }
1420}
1421
Chris Lattner4cda8352002-08-20 16:55:48 +00001422void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
Chris Lattner0c54d892006-05-23 23:39:48 +00001423 /// isCStructReturn - Should this function actually return a struct by-value?
1424 bool isCStructReturn = F->getCallingConv() == CallingConv::CSRet;
1425
Joel Stanley54f60322003-06-25 04:52:09 +00001426 if (F->hasInternalLinkage()) Out << "static ";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001427 if (F->hasDLLImportLinkage()) Out << "__declspec(dllimport) ";
1428 if (F->hasDLLExportLinkage()) Out << "__declspec(dllexport) ";
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001429 switch (F->getCallingConv()) {
1430 case CallingConv::X86_StdCall:
1431 Out << "__stdcall ";
1432 break;
1433 case CallingConv::X86_FastCall:
1434 Out << "__fastcall ";
1435 break;
1436 }
1437
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001438 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +00001439 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Misha Brukmand6a29a52005-04-20 16:05:03 +00001440
1441 std::stringstream FunctionInnards;
1442
Nick Hildenbrandt98502372002-11-18 20:55:50 +00001443 // Print out the name...
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001444 FunctionInnards << Mang->getValueName(F) << '(';
Misha Brukmand6a29a52005-04-20 16:05:03 +00001445
Chris Lattner0c54d892006-05-23 23:39:48 +00001446 bool PrintedArg = false;
Chris Lattner16c7bb22002-05-09 02:28:59 +00001447 if (!F->isExternal()) {
Chris Lattnere4d5c442005-03-15 04:54:21 +00001448 if (!F->arg_empty()) {
Chris Lattner0c54d892006-05-23 23:39:48 +00001449 Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1450
1451 // If this is a struct-return function, don't print the hidden
1452 // struct-return argument.
1453 if (isCStructReturn) {
1454 assert(I != E && "Invalid struct return function!");
1455 ++I;
1456 }
1457
Chris Lattneredd8ce12003-05-03 03:14:35 +00001458 std::string ArgName;
Chris Lattner0c54d892006-05-23 23:39:48 +00001459 for (; I != E; ++I) {
1460 if (PrintedArg) FunctionInnards << ", ";
Chris Lattner4cda8352002-08-20 16:55:48 +00001461 if (I->hasName() || !Prototype)
Brian Gaeked9fb37a2003-07-24 20:20:44 +00001462 ArgName = Mang->getValueName(I);
Misha Brukmand6a29a52005-04-20 16:05:03 +00001463 else
Chris Lattner4cda8352002-08-20 16:55:48 +00001464 ArgName = "";
Joel Stanley54f60322003-06-25 04:52:09 +00001465 printType(FunctionInnards, I->getType(), ArgName);
Chris Lattner0c54d892006-05-23 23:39:48 +00001466 PrintedArg = true;
Chris Lattnerdeed7a52002-05-09 15:18:52 +00001467 }
1468 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001469 } else {
Chris Lattner0c54d892006-05-23 23:39:48 +00001470 // Loop over the arguments, printing them.
1471 FunctionType::param_iterator I = FT->param_begin(), E = FT->param_end();
1472
1473 // If this is a struct-return function, don't print the hidden
1474 // struct-return argument.
1475 if (isCStructReturn) {
1476 assert(I != E && "Invalid struct return function!");
1477 ++I;
1478 }
1479
1480 for (; I != E; ++I) {
1481 if (PrintedArg) FunctionInnards << ", ";
Joel Stanley54f60322003-06-25 04:52:09 +00001482 printType(FunctionInnards, *I);
Chris Lattner0c54d892006-05-23 23:39:48 +00001483 PrintedArg = true;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001484 }
1485 }
1486
Chris Lattner1f8c4a12002-09-20 22:32:30 +00001487 // Finish printing arguments... if this is a vararg function, print the ...,
1488 // unless there are no known types, in which case, we just emit ().
1489 //
Chris Lattner0c54d892006-05-23 23:39:48 +00001490 if (FT->isVarArg() && PrintedArg) {
1491 if (PrintedArg) FunctionInnards << ", ";
Joel Stanley54f60322003-06-25 04:52:09 +00001492 FunctionInnards << "..."; // Output varargs portion of signature!
Chris Lattner0c54d892006-05-23 23:39:48 +00001493 } else if (!FT->isVarArg() && !PrintedArg) {
Chris Lattner4f7e1732003-11-26 00:09:17 +00001494 FunctionInnards << "void"; // ret() -> ret(void) in C.
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001495 }
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001496 FunctionInnards << ')';
Chris Lattner0c54d892006-05-23 23:39:48 +00001497
1498 // Get the return tpe for the function.
1499 const Type *RetTy;
1500 if (!isCStructReturn)
1501 RetTy = F->getReturnType();
1502 else {
1503 // If this is a struct-return function, print the struct-return type.
1504 RetTy = cast<PointerType>(FT->getParamType(0))->getElementType();
1505 }
1506
1507 // Print out the return type and the signature built above.
1508 printType(Out, RetTy, FunctionInnards.str());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001509}
1510
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001511void CWriter::printFunction(Function &F) {
1512 printFunctionSignature(&F, false);
Chris Lattner8c3c4bf2002-05-09 15:49:41 +00001513 Out << " {\n";
Chris Lattner0c54d892006-05-23 23:39:48 +00001514
1515 // If this is a struct return function, handle the result with magic.
1516 if (F.getCallingConv() == CallingConv::CSRet) {
1517 const Type *StructTy =
1518 cast<PointerType>(F.arg_begin()->getType())->getElementType();
1519 Out << " ";
1520 printType(Out, StructTy, "StructReturn");
1521 Out << "; /* Struct return temporary */\n";
Chris Lattner8c3c4bf2002-05-09 15:49:41 +00001522
Chris Lattner0c54d892006-05-23 23:39:48 +00001523 Out << " ";
1524 printType(Out, F.arg_begin()->getType(), Mang->getValueName(F.arg_begin()));
1525 Out << " = &StructReturn;\n";
1526 }
1527
1528 bool PrintedVar = false;
1529
Chris Lattner497e19a2002-05-09 20:39:03 +00001530 // print local variable information for the function
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001531 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I)
Chris Lattner6ffe5512004-04-27 15:13:33 +00001532 if (const AllocaInst *AI = isDirectAlloca(&*I)) {
Chris Lattnera8ab89e2003-06-17 04:39:14 +00001533 Out << " ";
Brian Gaeked9fb37a2003-07-24 20:20:44 +00001534 printType(Out, AI->getAllocatedType(), Mang->getValueName(AI));
Misha Brukman41ce39c2005-01-31 06:19:57 +00001535 Out << "; /* Address-exposed local */\n";
Chris Lattner0c54d892006-05-23 23:39:48 +00001536 PrintedVar = true;
Chris Lattner6ffe5512004-04-27 15:13:33 +00001537 } else if (I->getType() != Type::VoidTy && !isInlinableInst(*I)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +00001538 Out << " ";
Chris Lattner6ffe5512004-04-27 15:13:33 +00001539 printType(Out, I->getType(), Mang->getValueName(&*I));
Chris Lattner16c7bb22002-05-09 02:28:59 +00001540 Out << ";\n";
Misha Brukmand6a29a52005-04-20 16:05:03 +00001541
Chris Lattner84e66652003-05-03 07:11:00 +00001542 if (isa<PHINode>(*I)) { // Print out PHI node temporaries as well...
1543 Out << " ";
Chris Lattner6ffe5512004-04-27 15:13:33 +00001544 printType(Out, I->getType(),
1545 Mang->getValueName(&*I)+"__PHI_TEMPORARY");
Chris Lattner84e66652003-05-03 07:11:00 +00001546 Out << ";\n";
1547 }
Chris Lattner0c54d892006-05-23 23:39:48 +00001548 PrintedVar = true;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001549 }
Chris Lattnerd1cf1b42002-09-20 23:26:33 +00001550
Chris Lattner0c54d892006-05-23 23:39:48 +00001551 if (PrintedVar)
1552 Out << '\n';
Chris Lattnerd1cf1b42002-09-20 23:26:33 +00001553
Chris Lattner395fd592004-12-13 21:52:52 +00001554 if (F.hasExternalLinkage() && F.getName() == "main")
Chris Lattner50a8a172005-05-06 06:58:42 +00001555 Out << " CODE_FOR_MAIN();\n";
Chris Lattner395fd592004-12-13 21:52:52 +00001556
Chris Lattner16c7bb22002-05-09 02:28:59 +00001557 // print the basic blocks
Chris Lattner94f4f8e2004-02-13 23:00:29 +00001558 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattnercb3ad012004-05-09 20:41:32 +00001559 if (Loop *L = LI->getLoopFor(BB)) {
1560 if (L->getHeader() == BB && L->getParentLoop() == 0)
1561 printLoop(L);
1562 } else {
1563 printBasicBlock(BB);
Chris Lattnerd0c668c2002-05-09 21:18:38 +00001564 }
Chris Lattner8c3c4bf2002-05-09 15:49:41 +00001565 }
Misha Brukmand6a29a52005-04-20 16:05:03 +00001566
Chris Lattner8c3c4bf2002-05-09 15:49:41 +00001567 Out << "}\n\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001568}
1569
Chris Lattnercb3ad012004-05-09 20:41:32 +00001570void CWriter::printLoop(Loop *L) {
1571 Out << " do { /* Syntactic loop '" << L->getHeader()->getName()
1572 << "' to make GCC happy */\n";
1573 for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i) {
1574 BasicBlock *BB = L->getBlocks()[i];
1575 Loop *BBLoop = LI->getLoopFor(BB);
1576 if (BBLoop == L)
1577 printBasicBlock(BB);
1578 else if (BB == BBLoop->getHeader() && BBLoop->getParentLoop() == L)
Misha Brukmand6a29a52005-04-20 16:05:03 +00001579 printLoop(BBLoop);
Chris Lattnercb3ad012004-05-09 20:41:32 +00001580 }
1581 Out << " } while (1); /* end of syntactic loop '"
1582 << L->getHeader()->getName() << "' */\n";
1583}
1584
1585void CWriter::printBasicBlock(BasicBlock *BB) {
1586
1587 // Don't print the label for the basic block if there are no uses, or if
1588 // the only terminator use is the predecessor basic block's terminator.
1589 // We have to scan the use list because PHI nodes use basic blocks too but
1590 // do not require a label to be generated.
1591 //
1592 bool NeedsLabel = false;
1593 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
1594 if (isGotoCodeNecessary(*PI, BB)) {
1595 NeedsLabel = true;
1596 break;
1597 }
Misha Brukmand6a29a52005-04-20 16:05:03 +00001598
Chris Lattnercb3ad012004-05-09 20:41:32 +00001599 if (NeedsLabel) Out << Mang->getValueName(BB) << ":\n";
Misha Brukmand6a29a52005-04-20 16:05:03 +00001600
Chris Lattnercb3ad012004-05-09 20:41:32 +00001601 // Output all of the instructions in the basic block...
1602 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E;
1603 ++II) {
1604 if (!isInlinableInst(*II) && !isDirectAlloca(II)) {
1605 if (II->getType() != Type::VoidTy)
1606 outputLValue(II);
1607 else
1608 Out << " ";
1609 visit(*II);
1610 Out << ";\n";
1611 }
1612 }
Misha Brukmand6a29a52005-04-20 16:05:03 +00001613
Chris Lattnercb3ad012004-05-09 20:41:32 +00001614 // Don't emit prefix or suffix for the terminator...
1615 visit(*BB->getTerminator());
1616}
1617
1618
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001619// Specific Instruction type classes... note that all of the casts are
Misha Brukman5560c9d2003-08-18 14:43:39 +00001620// necessary because we use the instruction classes as opaque types...
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001621//
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001622void CWriter::visitReturnInst(ReturnInst &I) {
Chris Lattner0c54d892006-05-23 23:39:48 +00001623 // If this is a struct return function, return the temporary struct.
1624 if (I.getParent()->getParent()->getCallingConv() == CallingConv::CSRet) {
1625 Out << " return StructReturn;\n";
1626 return;
1627 }
1628
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001629 // Don't output a void return if this is the last basic block in the function
Misha Brukmand6a29a52005-04-20 16:05:03 +00001630 if (I.getNumOperands() == 0 &&
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001631 &*--I.getParent()->getParent()->end() == I.getParent() &&
1632 !I.getParent()->size() == 1) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001633 return;
Chris Lattner963b70b2002-05-21 18:05:19 +00001634 }
Chris Lattner44408262002-05-09 03:50:42 +00001635
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001636 Out << " return";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001637 if (I.getNumOperands()) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001638 Out << ' ';
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001639 writeOperand(I.getOperand(0));
Chris Lattner16c7bb22002-05-09 02:28:59 +00001640 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001641 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001642}
1643
Chris Lattnera9f5e052003-04-22 20:19:52 +00001644void CWriter::visitSwitchInst(SwitchInst &SI) {
Chris Lattnera05e0ec2004-05-09 03:42:48 +00001645
Chris Lattnera9f5e052003-04-22 20:19:52 +00001646 Out << " switch (";
1647 writeOperand(SI.getOperand(0));
Chris Lattnerf1acd962003-04-23 19:15:13 +00001648 Out << ") {\n default:\n";
John Criswell30cc2272004-10-25 18:30:09 +00001649 printPHICopiesForSuccessor (SI.getParent(), SI.getDefaultDest(), 2);
Chris Lattnerf1acd962003-04-23 19:15:13 +00001650 printBranchToBlock(SI.getParent(), SI.getDefaultDest(), 2);
Chris Lattnera9f5e052003-04-22 20:19:52 +00001651 Out << ";\n";
1652 for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) {
1653 Out << " case ";
1654 writeOperand(SI.getOperand(i));
1655 Out << ":\n";
1656 BasicBlock *Succ = cast<BasicBlock>(SI.getOperand(i+1));
John Criswell30cc2272004-10-25 18:30:09 +00001657 printPHICopiesForSuccessor (SI.getParent(), Succ, 2);
Chris Lattnera9f5e052003-04-22 20:19:52 +00001658 printBranchToBlock(SI.getParent(), Succ, 2);
Chris Lattnerefd02c72005-03-19 17:35:11 +00001659 if (Function::iterator(Succ) == next(Function::iterator(SI.getParent())))
Chris Lattnera9f5e052003-04-22 20:19:52 +00001660 Out << " break;\n";
1661 }
1662 Out << " }\n";
1663}
1664
Chris Lattnera9d12c02004-10-16 18:12:13 +00001665void CWriter::visitUnreachableInst(UnreachableInst &I) {
Chris Lattner963869e2004-10-17 23:49:11 +00001666 Out << " /*UNREACHABLE*/;\n";
Chris Lattnera9d12c02004-10-16 18:12:13 +00001667}
1668
Chris Lattnercb3ad012004-05-09 20:41:32 +00001669bool CWriter::isGotoCodeNecessary(BasicBlock *From, BasicBlock *To) {
1670 /// FIXME: This should be reenabled, but loop reordering safe!!
1671 return true;
1672
Chris Lattner67c2d182005-03-18 16:12:37 +00001673 if (next(Function::iterator(From)) != Function::iterator(To))
1674 return true; // Not the direct successor, we need a goto.
Chris Lattnercb3ad012004-05-09 20:41:32 +00001675
1676 //isa<SwitchInst>(From->getTerminator())
Chris Lattner9bda5f52003-06-28 17:53:05 +00001677
Chris Lattnercb3ad012004-05-09 20:41:32 +00001678 if (LI->getLoopFor(From) != LI->getLoopFor(To))
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001679 return true;
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001680 return false;
1681}
1682
John Criswell57bbfce2004-10-20 14:38:39 +00001683void CWriter::printPHICopiesForSuccessor (BasicBlock *CurBlock,
Misha Brukmand6a29a52005-04-20 16:05:03 +00001684 BasicBlock *Successor,
John Criswell57bbfce2004-10-20 14:38:39 +00001685 unsigned Indent) {
1686 for (BasicBlock::iterator I = Successor->begin(); isa<PHINode>(I); ++I) {
1687 PHINode *PN = cast<PHINode>(I);
1688 // Now we have to do the printing.
1689 Value *IV = PN->getIncomingValueForBlock(CurBlock);
1690 if (!isa<UndefValue>(IV)) {
1691 Out << std::string(Indent, ' ');
1692 Out << " " << Mang->getValueName(I) << "__PHI_TEMPORARY = ";
1693 writeOperand(IV);
1694 Out << "; /* for PHI node */\n";
1695 }
1696 }
1697}
1698
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001699void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
Chris Lattner9bda5f52003-06-28 17:53:05 +00001700 unsigned Indent) {
Chris Lattnercb3ad012004-05-09 20:41:32 +00001701 if (isGotoCodeNecessary(CurBB, Succ)) {
Chris Lattneredd8ce12003-05-03 03:14:35 +00001702 Out << std::string(Indent, ' ') << " goto ";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001703 writeOperand(Succ);
1704 Out << ";\n";
1705 }
1706}
1707
Misha Brukman37f92e22003-09-11 22:34:13 +00001708// Branch instruction printing - Avoid printing out a branch to a basic block
1709// that immediately succeeds the current one.
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001710//
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001711void CWriter::visitBranchInst(BranchInst &I) {
Chris Lattnera05e0ec2004-05-09 03:42:48 +00001712
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001713 if (I.isConditional()) {
Chris Lattner5ef6ffd2003-10-12 08:12:58 +00001714 if (isGotoCodeNecessary(I.getParent(), I.getSuccessor(0))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001715 Out << " if (";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001716 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001717 Out << ") {\n";
Misha Brukmand6a29a52005-04-20 16:05:03 +00001718
John Criswell57bbfce2004-10-20 14:38:39 +00001719 printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(0), 2);
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001720 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
Misha Brukmand6a29a52005-04-20 16:05:03 +00001721
Chris Lattner5ef6ffd2003-10-12 08:12:58 +00001722 if (isGotoCodeNecessary(I.getParent(), I.getSuccessor(1))) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001723 Out << " } else {\n";
John Criswell57bbfce2004-10-20 14:38:39 +00001724 printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(1), 2);
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001725 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001726 }
1727 } else {
Misha Brukman5560c9d2003-08-18 14:43:39 +00001728 // First goto not necessary, assume second one is...
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001729 Out << " if (!";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001730 writeOperand(I.getCondition());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001731 Out << ") {\n";
1732
John Criswell57bbfce2004-10-20 14:38:39 +00001733 printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(1), 2);
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001734 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001735 }
1736
1737 Out << " }\n";
1738 } else {
John Criswell57bbfce2004-10-20 14:38:39 +00001739 printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(0), 0);
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001740 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001741 }
1742 Out << "\n";
1743}
1744
Chris Lattner84e66652003-05-03 07:11:00 +00001745// PHI nodes get copied into temporary values at the end of predecessor basic
1746// blocks. We now need to copy these temporary values into the REAL value for
1747// the PHI.
1748void CWriter::visitPHINode(PHINode &I) {
1749 writeOperand(&I);
1750 Out << "__PHI_TEMPORARY";
1751}
1752
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001753
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001754void CWriter::visitBinaryOperator(Instruction &I) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001755 // binary instructions, shift instructions, setCond instructions.
Chris Lattnerf5612b762003-04-23 19:09:22 +00001756 assert(!isa<PointerType>(I.getType()));
Brian Gaeke031a1122003-06-23 20:00:51 +00001757
1758 // We must cast the results of binary operations which might be promoted.
1759 bool needsCast = false;
1760 if ((I.getType() == Type::UByteTy) || (I.getType() == Type::SByteTy)
Brian Gaekee99f4cf2003-06-25 03:05:33 +00001761 || (I.getType() == Type::UShortTy) || (I.getType() == Type::ShortTy)
1762 || (I.getType() == Type::FloatTy)) {
Brian Gaeke031a1122003-06-23 20:00:51 +00001763 needsCast = true;
1764 Out << "((";
Chris Lattner7635ea42003-11-03 01:01:59 +00001765 printType(Out, I.getType());
Brian Gaeke031a1122003-06-23 20:00:51 +00001766 Out << ")(";
1767 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001768
Chris Lattner6d9b69f2005-03-03 21:12:04 +00001769 // If this is a negation operation, print it out as such. For FP, we don't
1770 // want to print "-0.0 - X".
1771 if (BinaryOperator::isNeg(&I)) {
John Criswellce4e1e42005-07-14 19:41:16 +00001772 Out << "-(";
Chris Lattner6d9b69f2005-03-03 21:12:04 +00001773 writeOperand(BinaryOperator::getNegArgument(cast<BinaryOperator>(&I)));
John Criswellce4e1e42005-07-14 19:41:16 +00001774 Out << ")";
Chris Lattner57da2522005-08-23 20:22:50 +00001775 } else if (I.getOpcode() == Instruction::Rem &&
1776 I.getType()->isFloatingPoint()) {
1777 // Output a call to fmod/fmodf instead of emitting a%b
1778 if (I.getType() == Type::FloatTy)
1779 Out << "fmodf(";
1780 else
1781 Out << "fmod(";
1782 writeOperand(I.getOperand(0));
1783 Out << ", ";
1784 writeOperand(I.getOperand(1));
1785 Out << ")";
Chris Lattner6d9b69f2005-03-03 21:12:04 +00001786 } else {
Reid Spencer1628cec2006-10-26 06:15:43 +00001787
1788 // Write out the cast of the instruction's value back to the proper type
1789 // if necessary.
1790 bool NeedsClosingParens = writeInstructionCast(I);
1791
1792 // Certain instructions require the operand to be forced to a specific type
1793 // so we use writeOperandWithCast here instead of writeOperand. Similarly
1794 // below for operand 1
1795 writeOperandWithCast(I.getOperand(0), I.getOpcode());
Chris Lattner6d9b69f2005-03-03 21:12:04 +00001796
1797 switch (I.getOpcode()) {
1798 case Instruction::Add: Out << " + "; break;
1799 case Instruction::Sub: Out << " - "; break;
1800 case Instruction::Mul: Out << '*'; break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001801 case Instruction::UDiv:
1802 case Instruction::SDiv:
1803 case Instruction::FDiv: Out << '/'; break;
Chris Lattner6d9b69f2005-03-03 21:12:04 +00001804 case Instruction::Rem: Out << '%'; break;
1805 case Instruction::And: Out << " & "; break;
1806 case Instruction::Or: Out << " | "; break;
1807 case Instruction::Xor: Out << " ^ "; break;
1808 case Instruction::SetEQ: Out << " == "; break;
1809 case Instruction::SetNE: Out << " != "; break;
1810 case Instruction::SetLE: Out << " <= "; break;
1811 case Instruction::SetGE: Out << " >= "; break;
1812 case Instruction::SetLT: Out << " < "; break;
1813 case Instruction::SetGT: Out << " > "; break;
1814 case Instruction::Shl : Out << " << "; break;
1815 case Instruction::Shr : Out << " >> "; break;
1816 default: std::cerr << "Invalid operator type!" << I; abort();
1817 }
1818
Reid Spencer1628cec2006-10-26 06:15:43 +00001819 writeOperandWithCast(I.getOperand(1), I.getOpcode());
1820 if (NeedsClosingParens)
1821 Out << "))";
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001822 }
1823
Brian Gaeke031a1122003-06-23 20:00:51 +00001824 if (needsCast) {
1825 Out << "))";
1826 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001827}
1828
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001829void CWriter::visitCastInst(CastInst &I) {
Chris Lattnerd13bd222003-06-01 03:36:51 +00001830 if (I.getType() == Type::BoolTy) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001831 Out << '(';
Chris Lattnerd13bd222003-06-01 03:36:51 +00001832 writeOperand(I.getOperand(0));
1833 Out << " != 0)";
1834 return;
1835 }
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001836 Out << '(';
Chris Lattner7635ea42003-11-03 01:01:59 +00001837 printType(Out, I.getType());
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001838 Out << ')';
Chris Lattnerf5612b762003-04-23 19:09:22 +00001839 if (isa<PointerType>(I.getType())&&I.getOperand(0)->getType()->isIntegral() ||
1840 isa<PointerType>(I.getOperand(0)->getType())&&I.getType()->isIntegral()) {
1841 // Avoid "cast to pointer from integer of different size" warnings
Misha Brukmand6a29a52005-04-20 16:05:03 +00001842 Out << "(long)";
Chris Lattnerf5612b762003-04-23 19:09:22 +00001843 }
Misha Brukmand6a29a52005-04-20 16:05:03 +00001844
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001845 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00001846}
1847
Chris Lattner9f24a072004-03-12 05:52:14 +00001848void CWriter::visitSelectInst(SelectInst &I) {
1849 Out << "((";
1850 writeOperand(I.getCondition());
1851 Out << ") ? (";
1852 writeOperand(I.getTrueValue());
1853 Out << ") : (";
1854 writeOperand(I.getFalseValue());
Misha Brukmand6a29a52005-04-20 16:05:03 +00001855 Out << "))";
Chris Lattner9f24a072004-03-12 05:52:14 +00001856}
1857
1858
Chris Lattnerc2421432004-05-09 04:30:20 +00001859void CWriter::lowerIntrinsics(Function &F) {
1860 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1861 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1862 if (CallInst *CI = dyn_cast<CallInst>(I++))
1863 if (Function *F = CI->getCalledFunction())
1864 switch (F->getIntrinsicID()) {
1865 case Intrinsic::not_intrinsic:
1866 case Intrinsic::vastart:
1867 case Intrinsic::vacopy:
1868 case Intrinsic::vaend:
1869 case Intrinsic::returnaddress:
1870 case Intrinsic::frameaddress:
1871 case Intrinsic::setjmp:
1872 case Intrinsic::longjmp:
Chris Lattner33e9d292005-02-28 19:29:46 +00001873 case Intrinsic::prefetch:
Jim Laskey7075d6f2006-03-08 19:31:15 +00001874 case Intrinsic::dbg_stoppoint:
Chris Lattner1e142892006-09-09 06:17:12 +00001875 case Intrinsic::powi_f32:
1876 case Intrinsic::powi_f64:
Chris Lattnerc2421432004-05-09 04:30:20 +00001877 // We directly implement these intrinsics
1878 break;
1879 default:
Chris Lattner87242152006-03-13 23:09:05 +00001880 // If this is an intrinsic that directly corresponds to a GCC
1881 // builtin, we handle it.
1882 const char *BuiltinName = "";
1883#define GET_GCC_BUILTIN_NAME
1884#include "llvm/Intrinsics.gen"
1885#undef GET_GCC_BUILTIN_NAME
1886 // If we handle it, don't lower it.
1887 if (BuiltinName[0]) break;
1888
Chris Lattnerc2421432004-05-09 04:30:20 +00001889 // All other intrinsic calls we must lower.
Chris Lattner67c2d182005-03-18 16:12:37 +00001890 Instruction *Before = 0;
Misha Brukmand6a29a52005-04-20 16:05:03 +00001891 if (CI != &BB->front())
Chris Lattner67c2d182005-03-18 16:12:37 +00001892 Before = prior(BasicBlock::iterator(CI));
Misha Brukmand6a29a52005-04-20 16:05:03 +00001893
Chris Lattnerc2421432004-05-09 04:30:20 +00001894 IL.LowerIntrinsicCall(CI);
1895 if (Before) { // Move iterator to instruction after call
1896 I = Before; ++I;
1897 } else {
1898 I = BB->begin();
Chris Lattner4ef51372004-02-14 00:31:10 +00001899 }
Chris Lattner87242152006-03-13 23:09:05 +00001900 break;
Chris Lattnerc2421432004-05-09 04:30:20 +00001901 }
Chris Lattner4ef51372004-02-14 00:31:10 +00001902}
1903
1904
1905
Chris Lattnerbb03efd2002-06-25 15:57:03 +00001906void CWriter::visitCallInst(CallInst &I) {
Chris Lattner87242152006-03-13 23:09:05 +00001907 bool WroteCallee = false;
1908
Chris Lattner18ac3c82003-05-08 18:41:45 +00001909 // Handle intrinsic function calls first...
1910 if (Function *F = I.getCalledFunction())
Brian Gaeked0fde302003-11-11 22:41:34 +00001911 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
Chris Lattner18ac3c82003-05-08 18:41:45 +00001912 switch (ID) {
Chris Lattner87242152006-03-13 23:09:05 +00001913 default: {
1914 // If this is an intrinsic that directly corresponds to a GCC
1915 // builtin, we emit it here.
1916 const char *BuiltinName = "";
1917#define GET_GCC_BUILTIN_NAME
1918#include "llvm/Intrinsics.gen"
1919#undef GET_GCC_BUILTIN_NAME
1920 assert(BuiltinName[0] && "Unknown LLVM intrinsic!");
1921
1922 Out << BuiltinName;
1923 WroteCallee = true;
1924 break;
1925 }
Misha Brukmand6a29a52005-04-20 16:05:03 +00001926 case Intrinsic::vastart:
Chris Lattner4d45bd02003-10-18 05:57:43 +00001927 Out << "0; ";
Misha Brukmand6a29a52005-04-20 16:05:03 +00001928
Andrew Lenharth558bc882005-06-18 18:34:52 +00001929 Out << "va_start(*(va_list*)";
1930 writeOperand(I.getOperand(1));
1931 Out << ", ";
Chris Lattner18ac3c82003-05-08 18:41:45 +00001932 // Output the last argument to the enclosing function...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001933 if (I.getParent()->getParent()->arg_empty()) {
Chris Lattner4065ef92003-10-23 18:39:22 +00001934 std::cerr << "The C backend does not currently support zero "
1935 << "argument varargs functions, such as '"
1936 << I.getParent()->getParent()->getName() << "'!\n";
1937 abort();
1938 }
Chris Lattnerc5e7df12005-03-15 04:59:17 +00001939 writeOperand(--I.getParent()->getParent()->arg_end());
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001940 Out << ')';
Chris Lattner18ac3c82003-05-08 18:41:45 +00001941 return;
Chris Lattner317201d2004-03-13 00:24:00 +00001942 case Intrinsic::vaend:
Chris Lattnerf8e6f4f2004-08-10 00:19:16 +00001943 if (!isa<ConstantPointerNull>(I.getOperand(1))) {
Andrew Lenharth558bc882005-06-18 18:34:52 +00001944 Out << "0; va_end(*(va_list*)";
Chris Lattnerf8e6f4f2004-08-10 00:19:16 +00001945 writeOperand(I.getOperand(1));
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001946 Out << ')';
Chris Lattnerf8e6f4f2004-08-10 00:19:16 +00001947 } else {
1948 Out << "va_end(*(va_list*)0)";
1949 }
Chris Lattner18ac3c82003-05-08 18:41:45 +00001950 return;
Chris Lattner317201d2004-03-13 00:24:00 +00001951 case Intrinsic::vacopy:
Andrew Lenharth558bc882005-06-18 18:34:52 +00001952 Out << "0; ";
1953 Out << "va_copy(*(va_list*)";
Chris Lattner18ac3c82003-05-08 18:41:45 +00001954 writeOperand(I.getOperand(1));
Andrew Lenharth213e5572005-06-22 21:04:42 +00001955 Out << ", *(va_list*)";
Andrew Lenharth558bc882005-06-18 18:34:52 +00001956 writeOperand(I.getOperand(2));
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001957 Out << ')';
Chris Lattner18ac3c82003-05-08 18:41:45 +00001958 return;
Chris Lattnerfe1554a2004-02-14 02:55:36 +00001959 case Intrinsic::returnaddress:
1960 Out << "__builtin_return_address(";
1961 writeOperand(I.getOperand(1));
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001962 Out << ')';
Chris Lattnerfe1554a2004-02-14 02:55:36 +00001963 return;
1964 case Intrinsic::frameaddress:
1965 Out << "__builtin_frame_address(";
1966 writeOperand(I.getOperand(1));
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001967 Out << ')';
Chris Lattnerfe1554a2004-02-14 02:55:36 +00001968 return;
Chris Lattner1e142892006-09-09 06:17:12 +00001969 case Intrinsic::powi_f32:
1970 case Intrinsic::powi_f64:
1971 Out << "__builtin_powi(";
1972 writeOperand(I.getOperand(1));
1973 Out << ", ";
1974 writeOperand(I.getOperand(2));
1975 Out << ')';
1976 return;
Chris Lattnere42cde22004-02-15 22:51:47 +00001977 case Intrinsic::setjmp:
Chris Lattner475c5532006-06-06 21:45:47 +00001978#if defined(HAVE__SETJMP) && defined(HAVE__LONGJMP)
1979 Out << "_"; // Use _setjmp on systems that support it!
1980#endif
Chris Lattnere42cde22004-02-15 22:51:47 +00001981 Out << "setjmp(*(jmp_buf*)";
1982 writeOperand(I.getOperand(1));
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001983 Out << ')';
Chris Lattnere42cde22004-02-15 22:51:47 +00001984 return;
1985 case Intrinsic::longjmp:
Chris Lattner475c5532006-06-06 21:45:47 +00001986#if defined(HAVE__SETJMP) && defined(HAVE__LONGJMP)
1987 Out << "_"; // Use _longjmp on systems that support it!
1988#endif
Chris Lattnere42cde22004-02-15 22:51:47 +00001989 Out << "longjmp(*(jmp_buf*)";
1990 writeOperand(I.getOperand(1));
1991 Out << ", ";
1992 writeOperand(I.getOperand(2));
Misha Brukmanb70aaa62005-02-14 18:52:35 +00001993 Out << ')';
Chris Lattnere42cde22004-02-15 22:51:47 +00001994 return;
Chris Lattner33e9d292005-02-28 19:29:46 +00001995 case Intrinsic::prefetch:
Chris Lattnerca76f352005-02-28 19:36:15 +00001996 Out << "LLVM_PREFETCH((const void *)";
Chris Lattner33e9d292005-02-28 19:29:46 +00001997 writeOperand(I.getOperand(1));
1998 Out << ", ";
1999 writeOperand(I.getOperand(2));
2000 Out << ", ";
2001 writeOperand(I.getOperand(3));
2002 Out << ")";
Chris Lattnerca76f352005-02-28 19:36:15 +00002003 return;
Jim Laskey7075d6f2006-03-08 19:31:15 +00002004 case Intrinsic::dbg_stoppoint: {
2005 // If we use writeOperand directly we get a "u" suffix which is rejected
2006 // by gcc.
Jim Laskey580418e2006-03-23 18:08:29 +00002007 DbgStopPointInst &SPI = cast<DbgStopPointInst>(I);
Jim Laskey7075d6f2006-03-08 19:31:15 +00002008
2009 Out << "\n#line "
Jim Laskey580418e2006-03-23 18:08:29 +00002010 << SPI.getLine()
2011 << " \"" << SPI.getDirectory()
2012 << SPI.getFileName() << "\"\n";
Jim Laskey7075d6f2006-03-08 19:31:15 +00002013 return;
2014 }
Chris Lattner18ac3c82003-05-08 18:41:45 +00002015 }
2016 }
2017
Chris Lattner4394d512004-11-13 22:21:56 +00002018 Value *Callee = I.getCalledValue();
Misha Brukmand6a29a52005-04-20 16:05:03 +00002019
Chris Lattner0c54d892006-05-23 23:39:48 +00002020 // If this is a call to a struct-return function, assign to the first
2021 // parameter instead of passing it to the call.
2022 bool isStructRet = I.getCallingConv() == CallingConv::CSRet;
2023 if (isStructRet) {
2024 Out << "*(";
2025 writeOperand(I.getOperand(1));
2026 Out << ") = ";
2027 }
2028
Chris Lattnerfe673d92005-05-06 06:53:07 +00002029 if (I.isTailCall()) Out << " /*tail*/ ";
Chris Lattner4394d512004-11-13 22:21:56 +00002030
2031 const PointerType *PTy = cast<PointerType>(Callee->getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002032 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
Chris Lattner0c54d892006-05-23 23:39:48 +00002033
2034 if (!WroteCallee) {
2035 // If this is an indirect call to a struct return function, we need to cast
2036 // the pointer.
2037 bool NeedsCast = isStructRet && !isa<Function>(Callee);
Misha Brukmand6a29a52005-04-20 16:05:03 +00002038
Chris Lattner0c54d892006-05-23 23:39:48 +00002039 // GCC is a real PITA. It does not permit codegening casts of functions to
2040 // function pointers if they are in a call (it generates a trap instruction
2041 // instead!). We work around this by inserting a cast to void* in between
2042 // the function and the function pointer cast. Unfortunately, we can't just
2043 // form the constant expression here, because the folder will immediately
2044 // nuke it.
2045 //
2046 // Note finally, that this is completely unsafe. ANSI C does not guarantee
2047 // that void* and function pointers have the same size. :( To deal with this
2048 // in the common case, we handle casts where the number of arguments passed
2049 // match exactly.
2050 //
2051 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Callee))
2052 if (CE->getOpcode() == Instruction::Cast)
2053 if (Function *RF = dyn_cast<Function>(CE->getOperand(0))) {
2054 NeedsCast = true;
2055 Callee = RF;
2056 }
2057
2058 if (NeedsCast) {
2059 // Ok, just cast the pointer type.
2060 Out << "((";
2061 if (!isStructRet)
2062 printType(Out, I.getCalledValue()->getType());
2063 else
2064 printStructReturnPointerFunctionType(Out,
2065 cast<PointerType>(I.getCalledValue()->getType()));
2066 Out << ")(void*)";
2067 }
2068 writeOperand(Callee);
2069 if (NeedsCast) Out << ')';
2070 }
2071
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002072 Out << '(';
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002073
Chris Lattner4394d512004-11-13 22:21:56 +00002074 unsigned NumDeclaredParams = FTy->getNumParams();
2075
Chris Lattner0c54d892006-05-23 23:39:48 +00002076 CallSite::arg_iterator AI = I.op_begin()+1, AE = I.op_end();
2077 unsigned ArgNo = 0;
2078 if (isStructRet) { // Skip struct return argument.
2079 ++AI;
2080 ++ArgNo;
2081 }
2082
2083 bool PrintedArg = false;
2084 for (; AI != AE; ++AI, ++ArgNo) {
2085 if (PrintedArg) Out << ", ";
2086 if (ArgNo < NumDeclaredParams &&
2087 (*AI)->getType() != FTy->getParamType(ArgNo)) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002088 Out << '(';
Chris Lattner0c54d892006-05-23 23:39:48 +00002089 printType(Out, FTy->getParamType(ArgNo));
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002090 Out << ')';
Chris Lattner4394d512004-11-13 22:21:56 +00002091 }
Chris Lattner9bda5f52003-06-28 17:53:05 +00002092 writeOperand(*AI);
Chris Lattner0c54d892006-05-23 23:39:48 +00002093 PrintedArg = true;
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002094 }
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002095 Out << ')';
Misha Brukmand6a29a52005-04-20 16:05:03 +00002096}
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002097
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002098void CWriter::visitMallocInst(MallocInst &I) {
Chris Lattnerf31182a2004-02-13 23:18:48 +00002099 assert(0 && "lowerallocations pass didn't work!");
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002100}
2101
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002102void CWriter::visitAllocaInst(AllocaInst &I) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002103 Out << '(';
Nick Hildenbrandt98502372002-11-18 20:55:50 +00002104 printType(Out, I.getType());
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002105 Out << ") alloca(sizeof(";
Nick Hildenbrandt98502372002-11-18 20:55:50 +00002106 printType(Out, I.getType()->getElementType());
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002107 Out << ')';
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002108 if (I.isArrayAllocation()) {
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002109 Out << " * " ;
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002110 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002111 }
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002112 Out << ')';
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002113}
2114
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002115void CWriter::visitFreeInst(FreeInst &I) {
Chris Lattnerf31182a2004-02-13 23:18:48 +00002116 assert(0 && "lowerallocations pass didn't work!");
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002117}
2118
Chris Lattner23e90702003-11-25 20:49:55 +00002119void CWriter::printIndexingExpression(Value *Ptr, gep_type_iterator I,
2120 gep_type_iterator E) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00002121 bool HasImplicitAddress = false;
2122 // If accessing a global value with no indexing, avoid *(&GV) syndrome
2123 if (GlobalValue *V = dyn_cast<GlobalValue>(Ptr)) {
2124 HasImplicitAddress = true;
Chris Lattnera8ab89e2003-06-17 04:39:14 +00002125 } else if (isDirectAlloca(Ptr)) {
2126 HasImplicitAddress = true;
Chris Lattnerd0c668c2002-05-09 21:18:38 +00002127 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002128
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00002129 if (I == E) {
2130 if (!HasImplicitAddress)
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002131 Out << '*'; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002132
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00002133 writeOperandInternal(Ptr);
2134 return;
2135 }
2136
Chris Lattner23e90702003-11-25 20:49:55 +00002137 const Constant *CI = dyn_cast<Constant>(I.getOperand());
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00002138 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
2139 Out << "(&";
2140
2141 writeOperandInternal(Ptr);
2142
Nick Hildenbrandte548f002002-09-25 20:29:26 +00002143 if (HasImplicitAddress && (!CI || !CI->isNullValue())) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002144 Out << ')';
Nick Hildenbrandte548f002002-09-25 20:29:26 +00002145 HasImplicitAddress = false; // HIA is only true if we haven't addressed yet
2146 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002147
Nick Hildenbrandte548f002002-09-25 20:29:26 +00002148 assert(!HasImplicitAddress || (CI && CI->isNullValue()) &&
2149 "Can only have implicit address with direct accessing");
2150
2151 if (HasImplicitAddress) {
2152 ++I;
Chris Lattner23e90702003-11-25 20:49:55 +00002153 } else if (CI && CI->isNullValue()) {
2154 gep_type_iterator TmpI = I; ++TmpI;
2155
Nick Hildenbrandte548f002002-09-25 20:29:26 +00002156 // Print out the -> operator if possible...
Chris Lattner23e90702003-11-25 20:49:55 +00002157 if (TmpI != E && isa<StructType>(*TmpI)) {
Chris Lattnerdf35a1c2002-08-19 23:09:46 +00002158 Out << (HasImplicitAddress ? "." : "->");
Reid Spencerb83eb642006-10-20 07:07:24 +00002159 Out << "field" << cast<ConstantInt>(TmpI.getOperand())->getZExtValue();
Chris Lattner23e90702003-11-25 20:49:55 +00002160 I = ++TmpI;
2161 }
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002162 }
Chris Lattner5dfe7672002-08-22 22:48:55 +00002163
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002164 for (; I != E; ++I)
Chris Lattner23e90702003-11-25 20:49:55 +00002165 if (isa<StructType>(*I)) {
Reid Spencerb83eb642006-10-20 07:07:24 +00002166 Out << ".field" << cast<ConstantInt>(I.getOperand())->getZExtValue();
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002167 } else {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002168 Out << '[';
Chris Lattner23e90702003-11-25 20:49:55 +00002169 writeOperand(I.getOperand());
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002170 Out << ']';
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002171 }
2172}
2173
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002174void CWriter::visitLoadInst(LoadInst &I) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002175 Out << '*';
Chris Lattner9d30e222005-02-14 16:47:52 +00002176 if (I.isVolatile()) {
Chris Lattner8399e022005-02-15 05:52:14 +00002177 Out << "((";
Chris Lattnerb94388a2005-09-27 20:52:44 +00002178 printType(Out, I.getType(), "volatile*");
2179 Out << ")";
Chris Lattner9d30e222005-02-14 16:47:52 +00002180 }
2181
Chris Lattner5dfe7672002-08-22 22:48:55 +00002182 writeOperand(I.getOperand(0));
Chris Lattner9d30e222005-02-14 16:47:52 +00002183
2184 if (I.isVolatile())
Misha Brukman23ba0c52005-03-08 00:26:08 +00002185 Out << ')';
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002186}
2187
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002188void CWriter::visitStoreInst(StoreInst &I) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002189 Out << '*';
Chris Lattner9d30e222005-02-14 16:47:52 +00002190 if (I.isVolatile()) {
Chris Lattner8399e022005-02-15 05:52:14 +00002191 Out << "((";
Chris Lattnerb94388a2005-09-27 20:52:44 +00002192 printType(Out, I.getOperand(0)->getType(), " volatile*");
2193 Out << ")";
Chris Lattner9d30e222005-02-14 16:47:52 +00002194 }
Chris Lattner5dfe7672002-08-22 22:48:55 +00002195 writeOperand(I.getPointerOperand());
Misha Brukman23ba0c52005-03-08 00:26:08 +00002196 if (I.isVolatile()) Out << ')';
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002197 Out << " = ";
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002198 writeOperand(I.getOperand(0));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002199}
2200
Chris Lattnerbb03efd2002-06-25 15:57:03 +00002201void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
Misha Brukmanb70aaa62005-02-14 18:52:35 +00002202 Out << '&';
Chris Lattner23e90702003-11-25 20:49:55 +00002203 printIndexingExpression(I.getPointerOperand(), gep_type_begin(I),
2204 gep_type_end(I));
Chris Lattner4fbf26d2002-05-09 20:53:56 +00002205}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00002206
Chris Lattner4d45bd02003-10-18 05:57:43 +00002207void CWriter::visitVAArgInst(VAArgInst &I) {
Andrew Lenharth558bc882005-06-18 18:34:52 +00002208 Out << "va_arg(*(va_list*)";
Chris Lattner4d45bd02003-10-18 05:57:43 +00002209 writeOperand(I.getOperand(0));
Andrew Lenharth558bc882005-06-18 18:34:52 +00002210 Out << ", ";
Chris Lattner7635ea42003-11-03 01:01:59 +00002211 printType(Out, I.getType());
Andrew Lenharth558bc882005-06-18 18:34:52 +00002212 Out << ");\n ";
Chris Lattner4d45bd02003-10-18 05:57:43 +00002213}
2214
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00002215//===----------------------------------------------------------------------===//
2216// External Interface declaration
2217//===----------------------------------------------------------------------===//
2218
Chris Lattner1911fd42006-09-04 04:14:57 +00002219bool CTargetMachine::addPassesToEmitWholeFile(PassManager &PM,
2220 std::ostream &o,
2221 CodeGenFileType FileType,
2222 bool Fast) {
Chris Lattner0431c962005-06-25 02:48:37 +00002223 if (FileType != TargetMachine::AssemblyFile) return true;
2224
Chris Lattner99c59e82004-05-23 21:23:35 +00002225 PM.add(createLowerGCPass());
Chris Lattnerf7fe4942005-03-03 01:04:50 +00002226 PM.add(createLowerAllocationsPass(true));
Chris Lattner94f4f8e2004-02-13 23:00:29 +00002227 PM.add(createLowerInvokePass());
Chris Lattnerbad13eb2005-11-02 17:42:58 +00002228 PM.add(createCFGSimplificationPass()); // clean up after lower invoke.
Chris Lattnerf9a05322006-02-13 22:22:42 +00002229 PM.add(new CBackendNameAllUsedStructsAndMergeFunctions());
Chris Lattnerbc641b92006-03-23 05:43:16 +00002230 PM.add(new CWriter(o));
Chris Lattnerf31182a2004-02-13 23:18:48 +00002231 return false;
2232}