| 1 |
// Compiler implementation of the D programming language |
|---|
| 2 |
// Copyright (c) 1999-2009 by Digital Mars |
|---|
| 3 |
// All Rights Reserved |
|---|
| 4 |
// written by Walter Bright |
|---|
| 5 |
// http://www.digitalmars.com |
|---|
| 6 |
// License for redistribution is by either the Artistic License |
|---|
| 7 |
// in artistic.txt, or the GNU General Public License in gnu.txt. |
|---|
| 8 |
// See the included readme.txt for details. |
|---|
| 9 |
|
|---|
| 10 |
#include <stdio.h> |
|---|
| 11 |
#include <assert.h> |
|---|
| 12 |
#include <math.h> |
|---|
| 13 |
|
|---|
| 14 |
#if __FreeBSD__ |
|---|
| 15 |
extern "C" |
|---|
| 16 |
{ |
|---|
| 17 |
long double sinl(long double); |
|---|
| 18 |
long double cosl(long double); |
|---|
| 19 |
long double tanl(long double); |
|---|
| 20 |
long double sqrtl(long double); |
|---|
| 21 |
} |
|---|
| 22 |
#endif |
|---|
| 23 |
|
|---|
| 24 |
#include "mars.h" |
|---|
| 25 |
#include "declaration.h" |
|---|
| 26 |
#include "attrib.h" |
|---|
| 27 |
#include "expression.h" |
|---|
| 28 |
#include "scope.h" |
|---|
| 29 |
#include "mtype.h" |
|---|
| 30 |
#include "aggregate.h" |
|---|
| 31 |
#include "identifier.h" |
|---|
| 32 |
#include "id.h" |
|---|
| 33 |
#include "module.h" |
|---|
| 34 |
|
|---|
| 35 |
#if DMDV2 |
|---|
| 36 |
|
|---|
| 37 |
/********************************** |
|---|
| 38 |
* Determine if function is a builtin one that we can |
|---|
| 39 |
* evaluate at compile time. |
|---|
| 40 |
*/ |
|---|
| 41 |
enum BUILTIN FuncDeclaration::isBuiltin() |
|---|
| 42 |
{ |
|---|
| 43 |
static const char FeZe [] = "FNaNbNfeZe"; // @safe pure nothrow real function(real) |
|---|
| 44 |
static const char FeZe2[] = "FNaNbNeeZe"; // @trusted pure nothrow real function(real) |
|---|
| 45 |
|
|---|
| 46 |
//printf("FuncDeclaration::isBuiltin() %s\n", toChars()); |
|---|
| 47 |
if (builtin == BUILTINunknown) |
|---|
| 48 |
{ |
|---|
| 49 |
builtin = BUILTINnot; |
|---|
| 50 |
if (parent && parent->isModule()) |
|---|
| 51 |
{ |
|---|
| 52 |
// If it's in the std.math package |
|---|
| 53 |
if (parent->ident == Id::math && |
|---|
| 54 |
parent->parent && parent->parent->ident == Id::std && |
|---|
| 55 |
!parent->parent->parent) |
|---|
| 56 |
{ |
|---|
| 57 |
//printf("deco = %s\n", type->deco); |
|---|
| 58 |
if (strcmp(type->deco, FeZe) == 0 || strcmp(type->deco, FeZe2) == 0) |
|---|
| 59 |
{ |
|---|
| 60 |
if (ident == Id::sin) |
|---|
| 61 |
builtin = BUILTINsin; |
|---|
| 62 |
else if (ident == Id::cos) |
|---|
| 63 |
builtin = BUILTINcos; |
|---|
| 64 |
else if (ident == Id::tan) |
|---|
| 65 |
builtin = BUILTINtan; |
|---|
| 66 |
else if (ident == Id::_sqrt) |
|---|
| 67 |
builtin = BUILTINsqrt; |
|---|
| 68 |
else if (ident == Id::fabs) |
|---|
| 69 |
builtin = BUILTINfabs; |
|---|
| 70 |
//printf("builtin = %d\n", builtin); |
|---|
| 71 |
} |
|---|
| 72 |
// if float or double versions |
|---|
| 73 |
else if (strcmp(type->deco, "FNaNbNfdZd") == 0 || |
|---|
| 74 |
strcmp(type->deco, "FNaNbNffZf") == 0) |
|---|
| 75 |
{ |
|---|
| 76 |
if (ident == Id::_sqrt) |
|---|
| 77 |
builtin = BUILTINsqrt; |
|---|
| 78 |
} |
|---|
| 79 |
} |
|---|
| 80 |
} |
|---|
| 81 |
} |
|---|
| 82 |
return builtin; |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
/************************************** |
|---|
| 87 |
* Evaluate builtin function. |
|---|
| 88 |
* Return result; NULL if cannot evaluate it. |
|---|
| 89 |
*/ |
|---|
| 90 |
|
|---|
| 91 |
Expression *eval_builtin(enum BUILTIN builtin, Expressions *arguments) |
|---|
| 92 |
{ |
|---|
| 93 |
assert(arguments && arguments->dim); |
|---|
| 94 |
Expression *arg0 = (Expression *)arguments->data[0]; |
|---|
| 95 |
Expression *e = NULL; |
|---|
| 96 |
switch (builtin) |
|---|
| 97 |
{ |
|---|
| 98 |
case BUILTINsin: |
|---|
| 99 |
if (arg0->op == TOKfloat64) |
|---|
| 100 |
e = new RealExp(0, sinl(arg0->toReal()), arg0->type); |
|---|
| 101 |
break; |
|---|
| 102 |
|
|---|
| 103 |
case BUILTINcos: |
|---|
| 104 |
if (arg0->op == TOKfloat64) |
|---|
| 105 |
e = new RealExp(0, cosl(arg0->toReal()), arg0->type); |
|---|
| 106 |
break; |
|---|
| 107 |
|
|---|
| 108 |
case BUILTINtan: |
|---|
| 109 |
if (arg0->op == TOKfloat64) |
|---|
| 110 |
e = new RealExp(0, tanl(arg0->toReal()), arg0->type); |
|---|
| 111 |
break; |
|---|
| 112 |
|
|---|
| 113 |
case BUILTINsqrt: |
|---|
| 114 |
if (arg0->op == TOKfloat64) |
|---|
| 115 |
e = new RealExp(0, sqrtl(arg0->toReal()), arg0->type); |
|---|
| 116 |
break; |
|---|
| 117 |
|
|---|
| 118 |
case BUILTINfabs: |
|---|
| 119 |
if (arg0->op == TOKfloat64) |
|---|
| 120 |
e = new RealExp(0, fabsl(arg0->toReal()), arg0->type); |
|---|
| 121 |
break; |
|---|
| 122 |
} |
|---|
| 123 |
return e; |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
#endif |
|---|