File: Code.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 (301 lines) | stat: -rw-r--r-- 7,100 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
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
#include "stdafx.h"
#include "Code.h"
#include "Engine.h"
#include "Function.h"
#include "Exception.h"
#include "Engine.h"
#include "Core/Str.h"

namespace storm {

	Code::Code() : toUpdate(null), owner(null) {}

	void Code::attach(Function *to) {
		owner = to;
	}

	void Code::detach() {
		owner = null;
		if (toUpdate)
			toUpdate->clear();
		toUpdate = null;
	}

	void Code::update(code::RefSource *update) {
		if (toUpdate == update)
			return;
		if (toUpdate)
			toUpdate->clear();
		toUpdate = update;
		newRef();
	}

	void Code::compile() {}

	void Code::newRef() {}


	/**
	 * Static code.
	 */

	StaticCode::StaticCode(const void *ptr) : ptr(ptr) {}

	void StaticCode::newRef() {
		toUpdate->setPtr(ptr);
	}


	/**
	 * Delegated code.
	 */

	DelegatedCode::DelegatedCode(code::Ref ref) {
		content = new (this) code::DelegatedContent(ref);
	}

	void DelegatedCode::newRef() {
		toUpdate->set(content);
	}

	code::Ref DelegatedCode::to() const {
		return content->to();
	}


	/**
	 * Static engine code.
	 */

	StaticEngineCode::StaticEngineCode(const void *src) {
		// TODO: Better reference somehow?
		original = new (this) code::StrRefSource(S("ref-to"));
		original->setPtr(src);

		code = null;
	}

	void StaticEngineCode::newRef() {
		if (!code) {
			code::Listing *l = redirectCode(original);
			code = new (this) code::Binary(engine().arena(), l);
		}
		toUpdate->set(code);
	}

	code::Listing *StaticEngineCode::redirectCode(code::Ref ref) {
		Engine &e = engine();
		assert(owner);
		using code::TypeDesc;

		Array<TypeDesc *> *p = new (this) Array<TypeDesc *>();
		Array<Value> *params = owner->params;
		for (Nat i = 0; i < params->count(); i++)
			p->push(params->at(i).desc(e));
		Value result = owner->result;

		return e.arena()->engineRedirect(result.desc(e), p, ref, e.ref(builtin::engine));
	}

	/**
	 * GeneratedCode.
	 */

	MAYBE(code::Listing *) GeneratedCode::source() {
		return null;
	}

	void GeneratedCode::discardSource() {}


	/**
	 * Dynamic code.
	 */

	DynamicCode::DynamicCode(code::Listing *code) : code(code) {
		binary = new (this) code::Binary(engine().arena(), code);
	}

	MAYBE(code::Listing *) DynamicCode::source() {
		return code;
	}

	void DynamicCode::discardSource() {
		code = null;
	}

	void DynamicCode::newRef() {
		toUpdate->set(binary);
	}


	/**
	 * Lazy code.
	 */

	LazyCode::LazyCode(Fn<CodeGen *> *generate) : binary(null), sourceData(generate), state(sUnloaded) {}

	void LazyCode::compile() {
		// We're always running on the Compiler thread, so it is safe to call 'updateCodeLocal'.
		if ((state & sMask) != sLoading)
			updateCodeLocal(this);
	}

	MAYBE(code::Listing *) LazyCode::source() {
		// Load our code if we need to.
		if ((state & sMask) != sLoaded)
			updateCodeLocal(this);

		if (state & sDiscardSource)
			return null;
		else
			return (code::Listing *)sourceData;
	}

	void LazyCode::discardSource() {
		state |= sDiscardSource;

		if ((state & sMask) == sLoaded)
			sourceData = null;
	}

	void LazyCode::newRef() {
		if (!binary)
			createRedirect();
		toUpdate->set(binary);
	}

	void LazyCode::createRedirect() {
		state = (state & ~sMask) | sUnloaded;
		Engine &e = engine();
		using code::TypeDesc;

		Array<TypeDesc *> *params = new (this) Array<TypeDesc *>();
		for (Nat i = 0; i < owner->params->count(); i++) {
			params->push(owner->params->at(i).desc(e));
		}

		Bool member = owner->isMember();
		TypeDesc *result = owner->result.desc(e);
		code::Ref fn = e.ref(builtin::lazyCodeUpdate);
		setCode(e.arena()->redirect(member, result, params, fn, code::objPtr(this)));
	}

	void LazyCode::setCode(code::Listing *to) {
		binary = new (this) code::Binary(engine().arena(), to);
		if (toUpdate)
			toUpdate->set(binary);
	}

	const void *LazyCode::updateCode(LazyCode *me) {
		// TODO? Always allocate a new UThread? This will make sure we don't run out of stack for the compiler.
		Thread *cThread = Compiler::thread(me->engine());
		if (cThread->thread() == os::Thread::current()) {
			// If we're on the Compiler thread, we may call directly.
			return updateCodeLocal(me);
		} else {
			// Note: we're blocking the calling thread entirely since we would otherwise possibly
			// let other UThreads run where they are not expected to.
			os::Future<const void *> result(os::FutureMode::exclusive);
			os::FnCall<const void *, 1> params = os::fnCall().add(me);
			os::UThread::spawn(address(&LazyCode::updateCodeLocal), true, params, result, &cThread->thread());
			return result.result(&updateFutureExceptions, null);
		}
	}

	const void *LazyCode::updateCodeLocal(LazyCode *me) {
		while ((me->state & sMask) == sLoading) {
			// Wait for the other one loading this function.
			// TODO: Try to detect when a function is recursively loaded!
			os::UThread::leave();
		}

		if ((me->state & sMask) != sLoaded) {
			if ((me->state & sMask) == sLoading) {
				Str *msg = TO_S(me, S("Trying to update ") << me->owner->identifier() << S(" recursively."));
				throw new (me) InternalError(msg);
			}

			me->state = (me->state & ~sMask) | sLoading;

			try {
				CodeGen *l = me->generate()->call();
				// Append any data needed.
				me->setCode(l->l);

				// Store the listing unless we don't need it anymore.
				if (me->state & sDiscardSource)
					me->sourceData = null;
				else
					me->sourceData = l->l;
			} catch (...) {
				me->state = (me->state & ~sMask) | sUnloaded;
				throw;
			}

			me->state = (me->state & ~sMask) | sLoaded;
		}

		return me->binary->address();
	}


	/**
	 * Inline code.
	 */

	InlineCode::InlineCode(Fn<void, InlineParams> *create) :
		LazyCode(fnPtr(TObject::engine(), &InlineCode::generatePtr, this)), create(create) {}

	void InlineCode::code(CodeGen *state, Array<code::Operand> *params, CodeResult *result) {
		InlineParams p(state, params, result);
		create->call(p);
	}

	CodeGen *InlineCode::generatePtr() {
		using namespace code;

		CodeGen *state = new (this) CodeGen(owner->runOn(), owner->isMember(), owner->result);
		Listing *l = state->l;

		Array<code::Operand> *params = new (this) Array<code::Operand>();
		for (nat i = 0; i < owner->params->count(); i++) {
			Value p = owner->params->at(i);
			params->push(state->createParam(p));
		}

		*l << prolog();

		if (owner->result == Value()) {
			CodeResult *result = new (this) CodeResult();
			code(state, params, result);
			state->returnValue(code::Var());
		} else {
			CodeResult *result = new (this) CodeResult(owner->result, l->root());
			code(state, params, result);
			state->returnValue(result->location(state));
		}

		return state;
	}

	Code *STORM_FN abstractThrowCode(Value result, Array<Value> *params, Str *name) {
		using namespace code;
		Engine &e = params->engine();

		CodeGen *g = new (e) CodeGen(RunOn(), true, result);
		for (Nat i = 0; i < params->count(); i++)
			g->createParam(params->at(i));

		*g->l << prolog();
		*g->l << fnParam(e.ptrDesc(), objPtr(name));
		*g->l << fnCall(e.ref(builtin::throwAbstractError), false);

		// 'throwAbstractError' does not return, but to be sure.
		*g->l << epilog();
		*g->l << ret(Size());

		return new (e) DynamicCode(g->l);
	}

}