2007-08-20 22:05:56 +04:00
|
|
|
//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Anders Carlsson and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit Builtin calls as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
#include "CodeGenModule.h"
|
2007-08-31 08:31:45 +04:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2007-08-20 22:05:56 +04:00
|
|
|
#include "clang/AST/Builtins.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
2007-08-26 08:17:05 +04:00
|
|
|
#include "llvm/Constants.h"
|
2007-08-31 08:44:06 +04:00
|
|
|
#include "llvm/Function.h"
|
2007-10-13 03:56:29 +04:00
|
|
|
#include "llvm/Intrinsics.h"
|
|
|
|
|
2007-08-20 22:05:56 +04:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2007-08-26 08:17:05 +04:00
|
|
|
RValue CodeGenFunction::EmitBuiltinExpr(unsigned BuiltinID, const CallExpr *E) {
|
|
|
|
switch (BuiltinID) {
|
|
|
|
default:
|
2007-08-31 08:44:06 +04:00
|
|
|
if (getContext().BuiltinInfo.isLibFunction(BuiltinID))
|
|
|
|
return EmitCallExpr(CGM.getBuiltinLibFunction(BuiltinID), E);
|
|
|
|
|
2007-08-26 08:17:05 +04:00
|
|
|
fprintf(stderr, "Unimplemented builtin!!\n");
|
2007-09-13 05:17:29 +04:00
|
|
|
E->dump(getContext().SourceMgr);
|
2007-08-26 08:17:05 +04:00
|
|
|
|
|
|
|
// Unknown builtin, for now just dump it out and return undef.
|
|
|
|
if (hasAggregateLLVMType(E->getType()))
|
|
|
|
return RValue::getAggregate(CreateTempAlloca(ConvertType(E->getType())));
|
|
|
|
return RValue::get(llvm::UndefValue::get(ConvertType(E->getType())));
|
|
|
|
|
|
|
|
case Builtin::BI__builtin___CFStringMakeConstantString: {
|
|
|
|
const Expr *Arg = E->getArg(0);
|
|
|
|
|
|
|
|
while (const ParenExpr *PE = dyn_cast<ParenExpr>(Arg))
|
|
|
|
Arg = PE->getSubExpr();
|
|
|
|
|
|
|
|
const StringLiteral *Literal = cast<StringLiteral>(Arg);
|
|
|
|
std::string S(Literal->getStrData(), Literal->getByteLength());
|
|
|
|
|
|
|
|
return RValue::get(CGM.GetAddrOfConstantCFString(S));
|
2007-10-13 03:56:29 +04:00
|
|
|
}
|
|
|
|
case Builtin::BI__builtin_va_start:
|
|
|
|
case Builtin::BI__builtin_va_end: {
|
|
|
|
llvm::Value *ArgValue = EmitScalarExpr(E->getArg(0));
|
|
|
|
const llvm::Type *DestType = llvm::PointerType::get(llvm::Type::Int8Ty);
|
|
|
|
if (ArgValue->getType() != DestType)
|
|
|
|
ArgValue = Builder.CreateBitCast(ArgValue, DestType,
|
|
|
|
ArgValue->getNameStart());
|
|
|
|
|
|
|
|
llvm::Intrinsic::ID inst = (BuiltinID == Builtin::BI__builtin_va_start) ?
|
|
|
|
llvm::Intrinsic::vastart : llvm::Intrinsic::vaend;
|
|
|
|
llvm::Value *F = llvm::Intrinsic::getDeclaration(&CGM.getModule(), inst);
|
|
|
|
llvm::Value *V = Builder.CreateCall(F, ArgValue);
|
|
|
|
|
|
|
|
return RValue::get(V);
|
|
|
|
}
|
2007-08-20 22:05:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return RValue::get(0);
|
|
|
|
}
|