File: VTableCpp.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • 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 (216 lines) | stat: -rw-r--r-- 5,131 bytes parent folder | download
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
#include "stdafx.h"
#include "VTableCpp.h"
#include "Utils/Platform.h"
#include "Utils/Memory.h"
#include "Core/GcType.h"
#include "Exception.h"
#include "Function.h"
#include "Engine.h"

namespace storm {

	VTableCpp *VTableCpp::wrap(Engine &e, const void *vtable) {
		return new (e) VTableCpp(vtable, vtable::count(vtable), false);
	}

	VTableCpp *VTableCpp::wrap(Engine &e, const void *vtable, nat count) {
		return new (e) VTableCpp(vtable, count, false);
	}

	VTableCpp *VTableCpp::copy(Engine &e, const void *vtable) {
		return new (e) VTableCpp(vtable, vtable::count(vtable), true);
	}

	VTableCpp *VTableCpp::copy(Engine &e, const void *vtable, nat count) {
		return new (e) VTableCpp(vtable, count, true);
	}

	VTableCpp *VTableCpp::copy(Engine &e, const VTableCpp *src) {
		return new (e) VTableCpp(src->table(), src->count(), true);
	}

	VTableCpp::VTableCpp(const void *vtable, nat count, bool copy) {
		init(vtable, count, copy);
	}

	void VTableCpp::init(const void *vtable, nat count, bool copy) {
		refs = null;
		data = null;
		tabSize = count;
		raw = null;

		if (copy) {
			data = runtime::allocArray<const void *>(engine(), &pointerArrayType, count + vtable::extraOffset);
			data->filled = 0;

			const void ** src = (const void **)vtable - vtable::extraOffset;
			for (nat i = 1; i < count + vtable::extraOffset; i++) {
				data->v[i] = src[i];
			}
		} else {
			raw = (const void **)vtable;
		}
	}

	const void **VTableCpp::table() const {
		if (data)
			return &data->v[vtable::extraOffset];
		else
			return raw;
	}

	void VTableCpp::replace(const void *vtable) {
		replace(vtable, vtable::count(vtable));
	}

	void VTableCpp::replace(const VTableCpp *src) {
		replace(src->table(), src->count());
	}

	class VTableSwitch : public Walker {
	public:
		VTableSwitch(const void **from, const void **to) : from(from), to(to) {
			flags = fObjects;
		}

		const void **from;
		const void **to;

		virtual void object(RootObject *inspect) {
			if (vtable::from(inspect) == from)
				vtable::set(to, inspect);
		}
	};

	void VTableCpp::replace(const void *vtable, nat count) {
		if (!data || count > this->count()) {
			// We need to replace the vtables on all live objects using the vtable.
			bool needWalk = used();
			const void **from = table();

			// Create the new vtable.
			init(vtable, count, true);

			VTableSwitch data(from, table());

			// Don't walk the heap if we don't need to. That can be *very* expensive. In most cases
			// this happens right after we've created an object but before we have set the vtable to
			// an object, which means it is safe not to do the expensive heap walk.
			if (needWalk)
				engine().gc.walk(data);
		} else {
			// We can copy, we know we can modify 'data'.
			const void *const* src = (const void *const*)vtable - vtable::extraOffset;
			for (nat i = 1; i < count + vtable::extraOffset; i++) {
				data->v[i] = src[i];
			}
		}
	}

	nat VTableCpp::count() const {
		return tabSize;
	}

	nat VTableCpp::findSlot(const void *fn) const {
		return vtable::find(table(), fn, count());
	}

	const void *VTableCpp::extra() const {
		if (data)
			return data->v[0];
		else
			return null;
	}

	void VTableCpp::extra(const void *to) {
		if (data)
			data->v[0] = to;
	}

	void VTableCpp::set(nat id, const void *to) {
		if (data)
			data->v[id + vtable::extraOffset] = to;
	}

	void VTableCpp::set(nat id, Function *fn) {
		assert(id < count());

		if (!refs)
			refs = runtime::allocArray<Function *>(engine(), &pointerArrayType, count());

		refs->v[id] = fn;
		set(id, fn->directRef().address());
	}

	Function *VTableCpp::get(nat id) const {
		if (refs)
			if (id < count())
				return refs->v[id];

		return null;
	}

	void VTableCpp::clear(nat id) {
		assert(id < count());

		if (refs) {
			refs->v[id] = null;
		}
		if (data)
			data->v[id + vtable::extraOffset] = null;
	}

	const void *VTableCpp::address() const {
		if (data)
			return data;
		else
			// Tag the pointer.
			return (const byte *)raw + 1;
	}

	nat VTableCpp::size() const {
		return Nat(count() * sizeof(const void *) + vtableAllocOffset());
	}

	bool VTableCpp::used() const {
		if (data)
			return data->filled != 0;
		else
			return true;
	}

	const void **VTableCpp::pointer() {
		if (data)
			data->filled = 1;
		return table();
	}

	void VTableCpp::insert(RootObject *obj) {
		if (data)
			data->filled = 1;
		vtable::set(table(), obj);
	}

	void VTableCpp::insert(code::Listing *to, code::Var obj, code::Ref table) {
		using namespace code;
		Label lbl = to->label();

		// See if the pointer in 'table' is tagged with a 1 in the lower bit. If so, remove it and
		// skip the mark-phase since we're dealing with a raw vtable.
		*to << mov(ptrA, table);
		*to << mov(ptrC, ptrConst(1));
		*to << bnot(ptrC);
		*to << band(ptrC, ptrA);
		*to << cmp(ptrC, ptrA);
		*to << jmp(lbl, ifNotEqual);

		// Mark as used by setting 'filled' to 1.
		*to << mov(ptrRel(ptrA, Offset::sPtr), ptrConst(1));
		// The pointer is offset a bit.
		*to << add(ptrA, to->engine().ref(builtin::VTableAllocOffset));

		*to << lbl;
		*to << mov(ptrC, obj);
		*to << mov(ptrRel(ptrC, Offset()), ptrA);
	}
}