root/branches/dmdfe-2.0/builtin.c

Revision 836, 2.5 kB (checked in by Gregor, 1 year ago)

MERGE: DMD 2.005

  • Property svn:eol-style set to native
Line 
1 // Compiler implementation of the D programming language
2 // Copyright (c) 1999-2007 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 #include "mars.h"
15 #include "declaration.h"
16 #include "attrib.h"
17 #include "expression.h"
18 #include "scope.h"
19 #include "mtype.h"
20 #include "aggregate.h"
21 #include "identifier.h"
22 #include "id.h"
23 #include "module.h"
24
25 /**********************************
26  * Determine if function is a builtin one.
27  */
28 enum BUILTIN FuncDeclaration::isBuiltin()
29 {
30     static const char FeZe[] = "FeZe";  // real function(real)
31
32     //printf("FuncDeclaration::isBuiltin() %s\n", toChars());
33     if (builtin == BUILTINunknown)
34     {
35     builtin = BUILTINnot;
36     if (parent && parent->isModule())
37     {
38         if (parent->ident == Id::math &&
39         parent->parent && parent->parent->ident == Id::std &&
40         !parent->parent->parent)
41         {
42         if (strcmp(type->deco, FeZe) == 0)
43         {
44             if (ident == Id::sin)
45             builtin = BUILTINsin;
46             else if (ident == Id::cos)
47             builtin = BUILTINcos;
48             else if (ident == Id::tan)
49             builtin = BUILTINtan;
50             else if (ident == Id::_sqrt)
51             builtin = BUILTINsqrt;
52             else if (ident == Id::fabs)
53             builtin = BUILTINfabs;
54             //printf("builtin = %d\n", builtin);
55         }
56         }
57     }
58     }
59     return builtin;
60 }
61
62
63 /**************************************
64  * Evaluate builtin function.
65  * Return result; NULL if cannot evaluate it.
66  */
67
68 Expression *eval_builtin(enum BUILTIN builtin, Expressions *arguments)
69 {
70     assert(arguments && arguments->dim);
71     Expression *arg0 = (Expression *)arguments->data[0];
72     Expression *e = NULL;
73     switch (builtin)
74     {
75     case BUILTINsin:
76         if (arg0->op == TOKfloat64)
77         e = new RealExp(0, sinl(arg0->toReal()), Type::tfloat80);
78         break;
79
80     case BUILTINcos:
81         if (arg0->op == TOKfloat64)
82         e = new RealExp(0, cosl(arg0->toReal()), Type::tfloat80);
83         break;
84
85     case BUILTINtan:
86         if (arg0->op == TOKfloat64)
87         e = new RealExp(0, tanl(arg0->toReal()), Type::tfloat80);
88         break;
89
90     case BUILTINsqrt:
91         if (arg0->op == TOKfloat64)
92         e = new RealExp(0, sqrtl(arg0->toReal()), Type::tfloat80);
93         break;
94
95     case BUILTINfabs:
96         if (arg0->op == TOKfloat64)
97         e = new RealExp(0, fabsl(arg0->toReal()), Type::tfloat80);
98         break;
99     }
100     return e;
101 }
Note: See TracBrowser for help on using the browser.