File: Callee.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (346 lines) | stat: -rw-r--r-- 8,172 bytes parent folder | download | duplicates (3)
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "stdafx.h"
#include "CallCommon.h"
#include "Code/Binary.h"
#include "Code/Listing.h"
#include "Compiler/Debug.h"
#include "Core/Geometry/Rect.h"

/**
 * File containing tests for calling conventions from the callee's perspective.
 *
 * Mirrors tests in Call.cpp.
 */

using namespace code;

using storm::debug::DbgVal;

BEGIN_TEST(CalleePrimitive, Code) {
	Engine &e = gEngine();
	Arena *arena = code::arena(e);

	Listing *l = new (e) Listing(false, intDesc(e));
	Var p1 = l->createParam(intDesc(e));

	*l << prolog();

	*l << mov(eax, p1);
	*l << add(eax, intConst(2));

	*l << fnRet(eax);

	Binary *b = new (e) Binary(arena, l);
	typedef Int (*Fn)(Int);
	Fn fn = (Fn)b->address();

	CHECK_EQ((*fn)(100), 102);

} END_TEST


BEGIN_TEST(CalleePrimitiveMany, Code) {
	Engine &e = gEngine();
	Arena *arena = code::arena(e);

	Listing *l = new (e) Listing(false, intDesc(e));
	Var p1 = l->createParam(intDesc(e));
	Var p2 = l->createParam(intDesc(e));
	Var p3 = l->createParam(intDesc(e));
	Var p4 = l->createParam(intDesc(e));
	Var p5 = l->createParam(intDesc(e));
	Var p6 = l->createParam(intDesc(e));
	Var p7 = l->createParam(intDesc(e));
	Var p8 = l->createParam(intDesc(e));
	Var p9 = l->createParam(intDesc(e));

	*l << prolog();

	*l << mov(eax, p1);
	*l << mul(eax, intConst(10));
	*l << add(eax, p2);
	*l << mul(eax, intConst(10));
	*l << add(eax, p3);
	*l << mul(eax, intConst(10));
	*l << add(eax, p4);
	*l << mul(eax, intConst(10));
	*l << add(eax, p5);
	*l << mul(eax, intConst(10));
	*l << add(eax, p6);
	*l << mul(eax, intConst(10));
	*l << add(eax, p7);
	*l << mul(eax, intConst(10));
	*l << add(eax, p8);
	*l << mul(eax, intConst(10));
	*l << add(eax, p9);

	*l << fnRet(eax);

	Binary *b = new (e) Binary(arena, l);
	typedef Int (*Fn)(Int, Int, Int, Int, Int, Int, Int, Int, Int);
	Fn fn = (Fn)b->address();

	(*fn)(1, 2, 3, 4, 5, 6, 7, 8, 9);
	CHECK_EQ((*fn)(1, 2, 3, 4, 5, 6, 7, 8, 9), 123456789);

} END_TEST


BEGIN_TEST(CalleeTinyInt, Code) {
	Engine &e = gEngine();
	Arena *arena = code::arena(e);

	Listing *l = new (e) Listing(false, intDesc(e));
	Var p1 = l->createParam(tinyIntDesc(e));

	*l << prolog();

	*l << mov(ebx, intRel(p1, Offset()));
	*l << sub(ebx, intRel(p1, Offset::sInt));

	*l << fnRet(ebx);

	Binary *b = new (e) Binary(arena, l);
	typedef Int (*Fn)(TinyIntParam);
	Fn fn = (Fn)b->address();

	CHECK_EQ((*fn)(TinyIntParam(20, 10)), 10);

} END_TEST


BEGIN_TEST(CalleeSmallInt, Code) {
	Engine &e = gEngine();
	Arena *arena = code::arena(e);

	Listing *l = new (e) Listing(false, intDesc(e));
	Var p1 = l->createParam(smallIntDesc(e));

	*l << prolog();

	// Note: Members are technically pointer-sized, so this is a bit incorrect, but good enough for a test.
	*l << mov(ebx, intRel(p1, Offset()));
	*l << sub(ebx, intRel(p1, Offset::sPtr));

	*l << fnRet(ebx);

	Binary *b = new (e) Binary(arena, l);
	typedef Int (*Fn)(SmallIntParam);
	Fn fn = (Fn)b->address();

	CHECK_EQ((*fn)(SmallIntParam(20, 10)), 10);

} END_TEST


BEGIN_TEST(CalleeMixedInt, Code) {
	Engine &e = gEngine();
	Arena *arena = code::arena(e);

	Listing *l = new (e) Listing(false, intDesc(e));
	Var p1 = l->createParam(largeIntDesc(e));
	Var p2 = l->createParam(smallIntDesc(e));

	*l << prolog();

	// Note: Members are technically pointer-sized, so this is a bit incorrect, but good enough for a test.
	*l << mov(ebx, intRel(p1, Offset()));
	*l << add(ebx, intRel(p1, Offset::sPtr));
	*l << sub(ebx, intRel(p1, Offset::sPtr * 2));

	*l << add(ebx, intRel(p2, Offset()));
	*l << sub(ebx, intRel(p2, Offset::sPtr));

	*l << fnRet(ebx);

	Binary *b = new (e) Binary(arena, l);
	typedef Int (*Fn)(LargeIntParam, SmallIntParam);
	Fn fn = (Fn)b->address();

	CHECK_EQ((*fn)(LargeIntParam(1, 2, 3), SmallIntParam(40, 10)), 30);
} END_TEST


BEGIN_TEST(CalleeMixed, Code) {
	Engine &e = gEngine();
	Arena *arena = code::arena(e);

	Listing *l = new (e) Listing(false, floatDesc(e));
	Var p1 = l->createParam(mixedDesc(e));

	*l << prolog();

	// Implements: Float(a.a) - a.b / a.c
	*l << mov(ebx, intRel(p1, Offset()));
	*l << icastf(ebx, ebx);
	*l << mov(eax, floatRel(p1, Offset::sPtr));
	*l << fdiv(eax, floatRel(p1, Offset::sPtr + Offset::sFloat));
	*l << code::fsub(ebx, eax);

	*l << fnRet(ebx);

	Binary *b = new (e) Binary(arena, l);
	typedef Float (*Fn)(MixedParam);
	Fn fn = (Fn)b->address();

	CHECK_EQ((*fn)(MixedParam(100, 40.0f, 10.0f)), 96.0f);

} END_TEST


BEGIN_TEST(CalleeComplex, Code) {
	Engine &e = gEngine();
	Arena *arena = code::arena(e);
	Type *dbgVal = DbgVal::stormType(e);
	ComplexDesc *desc = new (e) ComplexDesc(dbgVal->size(), dbgVal->copyCtor()->ref(), dbgVal->destructor()->ref());

	Listing *l = new (e) Listing(false, intDesc(e));
	Var p1 = l->createParam(desc);
	Var p2 = l->createParam(intDesc(e));

	*l << prolog();

	*l << mov(eax, intRel(p1, Offset()));
	*l << add(eax, p2);

	*l << fnRet(eax);

	Binary *b = new (e) Binary(arena, l);
	typedef Int (*Fn)(DbgVal, Int);
	Fn fn = (Fn)b->address();

	DbgVal::clear();
	{
		DbgVal v;
		CHECK_EQ((*fn)(v, 8), 18);
	}
	CHECK(DbgVal::clear());

} END_TEST


// Note: CallBytes in call.cpp tests receiving a struct as well.

BEGIN_TEST(CalleeIntFloat, Code) {
	Engine &e = gEngine();
	Arena *arena = code::arena(e);

	Listing *l = new (e) Listing(false, floatDesc(e));
	Var p1 = l->createParam(floatDesc(e));
	Var p2 = l->createParam(intDesc(e));
	Var p3 = l->createParam(floatDesc(e));
	Var p4 = l->createParam(intDesc(e));

	*l << prolog();
	*l << mov(eax, p1);
	*l << icastf(ecx, p2);
	*l << code::fadd(eax, ecx);
	*l << code::fadd(eax, p3);
	*l << icastf(ecx, p4);
	*l << code::fadd(eax, ecx);
	*l << fnRet(eax);

	Binary *b = new (e) Binary(arena, l);
	typedef Float(*Fn)(Float, Int, Float, Int);
	Fn fn = (Fn)b->address();

	CHECK_EQ((*fn)(1.5f, 2, 2.5f, 3), 9.0f);
} END_TEST

BEGIN_TEST(CalleePoint, Code) {
	using geometry::Point;

	Engine &e = gEngine();
	Arena *arena = code::arena(e);

	SimpleDesc *point = pointDesc(e);
	Listing *l = new (e) Listing(false, floatDesc(e));
	Var p = l->createParam(point);

	*l << prolog();
	*l << mov(eax, floatRel(p, Offset::sFloat));
	*l << fsub(eax, floatRel(p, Offset()));
	*l << fnRet(eax);

	Binary *b = new (e) Binary(arena, l);
	typedef Float (*Fn)(Point);
	Fn fn = (Fn)b->address();

	Float r = (*fn)(Point(10, 20));
	CHECK_EQ(r, 10.0f);
} END_TEST


BEGIN_TEST(CalleePointRet, Code) {
	using geometry::Point;

	Engine &e = gEngine();
	Arena *arena = code::arena(e);

	SimpleDesc *point = pointDesc(e);
	Listing *l = new (e) Listing(false, point);
	Var p = l->createVar(l->root(), point->size());

	*l << prolog();
	*l << mov(floatRel(p, Offset()), floatConst(3.0f));
	*l << mov(floatRel(p, Offset::sFloat), floatConst(4.0f));
	*l << fnRet(p);

	Binary *b = new (e) Binary(arena, l);
	typedef geometry::Point (*Fn)();
	Fn fn = (Fn)b->address();

	geometry::Point r = (*fn)();
	CHECK_EQ(r, Point(3, 4));
} END_TEST


BEGIN_TEST(CalleeRect, Code) {
	using geometry::Rect;

	Engine &e = gEngine();
	Arena *arena = code::arena(e);

	SimpleDesc *rect = rectDesc(e);
	Listing *l = new (e) Listing(false, floatDesc(e));
	Var p = l->createParam(rect);

	*l << prolog();
	*l << mov(eax, floatRel(p, Offset::sFloat*2));
	*l << fsub(eax, floatRel(p, Offset()));
	*l << fadd(eax, floatRel(p, Offset::sFloat*3));
	*l << fsub(eax, floatRel(p, Offset::sFloat));
	*l << fnRet(eax);

	Binary *b = new (e) Binary(arena, l);
	typedef Float (*Fn)(Rect);
	Fn fn = (Fn)b->address();

	Float r = (*fn)(Rect(10, 11, 21, 31));
	CHECK_EQ(r, 31.0f);
} END_TEST


BEGIN_TEST(CalleeRectRet, Code) {
	using geometry::Rect;

	Engine &e = gEngine();
	Arena *arena = code::arena(e);

	SimpleDesc *rect = rectDesc(e);
	Listing *l = new (e) Listing(false, rect);
	Var p = l->createVar(l->root(), rect->size());

	*l << prolog();
	*l << mov(floatRel(p, Offset()), floatConst(1.0f));
	*l << mov(floatRel(p, Offset::sFloat), floatConst(2.0f));
	*l << mov(floatRel(p, Offset::sFloat*2), floatConst(3.0f));
	*l << mov(floatRel(p, Offset::sFloat*3), floatConst(4.0f));
	*l << fnRet(p);

	Binary *b = new (e) Binary(arena, l);
	typedef geometry::Rect (*Fn)();
	Fn fn = (Fn)b->address();

	geometry::Rect r = (*fn)();
	CHECK_EQ(r, Rect(1, 2, 3, 4));
} END_TEST