comparison dmd2/builtin.c @ 758:f04dde6e882c

Added initial D2 support, D2 frontend and changes to codegen to make things compile.
author Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
date Tue, 11 Nov 2008 01:38:48 +0100
parents
children 638d16625da2
comparison
equal deleted inserted replaced
757:2c730d530c98 758:f04dde6e882c
1
2 // Compiler implementation of the D programming language
3 // Copyright (c) 1999-2007 by Digital Mars
4 // All Rights Reserved
5 // written by Walter Bright
6 // http://www.digitalmars.com
7 // License for redistribution is by either the Artistic License
8 // in artistic.txt, or the GNU General Public License in gnu.txt.
9 // See the included readme.txt for details.
10
11 #include <stdio.h>
12 #include <assert.h>
13 #include <math.h>
14
15 #include "mars.h"
16 #include "declaration.h"
17 #include "attrib.h"
18 #include "expression.h"
19 #include "scope.h"
20 #include "mtype.h"
21 #include "aggregate.h"
22 #include "identifier.h"
23 #include "id.h"
24 #include "module.h"
25
26 /**********************************
27 * Determine if function is a builtin one.
28 */
29 enum BUILTIN FuncDeclaration::isBuiltin()
30 {
31 static const char FeZe[] = "FeZe"; // real function(real)
32
33 //printf("FuncDeclaration::isBuiltin() %s\n", toChars());
34 if (builtin == BUILTINunknown)
35 {
36 builtin = BUILTINnot;
37 if (parent && parent->isModule())
38 {
39 if (parent->ident == Id::math &&
40 parent->parent && parent->parent->ident == Id::std &&
41 !parent->parent->parent)
42 {
43 if (strcmp(type->deco, FeZe) == 0)
44 {
45 if (ident == Id::sin)
46 builtin = BUILTINsin;
47 else if (ident == Id::cos)
48 builtin = BUILTINcos;
49 else if (ident == Id::tan)
50 builtin = BUILTINtan;
51 else if (ident == Id::_sqrt)
52 builtin = BUILTINsqrt;
53 else if (ident == Id::fabs)
54 builtin = BUILTINfabs;
55 //printf("builtin = %d\n", builtin);
56 }
57 }
58 }
59 }
60 return builtin;
61 }
62
63
64 /**************************************
65 * Evaluate builtin function.
66 * Return result; NULL if cannot evaluate it.
67 */
68
69 Expression *eval_builtin(enum BUILTIN builtin, Expressions *arguments)
70 {
71 assert(arguments && arguments->dim);
72 Expression *arg0 = (Expression *)arguments->data[0];
73 Expression *e = NULL;
74 switch (builtin)
75 {
76 case BUILTINsin:
77 if (arg0->op == TOKfloat64)
78 e = new RealExp(0, sinl(arg0->toReal()), Type::tfloat80);
79 break;
80
81 case BUILTINcos:
82 if (arg0->op == TOKfloat64)
83 e = new RealExp(0, cosl(arg0->toReal()), Type::tfloat80);
84 break;
85
86 case BUILTINtan:
87 if (arg0->op == TOKfloat64)
88 e = new RealExp(0, tanl(arg0->toReal()), Type::tfloat80);
89 break;
90
91 case BUILTINsqrt:
92 if (arg0->op == TOKfloat64)
93 e = new RealExp(0, sqrtl(arg0->toReal()), Type::tfloat80);
94 break;
95
96 case BUILTINfabs:
97 if (arg0->op == TOKfloat64)
98 e = new RealExp(0, fabsl(arg0->toReal()), Type::tfloat80);
99 break;
100 }
101 return e;
102 }