annotate dmd/DivExp.d @ 63:cab4c37afb89

A bunch of implementations
author korDen
date Mon, 23 Aug 2010 16:52:24 +0400
parents 832f71e6f96c
children 2e2a5c3f943a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.DivExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
3 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Identifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.backend.elem;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.InterState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.IntRange;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.IRState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.ArrayTypes;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.BinExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.NegExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.Id;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.expression.Div;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.backend.OPER;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 class DivExp : BinExp
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 this(Loc loc, Expression e1, Expression e2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 super(loc, TOK.TOKdiv, DivExp.sizeof, e1, e2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 Expression semantic(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 if (type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 super.semanticp(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 e = op_overload(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 if (e)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 typeCombine(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 if (e1.op != TOK.TOKslice && e2.op != TOK.TOKslice)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 e1.checkArithmetic();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 e2.checkArithmetic();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 if (type.isfloating())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 Type t1 = e1.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 Type t2 = e2.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 if (t1.isreal())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 type = t2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 if (t2.isimaginary())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 // x/iv = i(-x/v)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 e2.type = t1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 Expression ee = new NegExp(loc, this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 ee = ee.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 else if (t2.isreal())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 type = t1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 else if (t1.isimaginary())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 if (t2.isimaginary()) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 switch (t1.ty)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 case TY.Timaginary32: type = Type.tfloat32; break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 case TY.Timaginary64: type = Type.tfloat64; break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 case TY.Timaginary80: type = Type.tfloat80; break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 default: assert(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 type = t2; // t2 is complex
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 else if (t2.isimaginary())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 type = t1; // t1 is complex
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 Expression optimize(int result)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 //printf("DivExp.optimize(%s)\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 e1 = e1.optimize(result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 e2 = e2.optimize(result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 if (e1.isConst() == 1 && e2.isConst() == 1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 e = Div(type, e1, e2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 e = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107
63
cab4c37afb89 A bunch of implementations
korDen
parents: 12
diff changeset
108 Expression interpret(InterState istate)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 void buildArrayIdent(OutBuffer buf, Expressions arguments)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 {
12
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
115 Exp_buildArrayIdent(buf, arguments, "Div");
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 Expression buildArrayLoop(Arguments fparams)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 {
12
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
120 /* Evaluate assign expressions left to right
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
121 */
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
122 Expression ex1 = e1.buildArrayLoop(fparams);
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
123 Expression ex2 = e2.buildArrayLoop(fparams);
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
124 Expression e = new DivExp(Loc(0), ex1, ex2);
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
125 return e;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 IntRange getIntRange()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 Identifier opId()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 return Id.div;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 Identifier opId_r()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 return Id.div_r;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 elem* toElem(IRState* irs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 return toElemBin(irs,OPdiv);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148