blob: ecac12b1f4b37938428591cc65ae763d0235d38a [file] [log] [blame]
Anders Carlsson49865302007-08-20 18:05:56 +00001//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Anders Carlsson and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Builtin calls as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
Chris Lattnerab862cc2007-08-31 04:31:45 +000016#include "clang/AST/ASTContext.h"
Anders Carlsson49865302007-08-20 18:05:56 +000017#include "clang/AST/Builtins.h"
18#include "clang/AST/Expr.h"
Chris Lattner783a78f2007-08-26 04:17:05 +000019#include "llvm/Constants.h"
Chris Lattner02c60f52007-08-31 04:44:06 +000020#include "llvm/Function.h"
Anders Carlssoncebb8d62007-10-12 23:56:29 +000021#include "llvm/Intrinsics.h"
22
Anders Carlsson49865302007-08-20 18:05:56 +000023using namespace clang;
24using namespace CodeGen;
25
Chris Lattner783a78f2007-08-26 04:17:05 +000026RValue CodeGenFunction::EmitBuiltinExpr(unsigned BuiltinID, const CallExpr *E) {
27 switch (BuiltinID) {
28 default:
Chris Lattner02c60f52007-08-31 04:44:06 +000029 if (getContext().BuiltinInfo.isLibFunction(BuiltinID))
30 return EmitCallExpr(CGM.getBuiltinLibFunction(BuiltinID), E);
31
Chris Lattner783a78f2007-08-26 04:17:05 +000032 fprintf(stderr, "Unimplemented builtin!!\n");
Chris Lattner1aef6212007-09-13 01:17:29 +000033 E->dump(getContext().SourceMgr);
Chris Lattner783a78f2007-08-26 04:17:05 +000034
35 // Unknown builtin, for now just dump it out and return undef.
36 if (hasAggregateLLVMType(E->getType()))
37 return RValue::getAggregate(CreateTempAlloca(ConvertType(E->getType())));
38 return RValue::get(llvm::UndefValue::get(ConvertType(E->getType())));
39
40 case Builtin::BI__builtin___CFStringMakeConstantString: {
41 const Expr *Arg = E->getArg(0);
42
43 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Arg))
44 Arg = PE->getSubExpr();
45
46 const StringLiteral *Literal = cast<StringLiteral>(Arg);
47 std::string S(Literal->getStrData(), Literal->getByteLength());
48
49 return RValue::get(CGM.GetAddrOfConstantCFString(S));
Anders Carlssoncebb8d62007-10-12 23:56:29 +000050 }
51 case Builtin::BI__builtin_va_start:
52 case Builtin::BI__builtin_va_end: {
53 llvm::Value *ArgValue = EmitScalarExpr(E->getArg(0));
54 const llvm::Type *DestType = llvm::PointerType::get(llvm::Type::Int8Ty);
55 if (ArgValue->getType() != DestType)
56 ArgValue = Builder.CreateBitCast(ArgValue, DestType,
57 ArgValue->getNameStart());
58
59 llvm::Intrinsic::ID inst = (BuiltinID == Builtin::BI__builtin_va_start) ?
60 llvm::Intrinsic::vastart : llvm::Intrinsic::vaend;
61 llvm::Value *F = llvm::Intrinsic::getDeclaration(&CGM.getModule(), inst);
62 llvm::Value *V = Builder.CreateCall(F, ArgValue);
63
64 return RValue::get(V);
65 }
Anders Carlsson40438402007-10-29 02:59:40 +000066 case Builtin::BI__builtin_classify_type: {
67 llvm::APSInt Result(32);
68
69 if (!E->isBuiltinClassifyType(Result))
70 assert(0 && "Expr not __builtin_classify_type!");
71
72 return RValue::get(llvm::ConstantInt::get(Result));
Anders Carlsson49865302007-08-20 18:05:56 +000073 }
Anders Carlsson40438402007-10-29 02:59:40 +000074 }
75
Anders Carlsson49865302007-08-20 18:05:56 +000076 return RValue::get(0);
77}