annotate dmd/AddExp.d @ 12:832f71e6f96c

*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete) Some unittest-specific functions implemented
author korDen
date Mon, 12 Apr 2010 15:13:00 +0400
parents 10317f0c89a5
children cab4c37afb89
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.AddExp;
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.Id;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.Scope;
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.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.expression.Add;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.backend.OPER;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 class AddExp : BinExp
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 this(Loc loc, Expression e1, Expression e2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 super(loc, TOK.TOKadd, AddExp.sizeof, e1, e2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 Expression semantic(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 version (LOGSEMANTIC) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 printf("AddExp.semantic('%s')\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 if (!type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 BinExp.semanticp(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 e = op_overload(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 if (e)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 Type tb1 = e1.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 Type tb2 = e2.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 if ((tb1.ty == TY.Tarray || tb1.ty == TY.Tsarray) &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 (tb2.ty == TY.Tarray || tb2.ty == TY.Tsarray) &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 tb1.nextOf().equals(tb2.nextOf())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 )
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 type = e1.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 e = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 else if (tb1.ty == TY.Tpointer && e2.type.isintegral() ||
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 tb2.ty == TY.Tpointer && e1.type.isintegral())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 e = scaleFactor(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 else if (tb1.ty == TY.Tpointer && tb2.ty == TY.Tpointer)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 incompatibleTypes();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 type = e1.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 e = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 typeCombine(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 if ((e1.type.isreal() && e2.type.isimaginary()) ||
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 (e1.type.isimaginary() && e2.type.isreal()))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 switch (type.toBasetype().ty)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 case TY.Tfloat32:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 case TY.Timaginary32:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 type = Type.tcomplex32;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 case TY.Tfloat64:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 case TY.Timaginary64:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 type = Type.tcomplex64;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 case TY.Tfloat80:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 case TY.Timaginary80:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 type = Type.tcomplex80;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 e = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 Expression optimize(int result)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 //printf("AddExp::optimize(%s)\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 e1 = e1.optimize(result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 e2 = e2.optimize(result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 if (e1.isConst() && e2.isConst())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 if (e1.op == TOK.TOKsymoff && e2.op == TOK.TOKsymoff)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 e = Add(type, e1, e2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 e = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 Expression interpret(InterState* istate)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 void buildArrayIdent(OutBuffer buf, Expressions arguments)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 {
12
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
121 Exp_buildArrayIdent(buf, arguments, "Add");
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 Expression buildArrayLoop(Arguments fparams)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 {
12
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
126 /* Evaluate assign expressions left to right
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
127 */
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
128 Expression ex1 = e1.buildArrayLoop(fparams);
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
129 Expression ex2 = e2.buildArrayLoop(fparams);
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
130 Expression e = new AddExp(Loc(0), ex1, ex2);
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
131 return e;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 bool isCommutative()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 Identifier opId()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 return Id.add;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 Identifier opId_r()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 return Id.add_r;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 elem* toElem(IRState* irs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 elem *e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 Type tb1 = e1.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 Type tb2 = e2.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 if ((tb1.ty == TY.Tarray || tb1.ty == TY.Tsarray) && (tb2.ty == TY.Tarray || tb2.ty == TY.Tsarray))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 error("Array operation %s not implemented", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 e = el_long(type.totym(), 0); // error recovery
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 e = toElemBin(irs, OPER.OPadd);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166