-
Notifications
You must be signed in to change notification settings - Fork 2
/
variable.cpp
306 lines (269 loc) · 5.51 KB
/
variable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "yystype.h"
#include "variable.h"
#include "string.h"
#include <vector>
struct Stmt;
struct Block;
#include "y.tab.h"
/*
std::string Value::toString(Type t)
{
char[256] buf;
}*/
//-----------------------------------Rval binary
Rval sum(Rval l, Rval r)
{
Rval ret;
ret.type = do_types(l, r);
ret().ullongv = l().ullongv + r().ullongv;
return ret;
}
Rval difference(Rval l, Rval r)
{
Rval ret;
ret.type = do_types(l, r);
ret().ullongv = l().ullongv - r().ullongv;
return ret;
}
Rval product(Rval l, Rval r)
{
Rval ret;
ret.type = do_types(l, r);
ret().ullongv = l().ullongv * r().ullongv;
return ret;
}
Rval quotient(Rval l, Rval r)
{
Rval ret;
ret.type = do_types(l, r);
ret().ullongv = l().ullongv / r().ullongv;
return ret;
}
Rval mod(Rval l, Rval r)
{
Rval ret;
ret.type = do_types(l, r);
ret().ullongv = l().ullongv % r().ullongv;
return ret;
}
//-----------------------------------Logical
Rval equal(Rval l, Rval r)
{
Rval ret;
ret.type = bool_t;
do_types(l, r);
ret().boolv = (l().ullongv == r().ullongv);
return ret;
}
Rval nequal (Rval l, Rval r)
{
Rval ret;
ret.type = bool_t;
do_types(l, r);
ret().boolv = (l().ullongv != r().ullongv);
return ret;
}
Rval less(Rval l, Rval r)
{
Rval ret;
ret.type = bool_t;
do_types(l, r);
ret().boolv = (l().ullongv < r().ullongv);
return ret;
}
Rval greater (Rval l, Rval r)
{
Rval ret;
ret.type = bool_t;
do_types(l, r);
ret().boolv = (l().ullongv > r().ullongv);
return ret;
}
Rval lesseq (Rval l, Rval r)
{
Rval ret;
ret.type = bool_t;
do_types(l, r);
ret().boolv = (l().ullongv <= r().ullongv);
return ret;
}
Rval greatereq (Rval l, Rval r)
{
Rval ret;
ret.type = bool_t;
do_types(l, r);
ret().boolv = (l().ullongv >= r().ullongv);
return ret;
}
Rval land (Rval l, Rval r)
{
Rval ret;
ret.type = bool_t;
ret().boolv = (l().boolv && r().boolv);
return ret;
}
Rval lor (Rval l, Rval r)
{
Rval ret;
ret.type = bool_t;
ret().boolv = (l().boolv || r().boolv);
return ret;
}
//-----------------------------------Rval unary
Rval negate (Rval l)
{
Rval ret;
ret.type = l.type;
ret().ullongv = -l().ullongv;
return ret;
}
Lval deref(Rval l)
{
if (l.type.indirection < 1)
{
std::cout << "Dereference of non-pointer value\n" << std::endl;
exit(-1);
}
Lval ret((Value*)l().ptrv);
ret.type.indirection = l.type.indirection - 1;
ret.type = l.type;
return ret;
}
//-----------------------------------Lval unary
Rval pre_inc(Lval l)
{
l().ullongv++;
return l;
}
Rval pre_dec(Lval l)
{
l().ullongv--;
return l;
}
Rval post_inc(Lval l)
{
Rval ret = l;
l().ullongv++;
return ret;
}
Rval post_dec(Lval l)
{
Rval ret = l;
l().ullongv--;
return ret;
}
Rval addr_of(Lval l)
{
Rval ret;
ret.type = l.type;
ret.type.indirection = l.type.indirection + 1;
ret().ptrv = &l().ullongv;
return ret;
}
//-----------------------------------Lval binary
Lval assign(Lval l, Rval r)
{
std::cout << l.name << " (" << l.type.toString() << ") (" << &l().ullongv << ") = " << r().ullongv << " (" << r.type.toString() << ")" << std::endl;
cast(r(), r.type, l.type);
l().ullongv = r().ullongv;
return l;
}
Lval index(Lval l, Rval r)
{
if (!l.type.indirection)
std::cout << "Operand must be of array type" << std::endl;
Type rt = l.type;
rt.indirection = l.type.indirection - 1;
cast(r(), r.type, signed_t + int_t);
// std::cout << l().ptrv << " " << r().sintv << " " << l.type.size() << std::endl;
Lval ret ((Value*)(l().ptrv + r().sintv * rt.size()));
ret.type = rt;
return ret;
}
Lval::operator Rval()
{
Rval ret;
ret.type = type;
ret.val = *pval;
clean(ret.val, ret.type);
return ret;
}
//-----------------------------------Type conversions
Type do_types(Rval & l, Rval & r)
{
Type type = auto_type(l.type, r.type);
cast(l(), l.type, type);
cast(r(), r.type, type);
return type;
}
inline Type greater_rank(Type ltype, Type rtype)
{
return ltype.size() > rtype.size() ? ltype : rtype;
}
Type auto_type(Type ltype, Type rtype)
{
if (ltype == rtype)
return ltype;
if (ltype & signed_t && rtype & signed_t || ltype & unsigned_t && rtype & unsigned_t)
return greater_rank(ltype, rtype);
if (ltype & unsigned_t)
if(ltype == greater_rank(ltype, rtype))
return ltype;
else
return rtype;
if (rtype & unsigned_t)
if(rtype == greater_rank(ltype, rtype))
return rtype;
else
return ltype;
if (ltype & signed_t)
return ltype - signed_t + unsigned_t;
else
return rtype - signed_t + unsigned_t;
}
void clean(Value & val, Type t)
{
memset(((void*)&val)+t.size(), 0, sizeof(Value)-t.size());
//TODO: warn about data loss if neccesary
}
void cast(Value & val, Type from, Type to)
{
if (from.indirection)
from = unsigned_t + int_t;
if (to.indirection)
to = unsigned_t + int_t;
if (from == to)
return;
if ((from & char_t || from & int_t) && (to & char_t || to & int_t))
{
if (from.size() > to.size())
{
clean(val, to); //easy!
return;
}
else if (from & unsigned_t) //sign ext. is only done if it starts out signed
return; //easier!
else
{
if (from & char_t)
{
if (to & short_t) val.sshortv = val.scharv;
else if (to & long_t) val.slongv = val.scharv;
else if (to & llong_t) val.slongv = val.scharv;
else val.sintv = val.scharv;
}
else if (from & short_t)
{
if (to & long_t) val.slongv = val.scharv;
else if (to & llong_t) val.slongv = val.scharv;
else val.sintv = val.scharv;
}
else if (from & long_t) val.slongv = val.scharv;
else
{
if (to & long_t) val.slongv = val.scharv;
else val.slongv = val.scharv;
}
}
} //TODO: Add float-float and float-int
}