Anders Carlsson | 022012e | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 1 | //===---- 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 Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | 022012e | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 17 | #include "clang/AST/Builtins.h" |
| 18 | #include "clang/AST/Expr.h" |
Chris Lattner | 6de93ff | 2007-08-26 04:17:05 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
Chris Lattner | c5e940f | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 20 | #include "llvm/Function.h" |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 21 | #include "llvm/Intrinsics.h" |
| 22 | |
Anders Carlsson | 022012e | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
| 25 | |
Chris Lattner | 6de93ff | 2007-08-26 04:17:05 +0000 | [diff] [blame] | 26 | RValue CodeGenFunction::EmitBuiltinExpr(unsigned BuiltinID, const CallExpr *E) { |
| 27 | switch (BuiltinID) { |
| 28 | default: |
Chris Lattner | c5e940f | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 29 | if (getContext().BuiltinInfo.isLibFunction(BuiltinID)) |
| 30 | return EmitCallExpr(CGM.getBuiltinLibFunction(BuiltinID), E); |
| 31 | |
Chris Lattner | 6de93ff | 2007-08-26 04:17:05 +0000 | [diff] [blame] | 32 | fprintf(stderr, "Unimplemented builtin!!\n"); |
Chris Lattner | 419ea7e | 2007-09-13 01:17:29 +0000 | [diff] [blame] | 33 | E->dump(getContext().SourceMgr); |
Chris Lattner | 6de93ff | 2007-08-26 04:17:05 +0000 | [diff] [blame] | 34 | |
| 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 | |
Anders Carlsson | d6a275f | 2007-11-01 00:39:26 +0000 | [diff] [blame] | 43 | while (1) { |
| 44 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(Arg)) |
| 45 | Arg = PE->getSubExpr(); |
| 46 | else if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(Arg)) |
| 47 | Arg = CE->getSubExpr(); |
| 48 | else |
| 49 | break; |
| 50 | } |
Chris Lattner | 6de93ff | 2007-08-26 04:17:05 +0000 | [diff] [blame] | 51 | |
| 52 | const StringLiteral *Literal = cast<StringLiteral>(Arg); |
| 53 | std::string S(Literal->getStrData(), Literal->getByteLength()); |
| 54 | |
| 55 | return RValue::get(CGM.GetAddrOfConstantCFString(S)); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 56 | } |
| 57 | case Builtin::BI__builtin_va_start: |
| 58 | case Builtin::BI__builtin_va_end: { |
| 59 | llvm::Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
| 60 | const llvm::Type *DestType = llvm::PointerType::get(llvm::Type::Int8Ty); |
| 61 | if (ArgValue->getType() != DestType) |
| 62 | ArgValue = Builder.CreateBitCast(ArgValue, DestType, |
| 63 | ArgValue->getNameStart()); |
| 64 | |
| 65 | llvm::Intrinsic::ID inst = (BuiltinID == Builtin::BI__builtin_va_start) ? |
| 66 | llvm::Intrinsic::vastart : llvm::Intrinsic::vaend; |
| 67 | llvm::Value *F = llvm::Intrinsic::getDeclaration(&CGM.getModule(), inst); |
| 68 | llvm::Value *V = Builder.CreateCall(F, ArgValue); |
| 69 | |
| 70 | return RValue::get(V); |
| 71 | } |
Anders Carlsson | 89799cf | 2007-10-29 02:59:40 +0000 | [diff] [blame] | 72 | case Builtin::BI__builtin_classify_type: { |
| 73 | llvm::APSInt Result(32); |
| 74 | |
| 75 | if (!E->isBuiltinClassifyType(Result)) |
| 76 | assert(0 && "Expr not __builtin_classify_type!"); |
| 77 | |
| 78 | return RValue::get(llvm::ConstantInt::get(Result)); |
Anders Carlsson | 022012e | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 79 | } |
Anders Carlsson | d6a275f | 2007-11-01 00:39:26 +0000 | [diff] [blame] | 80 | case Builtin::BI__builtin_constant_p: { |
| 81 | llvm::APSInt Result(32); |
| 82 | |
| 83 | // FIXME: Analyze the parameter and check if it is a constant. |
| 84 | Result = 0; |
| 85 | |
| 86 | return RValue::get(llvm::ConstantInt::get(Result)); |
| 87 | } |
Anders Carlsson | c2251dc | 2007-11-20 19:05:17 +0000 | [diff] [blame^] | 88 | case Builtin::BI__builtin_abs: { |
| 89 | llvm::Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
| 90 | |
| 91 | llvm::BinaryOperator *NegOp = |
| 92 | Builder.CreateNeg(ArgValue, (ArgValue->getName() + "neg").c_str()); |
| 93 | llvm::Value *CmpResult = |
| 94 | Builder.CreateICmpSGE(ArgValue, NegOp->getOperand(0), "abscond"); |
| 95 | llvm::Value *Result = |
| 96 | Builder.CreateSelect(CmpResult, ArgValue, NegOp, "abs"); |
| 97 | |
| 98 | return RValue::get(Result); |
| 99 | } |
Anders Carlsson | 89799cf | 2007-10-29 02:59:40 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Anders Carlsson | 022012e | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 102 | return RValue::get(0); |
| 103 | } |