File: TypeTransform.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 (191 lines) | stat: -rw-r--r-- 5,449 bytes parent folder | download | duplicates (2)
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
#include "stdafx.h"
#include "TypeTransform.h"
#include "Variable.h"
#include "Type.h"
#include "Engine.h"

namespace storm {

	TypeTransform::TypeTransform(Type *oldType, Type *newType)
		: oldT(oldType), newT(newType) {}

	void TypeTransform::added(MemberVar *var) {
		grow();

		variables->v[variables->filled].oldVar = null;
		variables->v[variables->filled].newVar = var;
		variables->v[variables->filled].cache = null;
		variables->filled++;
	}

	void TypeTransform::removed(MemberVar *var) {
		grow();

		variables->v[variables->filled].oldVar = var;
		variables->v[variables->filled].newVar = null;
		variables->v[variables->filled].cache = null;
		variables->filled++;
	}

	void TypeTransform::same(MemberVar *o, MemberVar *n) {
		grow();

		variables->v[variables->filled].oldVar = o;
		variables->v[variables->filled].newVar = n;
		variables->v[variables->filled].cache = null;
		variables->filled++;
	}

	void TypeTransform::grow() {
		const size_t scale = sizeof(MapItem) / sizeof(void *);

		if (!variables) {
			variables = runtime::allocArray<MapItem>(engine(), &pointerArrayType, 10 * scale);
			return;
		}

		Nat oldSize = Nat(variables->count / scale);
		if (variables->filled * 2 < oldSize)
			return;

		Nat newSize = Nat(variables->filled * 2);
		GcArray<MapItem> *copy = runtime::allocArray<MapItem>(engine(), &pointerArrayType, newSize * scale);
		memcpy(copy->v, variables->v, sizeof(MapItem) * oldSize);
		copy->filled = variables->filled;
		variables = copy;
	}

	RootObject *TypeTransform::apply(RootObject *old) {
		assert(newT->isClass(), L"Only classes are supported currently.");

		// TODO: We might want to generate code for parts of this. The cache makes it acceptable to
		// do even without compiled code.

		RootObject *copy = (RootObject *)runtime::allocObject(newT->size().current(), newT);

		// Set VTable:
		if (VTable *vtable = newT->vtable())
			vtable->insert(copy);

		// If it is a TObject, copy the thread. We don't expect that to be in the transforms.
		if (newT->isA(StormInfo<TObject>::type(engine()))) {
			size_t threadOffset = size_t(engine().ref(builtin::TObjectOffset).address());
			void *oldThread = (byte *)old + threadOffset;
			void *newThread = (byte *)copy + threadOffset;
			*(void **)newThread = *(void **)oldThread;
		}

		// Copy remaining things according to what is in 'variables':
		for (size_t i = 0; i < variables->filled; i++) {
			MapItem &var = variables->v[i];

			if (!var.newVar) {
				// Skip removed variables.
			} else if (!var.oldVar) {
				// Initialize new variables.
				initVar(copy, var.newVar, var.cache);
			} else {
				// Copy the rest.
				copyVar(copy, var.newVar, old, var.oldVar, var.cache);
			}
		}

		return copy;
	}

	void TypeTransform::initVar(RootObject *object, MemberVar *var, Function *&defaultCtor) {
		void *dest = (byte *)object + var->rawOffset().current();
		if (Function *init = var->initializer()) {
			// Call the initializer.
			os::CallThunk thunk = init->callThunk();
			void *params[1] = { null };
			(*thunk)(init->ref().address(), false, params, null, dest);
		} else {
			// Call the ctor. Cache the result to avoid multiple lookups.
			if (!defaultCtor)
				defaultCtor = var->type.type->defaultCtor();
			if (defaultCtor) {
				typedef void (*CtorFn)(void *);
				CtorFn c = (CtorFn)defaultCtor->ref().address();
				(*c)(dest);
			}
		}
	}

	void TypeTransform::copyVar(RootObject *to, MemberVar *toVar,
								RootObject *from, MemberVar *fromVar,
								Function *&copyCtor) {

		void *dest = (byte *)to + toVar->rawOffset().current();
		void *src = (byte *)from + fromVar->rawOffset().current();

		if (!toVar->type.isPrimitive()) {
			if (!copyCtor)
				copyCtor = toVar->type.type->copyCtor();
			if (copyCtor) {
				typedef void (*CtorCall)(void *, void *);
				CtorCall copy = (CtorCall)copyCtor->ref().address();
				(*copy)(dest, src);
				return;
			}
		}

		// Fall back to memcpy!
		memcpy(dest, src, toVar->type.size().current());
	}

	TypeTransform::Summary TypeTransform::summary() const {
		vector<Summary::Var> vars;

		for (size_t i = 0; i < variables->filled; i++) {
			const MapItem &var = variables->v[i];

			// Skip variables that did not exist previously.
			if (!var.oldVar)
				continue;

			Summary::Var v = {
				size_t(var.oldVar->rawOffset().current()),
				size_t(var.oldVar->type.size().current()),
				0
			};

			// Note: Removed variables are intentionally mapped to zero. Might not be ideal, but
			// currently we don't allow removing member variables, so it is fine.
			if (var.newVar)
				v.newStart = var.newVar->rawOffset().current();

			vars.push_back(v);
		}

		return Summary(oldT->size().current(), vars);
	}

	TypeTransform::Summary::Summary(size_t size, const vector<Var> &vars)
		: size(size), variables(vars) {

		std::sort(variables.begin(), variables.end());
	}

	size_t TypeTransform::Summary::translate(size_t x) const {
		vector<Var>::const_iterator found = std::lower_bound(variables.begin(), variables.end(), x);
		if (found != variables.end() && found->start == x) {
			// Use 'found' as it is.
		} else if (found == variables.begin()) {
			// No previous element, bail out. This should not happen.
			return x;
		} else {
			// Use the previous element.
			--found;
		}

		size_t varOffset = x - found->start;

		// Inside the variable? If not, should not really happen...
		if (varOffset > found->size)
			return x;

		return found->newStart + varOffset;
	}

}