//===----------------------------------------------------------------------===//
#include "CTargetMachine.h"
-#include "llvm/Target/TargetMachineImpls.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Module.h"
#include "llvm/PassManager.h"
#include "llvm/SymbolTable.h"
#include "llvm/Intrinsics.h"
-#include "llvm/IntrinsicLowering.h"
#include "llvm/Analysis/ConstantsScanner.h"
#include "llvm/Analysis/FindUsedTypes.h"
#include "llvm/Analysis/LoopInfo.h"
+#include "llvm/CodeGen/IntrinsicLowering.h"
#include "llvm/Transforms/Scalar.h"
+#include "llvm/Target/TargetMachineRegistry.h"
#include "llvm/Support/CallSite.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/GetElementPtrTypeIterator.h"
#include "llvm/Support/InstVisitor.h"
#include "llvm/Support/Mangler.h"
-#include "Support/StringExtras.h"
-#include "Config/config.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/Support/MathExtras.h"
+#include "llvm/Config/config.h"
#include <algorithm>
+#include <iostream>
#include <sstream>
using namespace llvm;
namespace {
+ // Register the target.
+ RegisterTarget<CTargetMachine> X("c", " C backend");
+
/// NameAllUsedStructs - This pass inserts names for any unnamed structure
/// types that are used by the program.
///
for (SymbolTable::type_iterator TI = MST.type_begin(), TE = MST.type_end();
TI != TE; ) {
SymbolTable::type_iterator I = TI++;
- if (StructType *STy = dyn_cast<StructType>(I->second)) {
+ if (const StructType *STy = dyn_cast<StructType>(I->second)) {
// If this is not used, remove it from the symbol table.
std::set<const Type *>::iterator UTI = UT.find(STy);
if (UTI == UT.end())
- MST.remove(I->first, (Type*)I->second);
+ MST.remove(I);
else
UT.erase(UTI);
}
// structure types.
//
bool Changed = false;
+ unsigned RenameCounter = 0;
for (std::set<const Type *>::const_iterator I = UT.begin(), E = UT.end();
I != E; ++I)
if (const StructType *ST = dyn_cast<StructType>(*I)) {
- ((Value*)ST)->setName("unnamed", &MST);
+ while (M.addTypeName("unnamed"+utostr(RenameCounter), ST))
+ ++RenameCounter;
Changed = true;
}
return Changed;
const std::string &NameSoFar,
bool IgnoreName) {
if (Ty->isPrimitiveType())
- switch (Ty->getPrimitiveID()) {
+ switch (Ty->getTypeID()) {
case Type::VoidTyID: return Out << "void " << NameSoFar;
case Type::BoolTyID: return Out << "bool " << NameSoFar;
case Type::UByteTyID: return Out << "unsigned char " << NameSoFar;
case Type::FloatTyID: return Out << "float " << NameSoFar;
case Type::DoubleTyID: return Out << "double " << NameSoFar;
default :
- std::cerr << "Unknown primitive type: " << Ty << "\n";
+ std::cerr << "Unknown primitive type: " << *Ty << "\n";
abort();
}
if (I != TypeNames.end()) return Out << I->second << " " << NameSoFar;
}
- switch (Ty->getPrimitiveID()) {
+ switch (Ty->getTypeID()) {
case Type::FunctionTyID: {
const FunctionType *MTy = cast<FunctionType>(Ty);
std::stringstream FunctionInnards;
default:
std::cerr << "CWriter Error: Unhandled constant expression: "
- << CE << "\n";
+ << *CE << "\n";
abort();
}
}
- switch (CPV->getType()->getPrimitiveID()) {
+ switch (CPV->getType()->getTypeID()) {
case Type::BoolTyID:
Out << (CPV == ConstantBool::False ? "0" : "1"); break;
case Type::SByteTyID:
Out << "(*(" << (FPC->getType() == Type::FloatTy ? "float" : "double")
<< "*)&FPConstant" << I->second << ")";
} else {
+ if (IsNAN(FPC->getValue())) {
+ // The value is NaN
+
+ // The prefix for a quiet NaN is 0x7FF8. For a signalling NaN,
+ // it's 0x7ff4.
+ const unsigned long QuietNaN = 0x7ff8UL;
+ const unsigned long SignalNaN = 0x7ff4UL;
+
+ // We need to grab the first part of the FP #
+ union {
+ double d;
+ uint64_t ll;
+ } DHex;
+ char Buffer[100];
+
+ DHex.d = FPC->getValue();
+ sprintf(Buffer, "0x%llx", DHex.ll);
+
+ std::string Num(&Buffer[0], &Buffer[6]);
+ unsigned long Val = strtoul(Num.c_str(), 0, 16);
+
+ if (FPC->getType() == Type::FloatTy)
+ Out << "LLVM_NAN" << (Val == QuietNaN ? "" : "S") << "F(\""
+ << Buffer << "\") /*nan*/ ";
+ else
+ Out << "LLVM_NAN" << (Val == QuietNaN ? "" : "S") << "(\""
+ << Buffer << "\") /*nan*/ ";
+ } else if (IsInf(FPC->getValue())) {
+ // The value is Inf
+ if (FPC->getValue() < 0) Out << "-";
+ Out << "LLVM_INF" << (FPC->getType() == Type::FloatTy ? "F" : "")
+ << " /*inf*/ ";
+ } else {
+ std::string Num;
#if HAVE_PRINTF_A
- // Print out the constant as a floating point number.
- char Buffer[100];
- sprintf(Buffer, "%a", FPC->getValue());
- Out << Buffer << " /*" << FPC->getValue() << "*/ ";
+ // Print out the constant as a floating point number.
+ char Buffer[100];
+ sprintf(Buffer, "%a", FPC->getValue());
+ Num = Buffer;
#else
- Out << ftostr(FPC->getValue());
+ Num = ftostr(FPC->getValue());
#endif
+ Out << Num;
+ }
}
break;
}
printType(Out, CPV->getType());
Out << ")/*NULL*/0)";
break;
- } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
- writeOperand(CPR->getValue());
+ } else if (GlobalValue *GV = dyn_cast<GlobalValue>(CPV)) {
+ writeOperand(GV);
break;
}
// FALL THROUGH
default:
- std::cerr << "Unknown constant type: " << CPV << "\n";
+ std::cerr << "Unknown constant type: " << *CPV << "\n";
abort();
}
}
return;
}
- if (Constant *CPV = dyn_cast<Constant>(Operand)) {
+ Constant* CPV = dyn_cast<Constant>(Operand);
+ if (CPV && !isa<GlobalValue>(CPV)) {
printConstant(CPV);
} else {
Out << Mang->getValueName(Operand);
static void generateCompilerSpecificCode(std::ostream& Out) {
// Alloca is hard to get, and we don't want to include stdlib.h here...
Out << "/* get a declaration for alloca */\n"
- << "#ifdef sun\n"
+ << "#if defined(sun) || defined(__CYGWIN__) || defined(__APPLE__)\n"
<< "extern void *__builtin_alloca(unsigned long);\n"
<< "#define alloca(x) __builtin_alloca(x)\n"
<< "#else\n"
<< "#else\n"
<< "#define __ATTRIBUTE_WEAK__\n"
<< "#endif\n\n";
+
+ // Define NaN and Inf as GCC builtins if using GCC, as 0 otherwise
+ // From the GCC documentation:
+ //
+ // double __builtin_nan (const char *str)
+ //
+ // This is an implementation of the ISO C99 function nan.
+ //
+ // Since ISO C99 defines this function in terms of strtod, which we do
+ // not implement, a description of the parsing is in order. The string is
+ // parsed as by strtol; that is, the base is recognized by leading 0 or
+ // 0x prefixes. The number parsed is placed in the significand such that
+ // the least significant bit of the number is at the least significant
+ // bit of the significand. The number is truncated to fit the significand
+ // field provided. The significand is forced to be a quiet NaN.
+ //
+ // This function, if given a string literal, is evaluated early enough
+ // that it is considered a compile-time constant.
+ //
+ // float __builtin_nanf (const char *str)
+ //
+ // Similar to __builtin_nan, except the return type is float.
+ //
+ // double __builtin_inf (void)
+ //
+ // Similar to __builtin_huge_val, except a warning is generated if the
+ // target floating-point format does not support infinities. This
+ // function is suitable for implementing the ISO C99 macro INFINITY.
+ //
+ // float __builtin_inff (void)
+ //
+ // Similar to __builtin_inf, except the return type is float.
+ Out << "#ifdef __GNUC__\n"
+ << "#define LLVM_NAN(NanStr) __builtin_nan(NanStr) /* Double */\n"
+ << "#define LLVM_NANF(NanStr) __builtin_nanf(NanStr) /* Float */\n"
+ << "#define LLVM_NANS(NanStr) __builtin_nans(NanStr) /* Double */\n"
+ << "#define LLVM_NANSF(NanStr) __builtin_nansf(NanStr) /* Float */\n"
+ << "#define LLVM_INF __builtin_inf() /* Double */\n"
+ << "#define LLVM_INFF __builtin_inff() /* Float */\n"
+ << "#else\n"
+ << "#define LLVM_NAN(NanStr) ((double)0.0) /* Double */\n"
+ << "#define LLVM_NANF(NanStr) 0.0F /* Float */\n"
+ << "#define LLVM_NANS(NanStr) ((double)0.0) /* Double */\n"
+ << "#define LLVM_NANSF(NanStr) 0.0F /* Float */\n"
+ << "#define LLVM_INF ((double)0.0) /* Double */\n"
+ << "#define LLVM_INFF 0.0F /* Float */\n"
+ << "#endif\n";
}
bool CWriter::doInitialization(Module &M) {
Out << ")";
return;
case Intrinsic::vaend:
- Out << "va_end(*(va_list*)&";
- writeOperand(I.getOperand(1));
- Out << ")";
+ if (!isa<ConstantPointerNull>(I.getOperand(1))) {
+ Out << "va_end(*(va_list*)&";
+ writeOperand(I.getOperand(1));
+ Out << ")";
+ } else {
+ Out << "va_end(*(va_list*)0)";
+ }
return;
case Intrinsic::vacopy:
Out << "0;";
// If accessing a global value with no indexing, avoid *(&GV) syndrome
if (GlobalValue *V = dyn_cast<GlobalValue>(Ptr)) {
HasImplicitAddress = true;
- } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Ptr)) {
- HasImplicitAddress = true;
- Ptr = CPR->getValue(); // Get to the global...
} else if (isDirectAlloca(Ptr)) {
HasImplicitAddress = true;
}
return false;
}
-TargetMachine *llvm::allocateCTargetMachine(const Module &M,
- IntrinsicLowering *IL) {
- return new CTargetMachine(M, IL);
-}
-
// vim: sw=2