File: Function.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 (889 lines) | stat: -rw-r--r-- 25,643 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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
#include "stdafx.h"
#include "Function.h"
#include "Type.h"
#include "Engine.h"
#include "Exception.h"
#include "Code.h"
#include "Core/Str.h"
#include "Lib/Future.h"
#include "Lib/Clone.h"
#include "ReplaceActive.h"
#include "Compiler/Lib/Io.h"

namespace storm {

	Function::Function(Value result, Str *name, Array<Value> *params) :
		Named(name, params),
		result(result),
		lookupRef(null),
		codeRef(null),
		runOnThread(null),
		toReplace(null),
		myFlags(fnNone) {}

	Function::Function(SrcPos pos, Value result, Str *name, Array<Value> *params) :
		Named(name, params),
		result(result),
		lookupRef(null),
		codeRef(null),
		runOnThread(null),
		toReplace(null),
		myFlags(fnNone) {

		this->pos = pos;
	}


	const void *Function::pointer() {
		return ref().address();
	}

	code::Ref Function::ref() {
		initRefs();
		return code::Ref(lookupRef);
	}

	code::Ref Function::directRef() {
		initRefs();
		return code::Ref(codeRef);
	}

	Bool Function::isMember() {
		if (fnFlags() & fnStatic)
			return false;

		if (params->empty())
			return false;

		Type *first = params->at(0).type;
		return first == parent();
	}

	RunOn Function::runOn() const {
		RunOn result;
		if (Type *t = as<Type>(parent())) {
			result = t->runOn();
		}

		if (result.state != RunOn::any)
			return result;

		// The function itself can override if the parent class says "run on any thread".
		if (runOnThread) {
			return RunOn(runOnThread);
		} else {
			return RunOn(RunOn::any);
		}
	}

	void Function::runOn(NamedThread *t) {
		runOnThread = t;
	}

	Bool Function::pure() const {
		return (myFlags & fnPure) != 0;
	}

	Function *Function::make(FnFlags flag) {
		myFlags |= flag;
		return this;
	}

	FnFlags Function::fnFlags() const {
		return myFlags;
	}

	void Function::toS(StrBuf *to) const {
		*to << result << S(" ");
		Named::toS(to);

		if (myFlags != fnNone) {
			*to << S(" :");
			if (myFlags & fnPure)
				*to << S(" pure");
			if (myFlags & fnAutoCast)
				*to << S(" cast");
			if (myFlags & fnAssign)
				*to << S(" assign");
			if (myFlags & fnFinal)
				*to << S(" final");
			if (myFlags & fnAbstract)
				*to << S(" abstract");
			if (myFlags & fnOverride)
				*to << S(" override");
			if (myFlags & fnStatic)
				*to << S(" static");
		}
	}

	MAYBE(Str *) Function::canReplace(Named *old, ReplaceContext *ctx) {
		Function *oldFn = as<Function>(old);
		if (!oldFn)
			return new (this) Str(S("Unable to replace something that is not a function with a function."));

		if (params->count() != oldFn->params->count())
			return new (this) Str(S("Cannot modify the number of formal parameters to a function."));
		for (Nat i = 0; i < params->count(); i++)
			if (!ctx->normalize(params->at(i)).mayStore(ctx->normalize(oldFn->params->at(i))))
				return new (this) Str(S("Cannot modify the types of formal parameters to incompatible types."));
		if (!ctx->normalize(oldFn->result).mayStore(ctx->normalize(result)))
			return new (this) Str(S("Cannot modify the return type of a function to an incompatible type."));

		return null;
	}

	static void addOldFunction(Engine &e, GcWeakArray<const void> *&array, const void *ptr) {
		size_t newCount = 0;
		if (array) {
			// Note: Safe to not use atomics here - this is an upper bound anyway.
			for (size_t i = 0; i < array->count(); i++)
				if (array->v[i])
					newCount++;
		}

		GcWeakArray<const void> *n = runtime::allocWeakArray<const void>(e, newCount + 1);

		size_t target = 0;
		if (array) {
			for (size_t i = 0; i < array->count(); i++) {
				if (const void *p = array->read(i))
					n->write(target++, p);
			}
		}

		n->write(target++, ptr);
		array = n;
	}

	void Function::doReplace(Named *old, ReplaceTasks *tasks, ReplaceContext *ctx) {
		Function *f = (Function *)old;

		// Store the old pointer to the function so that we can replace it if necessary.
		// Note: We ignore 'lookup' completely - we don't expect much to happen there.
		const void *oldCode = null;
		if (f->codeRef) {
			// Only consider cases where the code is GeneratedCode. We might eventually also want to
			// consider "DelegatedCode".
			if (as<GeneratedCode>(f->code)) {
				oldCode = f->codeRef->address();
			}
		}

		// Detach the code refs.
		if (f->code)
			f->code->detach();
		if (f->lookup)
			f->lookup->detach();

		// Create our references if needed.
		if (f->codeRef || f->lookupRef)
			initRefs();

		// Steal the references.
		if (f->codeRef)
			codeRef->steal(f->codeRef);
		if (f->lookupRef)
			lookupRef->steal(f->lookupRef);

		// Steal 'toReplace' so that we forward the backlog if any.
		toReplace = f->toReplace;

		f->codeRef = null;
		f->lookupRef = null;
		f->toReplace = null;

		// See if we need to update our old version:
		if (oldCode) {
			addOldFunction(engine(), toReplace, oldCode);
		}

		// Anything old to replace, either from here or from old versions?
		if (toReplace) {
			tasks->replaceActive(this);
		}
	}

	void Function::replaceActive(ReplaceTasks *tasks) {
		// There are two failure modes here. One is if 'replaceActiveFunction' returns false.
		// That is more of an implementation error, so we mostly ignore it. It may also throw an
		// exception (e.g. if it fails to compile this function). If so, we want to remember the
		// old function so that we can replace it in the future.

		try {
			// Note: we don't care if we fail to replace the function. That only means that the old
			// version will run until completion, and the new one will be used for new calls.
			if (toReplace) {
				for (size_t i = 0; i < toReplace->count(); i++) {
					const void *fn = toReplace->read(i);
					if (fn)
						replaceActiveFunction(fn, this, tasks);
					toReplace->write(i, null);
				}
			}
			// All of them are now updated, remove the array.
			toReplace = null;

		} catch (storm::Exception *e) {
			// Store the error.
			if (CodeError *c = as<CodeError>(e)) {
				tasks->error(new (this) ActiveReplaceError(c, this));
			} else {
				tasks->error(e);
			}
		}
	}

	void Function::setCode(Code *code) {
		if (this->code)
			this->code->detach();
		code->attach(this);
		this->code = code;
		if (codeRef)
			this->code->update(codeRef);
	}

	Code *Function::getCode() const {
		return code;
	}

	void Function::setLookup(MAYBE(Code *) code) {
		if (lookup)
			lookup->detach();

		if (code == null && codeRef != null)
			lookup = new (this) DelegatedCode(code::Ref(codeRef));
		else
			lookup = code;

		if (lookup) {
			lookup->attach(this);
			if (lookupRef)
				lookup->update(lookupRef);
		}
	}

	Code *Function::getLookup() const {
		return lookup;
	}

	void Function::compile() {
		if (code)
			code->compile();
		if (lookup)
			lookup->compile();
	}

	void Function::discardSource() {
		if (GeneratedCode *c = as<GeneratedCode>(code))
			c->discardSource();
		if (GeneratedCode *l = as<GeneratedCode>(lookup))
			l->discardSource();
	}

	const void *Function::originalPtr() {
		return directRef().address();
	}

	void Function::initRefs() {
		if (!codeRef) {
			codeRef = new (this) NamedSource(this, Char('d'));
			if (code)
				code->update(codeRef);
		}

		if (!lookupRef) {
			lookupRef = new (this) NamedSource(this);
			if (!lookup) {
				lookup = new (this) DelegatedCode(code::Ref(codeRef));
				lookup->attach(this);
			}
			lookup->update(lookupRef);
		}
	}

	/**
	 * Function calls.
	 */

	code::Var Function::findThread(CodeGen *s, Array<code::Operand> *params) {
		using namespace code;

		RunOn on = runOn();
		assert(on.state != RunOn::any, L"Only use 'findThread' on functions which 'runOn()' return something other than any.");

		Var r = s->l->createVar(s->block, Size::sPtr);

		switch (on.state) {
		case RunOn::runtime:
			// Should be a this-ptr. Does not work well for constructors.
			assert(*name != Type::CTOR,
				L"Please overload 'findThread' for your constructor '" + ::toS(identifier()) + L"'!");
			*s->l << mov(ptrA, params->at(0));
			*s->l << add(ptrA, engine().ref(builtin::TObjectOffset));
			*s->l << mov(r, ptrRel(ptrA, Offset()));
			break;
		case RunOn::named:
			*s->l << mov(r, on.thread->ref());
			break;
		default:
			assert(false, L"Unknown state.");
			break;
		}

		return r;
	}

	void Function::autoCall(CodeGen *to, Array<code::Operand> *params, CodeResult *result) {
		RunOn r = runOn();
		if (to->runOn.canRun(r)) {
			localCall(to, params, result, true);
		} else {
			code::Var v = findThread(to, params);
			threadCall(to, params, result, v);
		}
	}

	void Function::autoCallRef(CodeGen *to, Array<code::Operand> *params, code::Operand result) {
		RunOn r = runOn();
		if (to->runOn.canRun(r)) {
			localCallRef(to, params, result, true);
		} else {
			code::Var v = findThread(to, params);
			threadCallRef(to, params, result, v);
		}
	}

	void Function::localCall(CodeGen *to, Array<code::Operand> *params, CodeResult *result, Bool useLookup) {
		initRefs();
		if (params->count() != this->params->count()) {
			Str *msg = TO_S(engine(), S("Parameter count mismatch when calling ") << identifier()
							<< S(". Got ") << params->count() << S(" parameter(s)."));
			throw new (this) InternalError(msg);
		}

		InlineCode *inlined = as<InlineCode>(code);
		// If we're not going to use the lookup, we may choose to inline sooner.
		if (useLookup && as<DelegatedCode>(lookup) == null)
			inlined = null;

		if (inlined) {
			// TODO: We might want to create a new block here.
			inlined->code(to, params, result);
		} else {
			localCall(to, params, result, useLookup ? this->ref() : directRef());
		}
	}

	void Function::localCallRef(CodeGen *to, Array<code::Operand> *params, code::Operand result, Bool useLookup) {
		initRefs();
		if (params->count() != this->params->count()) {
			Str *msg = TO_S(engine(), S("Parameter count mismatch when calling ") << identifier()
							<< S(". Got ") << params->count() << S(" parameter(s)."));
			throw new (this) InternalError(msg);
		}

		InlineCode *inlined = as<InlineCode>(code);
		// If we're not going to use the lookup, we may choose to inline sooner.
		if (useLookup && as<DelegatedCode>(lookup) == null)
			inlined = null;

		if (inlined) {
			if (this->result.isAsmType()) {
				using namespace code;

				// TODO: We might want to create a new block here.
				CodeResult *r = new (this) CodeResult(this->result, to->block);
				inlined->code(to, params, r);
				*to->l << mov(ptrA, result);

				code::Var rVar = r->location(to);
				*to->l << mov(xRel(rVar.size(), ptrA, Offset()), rVar);
			} else {
				// Fall back on non-inlined version if we can't copy the result in-place.
				localCallRef(to, params, result, useLookup ? this->ref() : directRef());
			}
		} else {
			localCallRef(to, params, result, useLookup ? this->ref() : directRef());
		}
	}

	void Function::localCall(CodeGen *to, Array<code::Operand> *params, CodeResult *res, code::Ref ref) {
		using namespace code;

		addParams(to, params);

		if (result == Value()) {
			*to->l << fnCall(ref, isMember());
		} else {
			code::Var rVar = res->safeLocation(to, result);
			*to->l << fnCall(ref, isMember(), result.desc(engine()), rVar);
			res->created(to);
		}
	}

	void Function::localCallRef(CodeGen *to, Array<code::Operand> *params, code::Operand res, code::Ref ref) {
		using namespace code;

		addParams(to, params);

		if (result == Value()) {
			*to->l << fnCall(ref, isMember());
		} else {
			*to->l << fnCallRef(ref, isMember(), result.desc(engine()), res);
		}
	}

	void Function::addParams(CodeGen *to, Array<code::Operand> *params) {
		for (nat i = 0; i < params->count(); i++) {
			addParam(to, params, i);
		}
	}

	void Function::addParam(CodeGen *to, Array<code::Operand> *params, nat id) {
		Value p = this->params->at(id);
		if (!p.ref && p.isValue()) {
			if (params->at(id).type() == code::opVariable) {
				*to->l << fnParam(p.desc(engine()), params->at(id).var());
			} else {
				// Has to be a reference to the value...
				*to->l << fnParamRef(p.desc(engine()), params->at(id));
			}
		} else {
			*to->l << fnParam(p.desc(engine()), params->at(id));
		}
	}

	static inline code::Operand toOp(bool v) {
		return code::byteConst(v ? 1 : 0);
	}

	void Function::threadCall(CodeGen *to, Array<code::Operand> *params, CodeResult *res, code::Operand thread) {
		using namespace code;

		Engine &e = engine();
		CodeGen *sub = to->child();
		*to->l << begin(sub->block);

		// Spill any registers to memory if necessary...
		params = spillRegisters(sub, params);

		// Create the parameters.
		Var par = createFnCall(sub, this->params, params, true);

		// Where shall we store the result (store the pointer to it in ptrB)?
		code::Var resultPos;
		if (this->result == Value()) {
			// null-pointer.
			*to->l << mov(ptrB, ptrConst(Offset(0)));
		} else {
			// Note: We always create the result in the parent scope if we need to, otherwise we
			// will fail to clone it later on.
			resultPos = res->safeLocation(to, this->result);
			*to->l << lea(ptrB, resultPos);
		}

		Ref thunk = Ref(threadThunk());

		TypeDesc *ptr = e.ptrDesc();

		// Spawn the thread!
		*to->l << lea(ptrA, par);
		*to->l << fnParam(ptr, ref()); // fn
		*to->l << fnParam(byteDesc(e), toOp(isMember())); // member
		*to->l << fnParam(ptr, thunk); // thunk
		*to->l << fnParam(ptr, ptrA); // params
		*to->l << fnParam(ptr, ptrB); // result
		*to->l << fnParam(ptr, thread); // on
		*to->l << fnCall(e.ref(builtin::spawnResult), false);

		*to->l << end(sub->block);

		if (resultPos != code::Var())
			res->created(to);

		// Clone the result.
		if (result.isValue() && !result.isPrimitive()) {
			if (Function *f = result.type->deepCopyFn()) {
				CodeGen *child = to->child();
				*to->l << begin(child->block);

				Var env = allocObject(child, CloneEnv::stormType(e));
				*to->l << lea(ptrA, resultPos);
				*to->l << fnParam(ptr, ptrA);
				*to->l << fnParam(ptr, env);
				*to->l << fnCall(f->ref(), true);

				*to->l << end(child->block);
			}
		} else if (result.isClass()) {
			*to->l << fnParam(ptr, resultPos);
			*to->l << fnCall(cloneFn(result.type)->ref(), false, ptr, resultPos);
		}

	}

	void Function::threadCallRef(CodeGen *to, Array<code::Operand> *params, code::Operand res, code::Operand thread) {
		using namespace code;

		Engine &e = engine();
		CodeGen *sub = to->child();
		*to->l << begin(sub->block);

		// Spill any registers to memory if necessary...
		params = spillRegisters(sub, params);

		// Create the parameters.
		Var par = createFnCall(sub, this->params, params, true);

		Ref thunk = Ref(threadThunk());

		TypeDesc *ptr = e.ptrDesc();

		// Spawn the thread!
		*to->l << lea(ptrA, par);
		*to->l << fnParam(ptr, ref()); // fn
		*to->l << fnParam(byteDesc(e), toOp(isMember())); // member
		*to->l << fnParam(ptr, thunk); // thunk
		*to->l << fnParam(ptr, ptrA); // params
		*to->l << fnParam(ptr, res); // result
		*to->l << fnParam(ptr, thread); // on
		*to->l << fnCall(e.ref(builtin::spawnResult), false);

		*to->l << end(sub->block);

		// Clone the result.
		if (result.isValue() && !result.isPrimitive()) {
			if (Function *f = result.type->deepCopyFn()) {
				CodeGen *child = to->child();
				*to->l << begin(child->block);

				Var env = allocObject(child, CloneEnv::stormType(e));
				*to->l << fnParam(ptr, res);
				*to->l << fnParam(ptr, env);
				*to->l << fnCall(f->ref(), true);

				*to->l << end(child->block);
			}
		} else if (result.isClass()) {
			*to->l << mov(ptrA, res);
			*to->l << fnParam(ptr, ptrRel(ptrA, Offset()));
			*to->l << fnCallRef(cloneFn(result.type)->ref(), false, ptr, res);
		}

	}

	void Function::asyncAutoCall(CodeGen *to, Array<code::Operand> *params, CodeResult *result) {
		asyncAutoCall(to, params, result, null);
	}

	void Function::asyncAutoCall(CodeGen *to, Array<code::Operand> *params, CodeResult *result, CodeResult *id) {
		RunOn r = runOn();
		if (to->runOn.canRun(r)) {
			asyncLocalCall(to, params, result, id);
		} else {
			asyncThreadCall(to, params, result, id);
		}
	}

	void Function::asyncLocalCall(CodeGen *to, Array<code::Operand> *params, CodeResult *result) {
		asyncLocalCall(to, params, result, null);
	}

	void Function::asyncLocalCall(CodeGen *to, Array<code::Operand> *params, CodeResult *result, CodeResult *id) {
		asyncCall(to, params, code::Operand(), result, id, false);
	}

	void Function::asyncThreadCall(CodeGen *to, Array<code::Operand> *params, CodeResult *result) {
		asyncThreadCall(to, params, result, code::Operand(), null);
	}

	void Function::asyncThreadCall(CodeGen *to, Array<code::Operand> *params, CodeResult *result, code::Operand thread) {
		asyncThreadCall(to, params, result, thread, null);
	}

	void Function::asyncThreadCall(CodeGen *to, Array<code::Operand> *params, CodeResult *result, CodeResult *id) {
		asyncThreadCall(to, params, result, code::Operand(), id);
	}

	void Function::asyncThreadCall(CodeGen *to, Array<code::Operand> *params, CodeResult *result, code::Operand thread, CodeResult *id) {
		asyncCall(to, params, thread, result, id, true);
	}

	void Function::asyncCall(CodeGen *to, Array<code::Operand> *params, code::Operand thread,
							CodeResult *result, CodeResult *id, bool copy) {
		using namespace code;

		// Figure out what thread to pass to the spawn function.
		if (thread.empty())
			thread = ptrConst(0);
		if (runOn().state != RunOn::any)
			thread = findThread(to, params);

		// Not technically necessary to do before calling 'safeLocation', but we do it anyway for future-proofing.
		Bool resultNeeded = result->needed();

		// Check if we can call the 'asyncCallVoid' specialization:
		if (!resultNeeded && this->result == Value()) {
			asyncCallVoid(to, params, thread, id, copy);
			return;
		}

		Engine &e = engine();
		CodeGen *sub = to->child();
		*to->l << begin(sub->block);

		// Spill any registers to memory if necessary...
		params = spillRegisters(sub, params);

		// Create the parameters.
		Var par = createFnCall(sub, this->params, params, copy);

		// Create the result object.
		Type *futureT = wrapFuture(e, this->result).type;
		code::Var resultPos = result->safeLocation(sub, thisPtr(futureT));
		allocObject(sub, futureT->defaultCtor(), new (this) Array<Operand>(), resultPos);
		result->created(sub);

		// If we don't need copies, tell the future that as well.
		if (!copy) {
			*to->l << fnParam(e.ptrDesc(), resultPos);
			*to->l << fnCall(e.ref(builtin::futureNoClone), true);
		}

		// Get the thunk.
		Ref thunk = Ref(threadThunk());

		TypeDesc *ptr = e.ptrDesc();

		// Spawn the thread!
		*to->l << lea(ptrA, par);
		*to->l << fnParam(ptr, ref()); // fn
		*to->l << fnParam(byteDesc(e), toOp(isMember())); // member
		*to->l << fnParam(ptr, thunk); // thunk
		*to->l << fnParam(ptr, ptrA); // params
		*to->l << fnParam(ptr, resultPos); // result
		*to->l << fnParam(ptr, thread); // on

		// Take care of "id" if we need it.
		if (id) {
			*to->l << fnCall(e.ref(builtin::spawnFutureId), false, longDesc(e), rax);
			if (id->needed())
				*to->l << mov(id->location(to), rax);
		} else {
			*to->l << fnCall(e.ref(builtin::spawnFuture), false);
		}

		// If the result was not used, call 'detach' on the future:
		if (!resultNeeded) {
			Function *detachFn = as<Function>(futureT->find(S("detach"), Value(futureT), Scope()));
			if (detachFn) {
				*to->l << fnParam(ptrDesc(e), resultPos);
				*to->l << fnCall(detachFn->ref(), true);
			} else {
				WARNING(L"Failed to find Future::detach. Will not call it automatically!");
			}
		}

		// Now, we're done!
		*to->l << end(sub->block);
	}

	void Function::asyncCallVoid(CodeGen *to, Array<code::Operand> *params, code::Operand thread,
								CodeResult *id, bool copy) {
		using namespace code;

		Engine &e = engine();
		CodeGen *sub = to->child();
		*to->l << begin(sub->block);

		// Spill any registers to memory if necessary...
		params = spillRegisters(sub, params);

		// Create the parameters.
		Var par = createFnCall(sub, this->params, params, copy);

		// Get the thunk.
		Ref thunk = Ref(threadThunk());

		TypeDesc *ptr = e.ptrDesc();

		// Spawn the thread!
		*to->l << lea(ptrA, par);
		*to->l << fnParam(ptr, ref()); // fn
		*to->l << fnParam(byteDesc(e), toOp(isMember())); // member
		*to->l << fnParam(ptr, thunk); // thunk
		*to->l << fnParam(ptr, ptrA); // params
		*to->l << fnParam(ptr, thread); // on

		// Take care of "id" if we need it.
		if (id) {
			*to->l << fnCall(e.ref(builtin::spawnVoidId), false, longDesc(e), rax);
			if (id->needed())
				*to->l << mov(id->location(to), rax);
		} else {
			*to->l << fnCall(e.ref(builtin::spawnVoid), false);
		}

		// Now, we're done!
		*to->l << end(sub->block);
	}


	code::RefSource *Function::threadThunk() {
		using namespace code;

		if (threadThunkRef)
			return threadThunkRef;

		Binary *threadThunkCode = storm::callThunk(result, params);
		threadThunkRef = new (this) NamedSource(this, Char('t'));
		threadThunkRef->set(threadThunkCode);
		return threadThunkRef;
	}

	code::Ref Function::thunkRef() {
		// This is the same thing as the thread thunk nowadays.
		return code::Ref(threadThunk());
	}


	MAYBE(Function *) Function::withDerivedThis(Type *derived) {
		// Sanity checking:
		if (params->empty())
			return null;
		Value first = params->at(0);
		if (!derived->isA(first.type))
			return null;
		if (first.isValue() && !first.ref)
			return null;

		Array<Value> *paramCopy = new (this) Array<Value>(*params);
		paramCopy->at(0).type = derived;

		// Create a near-identical copy:
		Function *wrap = new (this) Function(pos, result, name, paramCopy);
		wrap->myFlags = myFlags;
		wrap->visibility = visibility;
		wrap->documentation = documentation;

		// Set refs:
		wrap->setCode(new (this) DelegatedCode(directRef()));
		wrap->setLookup(new (this) DelegatedCode(ref()));

		// Copy flags. Also set 'allowReplace' so that it can be replaced transparently.
		wrap->flags = flags | namedAllowReplace;

		return wrap;
	}

	void Function::automaticReplace(Named *old) {
		Function *f = as<Function>(old);
		if (!f)
			return;

		// Adopt the refs from the old function! We don't bother with active replacement here, since
		// the user just added the new function to the name set.
		if (f->code)
			f->code->detach();
		if (f->lookup)
			f->lookup->detach();

		if (f->codeRef || f->lookupRef)
			initRefs();

		if (f->codeRef)
			codeRef->steal(f->codeRef);
		if (f->lookupRef)
			lookupRef->steal(f->lookupRef);

		f->codeRef = null;
		f->lookupRef = null;
	}


	/**
	 * Low-level functions called by the generated code.
	 */

	void spawnThreadResult(const void *fn, bool member, os::CallThunk thunk, void **params, void *result, Thread *on) {
		os::FnCallRaw call(params, thunk);
		os::FutureBase future;
		const os::Thread *thread = on ? &on->thread() : null;
		os::UThread::spawnRaw(fn, member, null, call, future, result, thread);
		future.result(&updateFutureExceptions, null);
	}

	void spawnThreadFuture(const void *fn, bool member, os::CallThunk thunk, void **params, FutureBase *result, Thread *on) {
		os::FnCallRaw call(params, thunk);
		const os::Thread *thread = on ? &on->thread() : null;
		os::UThread::spawnRaw(fn, member, null, call, *result->rawFuture(), result->rawResult(), thread);
	}

	// As 'spawnThreadFuture', but returns a thread ID (same as currentUThread() returns in Storm) and detaches the future.
	Word spawnThreadId(const void *fn, bool member, os::CallThunk thunk, void **params, FutureBase *result, Thread *on) {
		os::FnCallRaw call(params, thunk);
		const os::Thread *thread = on ? &on->thread() : null;
		return os::UThread::spawnRaw(fn, member, null, call, *result->rawFuture(), result->rawResult(), thread).id();
	}

	// Spawn a thread, completely ignoring the result.
	void spawnThreadVoid(const void *fn, bool member, os::CallThunk thunk, void **params, Thread *on) {
		os::FnCallRaw call(params, thunk);
		const os::Thread *thread = on ? &on->thread() : null;
		os::UThread::spawnRaw(fn, member, null, call, thread);
	}

	// Spawn a thread, completely ignoring the result.
	Word spawnThreadVoidId(const void *fn, bool member, os::CallThunk thunk, void **params, Thread *on) {
		os::FnCallRaw call(params, thunk);
		const os::Thread *thread = on ? &on->thread() : null;
		return os::UThread::spawnRaw(fn, member, null, call, thread).id();
	}

	/**
	 * CppFunction.
	 */

	CppMemberFunction::CppMemberFunction(Value result, Str *name, Array<Value> *params, const void *original) :
		Function(result, name, params), original(original) {}

	const void *CppMemberFunction::originalPtr() {
		return original;
	}


	/**
	 * Convenience functions.
	 */

	Function *inlinedFunction(Engine &e, Value result, const wchar *name, Array<Value> *params, Fn<void, InlineParams> *fn) {
		Function *r = new (e) Function(result, new (e) Str(name), params);
		r->setCode(new (e) InlineCode(fn));
		return r;
	}

	Function *nativeFunction(Engine &e, Value result, const wchar *name, Array<Value> *params, const void *fn) {
		Function *r = new (e) Function(result, new (e) Str(name), params);
		r->setCode(new (e) StaticCode(fn));
		return r;
	}

	Function *nativeEngineFunction(Engine &e, Value result, const wchar *name, Array<Value> *params, const void *fn) {
		Function *r = new (e) Function(result, new (e) Str(name), params);
		r->setCode(new (e) StaticEngineCode(fn));
		return r;
	}

	Function *lazyFunction(Engine &e, Value result, const wchar *name, Array<Value> *params, Fn<CodeGen *> *generate) {
		Function *r = new (e) Function(result, new (e) Str(name), params);
		r->setCode(new (e) LazyCode(generate));
		return r;
	}

	Function *dynamicFunction(Engine &e, Value result, const wchar *name, Array<Value> *params, code::Listing *src) {
		Function *r = new (e) Function(result, new (e) Str(name), params);
		r->setCode(new (e) DynamicCode(src));
		return r;
	}

}