File: VTable.cpp

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; 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 (350 lines) | stat: -rw-r--r-- 10,467 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
347
348
349
350
#include "stdafx.h"
#include "Compiler/VTableCpp.h"
#include "Compiler/Debug.h"
#include "Compiler/Package.h"
#include "Compiler/TypeCtor.h"
#include "Code/X86/Arena.h"

class VTableTest : public RootObject {
public:
	VTableTest(int v) : z(v) {}

	virtual int CODECALL replace() const {
		return z;
	}

	int z;
};

static int CODECALL replaced(VTableTest *me) {
	return me->z + 10;
}

#if defined(GCC)
#define NO_OPTIMIZE __attribute__((optimize("O0")))
#else
#define NO_OPTIMIZE
#endif

static int NO_OPTIMIZE check(const VTableTest &v) {
	// NOTE: We may eventually have to disable optimizations for this function for tests to work. If
	// the compiler inlines this function, it will see that it knows the type of 'v', and that it
	// does not have to use the vtable at all.
	return v.replace();
}

BEGIN_TEST(VTableCppTest, Storm) {
	Engine &e = gEngine();

	VTableTest a(10);
	VTableTest b(20);

	VTableCpp *tab = VTableCpp::copy(e, vtable::from(&a));
	CHECK_LT(tab->count(), nat(10));
	CHECK_GT(tab->count(), nat(2));

	nat slot = vtable::fnSlot(address(&VTableTest::replace));
	CHECK_NEQ(slot, vtable::invalid);

	tab->set(slot, address(&replaced));

	CHECK_EQ(a.replace(), 10);
	CHECK_EQ(b.replace(), 20);

	tab->insert(&a);

	CHECK_EQ(check(a), 20);
	CHECK_EQ(check(b), 20);

	tab->insert(&b);

	CHECK_EQ(check(a), 20);
	CHECK_EQ(check(b), 30);

} END_TEST

static int CODECALL extendReplace(debug::Extend *me) {
	// Note: we can not easily perform a super-call here.
	return 20;
}

static int CODECALL extendReplace2(debug::Extend *me) {
	// Note: we can not easily perform a super-call here.
	return 40;
}

static int CODECALL extendReplace3(debug::Extend *me) {
	// Note: we can not easily perform a super-call here.
	return 60;
}

static Type *addSubclass(Package *pkg, Type *base) {
	Type *sub = new (pkg) Type(pkg->anonName(), typeClass);
	pkg->add(sub);
	sub->setSuper(base);
	sub->add(new (pkg) TypeDefaultCtor(sub));
	return sub;
}

static Type *addSubclass(Package *pkg, Type *base, const wchar *name, const void *fn) {
	Type *sub = addSubclass(pkg, base);

	Array<Value> *params = new (pkg) Array<Value>(1, Value(sub));
	sub->add(nativeFunction(pkg->engine(), Value(StormInfo<Int>::type(pkg->engine())), name, params, fn));

	return sub;
}

static Function *addFn(Type *to, const wchar *name, const void *fn) {
	Array<Value> *params = new (to) Array<Value>(1, Value(to));
	Function *f = nativeFunction(to->engine, Value(StormInfo<Int>::type(to->engine)), name, params, fn);
	to->add(f);
	return f;
}

static bool usesVTable(Function *fn) {
	return fn->directRef().address() != fn->ref().address();
}

static Int callFn(Object *o, Function *fn) {
	typedef Int (*Ptr)(Object *);
	Ptr p = (Ptr)fn->ref().address();
	return (*p)(o);
}

BEGIN_TEST(VTableCppTest2, Storm) {
	Engine &e = gEngine();

	Type *extend = debug::Extend::stormType(e);
	Package *pkg = as<Package>(extend->parent());
	Function *value = as<Function>(extend->find(S("value"), Value(extend), Scope()));
	VERIFY(value);
	VERIFY(pkg);

	// Should not use vtable calls now.
	CHECK_EQ(value->ref().address(), value->directRef().address());


	// Add our own instantiation...
	Type *sub1 = addSubclass(pkg, extend, S("value"), address(&extendReplace));
	Function *value1 = as<Function>(sub1->find(S("value"), Value(sub1), Scope()));

	// Now, we should use vtable calls on the base class!
	CHECK(usesVTable(value));
	CHECK(!usesVTable(value1));


	// Add another subclass, subling to sub1.
	Type *sub2 = addSubclass(pkg, extend, S("value"), address(&extendReplace2));
	Function *value2 = as<Function>(sub2->find(S("value"), Value(sub2), Scope()));

	// VTable call on base class and first level derived.
	CHECK(usesVTable(value));
	CHECK(!usesVTable(value1));
	CHECK(!usesVTable(value2));

	// Add another subclass from sub1.
	Type *sub3 = addSubclass(pkg, sub1, S("value"), address(&extendReplace2));
	Function *value3 = as<Function>(sub3->find(S("value"), Value(sub3), Scope()));

	// VTable call on base class and first level derived.
	CHECK(usesVTable(value));
	CHECK(usesVTable(value1));
	CHECK(!usesVTable(value2));
	CHECK(!usesVTable(value3));

	// Instantiate a class and see if it is actually working!
	debug::Extend *o = new (e) debug::Extend(1);
	CHECK_EQ(o->value(), 1);
	CHECK_EQ(callFn(o, value), 1);
	sub1->vtable()->insert(o);
	CHECK_EQ(o->value(), 20);
	CHECK_EQ(callFn(o, value), 20);
	CHECK_EQ(callFn(o, value1), 20);
	sub2->vtable()->insert(o);
	CHECK_EQ(o->value(), 40);
	CHECK_EQ(callFn(o, value), 40);
	CHECK_EQ(callFn(o, value1), 40);
	CHECK_EQ(callFn(o, value2), 40);

	// Create actual objects!
	debug::Extend *p = (debug::Extend *)alloc(extend);
	CHECK_EQ(p->value(), 1);
	CHECK_EQ(callFn(p, value), 1);
	p = (debug::Extend *)alloc(sub1);
	CHECK_EQ(p->value(), 20);
	CHECK_EQ(callFn(p, value), 20);
	CHECK_EQ(callFn(p, value1), 20);
	p = (debug::Extend *)alloc(sub2);
	CHECK_EQ(p->value(), 40);
	CHECK_EQ(callFn(p, value), 40);
	CHECK_EQ(callFn(p, value1), 40);
	CHECK_EQ(callFn(p, value2), 40);
} END_TEST

BEGIN_TEST(VTableCppTest3, Storm) {
	Engine &e = gEngine();

	Type *base = debug::Extend::stormType(e);
	Package *pkg = as<Package>(base->parent());
	VERIFY(pkg);

	Type *sub1 = addSubclass(pkg, base);
	Type *sub2 = addSubclass(pkg, sub1);
	Type *sub3 = addSubclass(pkg, sub2);

	Function *f1 = addFn(sub1, S("val"), address(&extendReplace));
	Function *f2 = addFn(sub2, S("val"), address(&extendReplace2));
	Function *f3 = addFn(sub3, S("val"), address(&extendReplace3));

	CHECK(usesVTable(f1));
	CHECK(usesVTable(f2));
	CHECK(!usesVTable(f3));

	// Instantiate a class and make sure everything works as intended!
	debug::Extend *o = new (e) debug::Extend(1);
	sub1->vtable()->insert(o);
	CHECK_EQ(callFn(o, f1), 20);
	sub2->vtable()->insert(o);
	CHECK_EQ(callFn(o, f1), 40);
	CHECK_EQ(callFn(o, f2), 40);
	sub3->vtable()->insert(o);
	CHECK_EQ(callFn(o, f1), 60);
	CHECK_EQ(callFn(o, f2), 60);
	CHECK_EQ(callFn(o, f3), 60);
} END_TEST

BEGIN_TEST(VTableCppTest4, Storm) {
	Engine &e = gEngine();

	Type *base = debug::Extend::stormType(e);
	Package *pkg = as<Package>(base->parent());
	VERIFY(pkg);

	Type *sub1 = addSubclass(pkg, base);
	Type *sub2 = addSubclass(pkg, sub1);
	Type *sub3 = addSubclass(pkg, sub2);

	Function *f3 = addFn(sub3, S("val"), address(&extendReplace3));
	Function *f2 = addFn(sub2, S("val"), address(&extendReplace2));
	Function *f1 = addFn(sub1, S("val"), address(&extendReplace));

	CHECK(usesVTable(f1));
	CHECK(usesVTable(f2));
	CHECK(!usesVTable(f3));

	// Instantiate a class and make sure everything works as intended!
	debug::Extend *o = new (e) debug::Extend(1);
	sub1->vtable()->insert(o);
	CHECK_EQ(callFn(o, f1), 20);
	sub2->vtable()->insert(o);
	CHECK_EQ(callFn(o, f1), 40);
	CHECK_EQ(callFn(o, f2), 40);
	sub3->vtable()->insert(o);
	CHECK_EQ(callFn(o, f1), 60);
	CHECK_EQ(callFn(o, f2), 60);
	CHECK_EQ(callFn(o, f3), 60);
} END_TEST

// Try create a few pure Storm functions which will need VTable entries and see that it works.
BEGIN_TEST(VTableStormTest, Storm) {
	Engine &e = gEngine();

	Type *base = debug::Extend::stormType(e);
	Package *pkg = as<Package>(base->parent());
	VERIFY(pkg);

	// Note: *not* overriding 'value' in debug::Extend.
	Type *sub1 = addSubclass(pkg, base, S("val"), address(&extendReplace));
	Function *val1 = as<Function>(sub1->find(S("val"), Value(sub1), Scope()));

	CHECK(!usesVTable(val1));

	Type *sub2 = addSubclass(pkg, sub1, S("val"), address(&extendReplace2));
	Function *val2 = as<Function>(sub2->find(S("val"), Value(sub2), Scope()));

	CHECK(!usesVTable(val2));
	CHECK(usesVTable(val1));

	TODO(L"Instantiate a class and make sure everything works as intended!");
} END_TEST


// Create a class hierarchy, remove the middle class, split it into two and verify that everything
// looks right.
BEGIN_TEST(VTableSplit, Storm) {
	Engine &e = gEngine();

	Type *base = debug::Extend::stormType(e);
	Package *pkg = as<Package>(base->parent());
	Function *value = as<Function>(base->find(S("value"), Value(base), Scope()));
	VERIFY(pkg);
	VERIFY(value);

	// Create subclasses a, b and c.
	Type *a = addSubclass(pkg, base);
	Type *b = addSubclass(pkg, a);
	Type *c = addSubclass(pkg, b);

	// Add both 'value' and 'val' to them. Note: this violates the assumption that functions with
	// different names have different implementation, which is made by the vtable subsystem.
	Function *aValue = addFn(a, S("value"), address(&extendReplace));
	Function *bValue = addFn(b, S("value"), address(&extendReplace2));
	Function *cValue = addFn(c, S("value"), address(&extendReplace3));

	Function *aVal = addFn(a, S("val"), address(&extendReplace));
	Function *bVal = addFn(b, S("val"), address(&extendReplace2));
	Function *cVal = addFn(c, S("val"), address(&extendReplace3));

	// See so that everything is as we expect.
	CHECK(usesVTable(value));
	CHECK(usesVTable(aValue));
	CHECK(usesVTable(bValue));
	CHECK(!usesVTable(cValue));

	CHECK(usesVTable(aVal));
	CHECK(usesVTable(bVal));
	CHECK(!usesVTable(cVal));

	// Now, make 'b' into its own hierarchy.
	b->setSuper(null);

	// And see everything has been updated as we would expect.
	CHECK(usesVTable(value));
	CHECK(!usesVTable(aValue));
	CHECK(usesVTable(bValue));
	CHECK(!usesVTable(cValue));

	CHECK(!usesVTable(aVal));
	CHECK(usesVTable(bVal));
	CHECK(!usesVTable(cVal));

	// Note: we should check if functions have VTable slots as well.

	TODO(L"Make sure to update the vtable slots of classes which do not contain the function in question as well!");
} END_TEST

// Make sure that classes implemented only in C++ behave correctly as well.
// Earlier, this could be observed with the code::Arena class.
BEGIN_TEST(VTableCppOnly, Storm) {
	Engine &e = gEngine();

	// Create an arena (always the X86 arena for simplicity).
	code::Arena *arena = new (e) code::x86::Arena();

	// Find the type of the base class.
	Type *base = StormInfo<code::Arena>::type(e);

	// Try to call the 'firstParamLoc' function, and see which function is actually called.
	Array<Value> *params = new (e) Array<Value>(2, Value());
	params->at(0) = Value(base);
	params->at(1) = Value(StormInfo<Nat>::type(e));
	Function *call = as<Function>(base->find(S("firstParamLoc"), params, Scope()));

	Nat param = 0;
	os::FnCall<code::Operand, 2> fnParams = os::fnCall().add(arena).add(param);

	// The base-class implementation asserts, so it is easy to see which version was actually called.
	CHECK_RUNS(fnParams.call(call->pointer(), true));

} END_TEST;