File: Maybe.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 (886 lines) | stat: -rw-r--r-- 26,560 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
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
#include "stdafx.h"
#include "Maybe.h"
#include "Compiler/Engine.h"
#include "Compiler/Package.h"
#include "Compiler/Exception.h"
#include "Core/CloneEnv.h"
#include "Core/Str.h"
#include "Core/StrBuf.h"
#include "Serialization.h"
#include "Fn.h"

namespace storm {

	static Bool isValue(Type *t) {
		return Value(t).isValue();
	}

	Type *createMaybe(Str *name, ValueArray *val) {
		if (val->count() != 1)
			return null;

		Value p = val->at(0);
		if (p.ref)
			return null;

		// It is strange to support Maybe<void>. It could work, but it is not possible to initialize
		// it to a sensible "present" value using the same API as for the other cases.
		if (!p.type)
			return null;

		// It is convenient to have layers of Maybe<T> sometimes... Seems to work OK.
		// if (isMaybe(p))
		// 	return null;

		if (p.isValue())
			return new (name) MaybeValueType(name, p.type);
		else
			return new (name) MaybeClassType(name, p.type);
	}


	Bool isMaybe(Value v) {
		return as<MaybeType>(v.type) != null;
	}

	Value unwrapMaybe(Value v) {
		if (MaybeType *t = as<MaybeType>(v.type))
			return t->param();
		else
			return v;
	}

	Value wrapMaybe(Value v) {
		if (v == Value())
			return v;
		Engine &e = v.type->engine;
		TemplateList *l = e.cppTemplate(MaybeId);
		NameSet *to = l->addTo();
		assert(to, L"Too early to use 'wrapMaybe'.");
		Type *found = as<Type>(to->find(S("Maybe"), v, Scope()));
		if (!found)
			throw new (e) InternalError(S("Can not find the maybe type!"));
		return Value(found);
	}

	static GcType *allocType(Engine &e) {
		GcType *t = e.gc.allocType(GcType::tArray, null, sizeof(void *), 1);
		t->offset[0] = 0;
		return t;
	}

	/**
	 * Common base class.
	 */

	MaybeType::MaybeType(Str *name, Type *param, TypeFlags flags, Size size, GcType *gcType)
		: Type(name, new (name) Array<Value>(1, Value(param)), flags, size, gcType, null), contained(param) {}

	MaybeType::MaybeType(Str *name, Type *param, TypeFlags flags, Size size)
		: Type(name, new (name) Array<Value>(1, Value(param)), flags, size), contained(param) {}


	Value MaybeType::param() const {
		return Value(contained);
	}

	/**
	 * For classes or actors.
	 */

	MaybeClassType::MaybeClassType(Str *name, Type *param)
		: MaybeType(name, param, typeValue | typeFinal, Size::sPtr) {}

	static void initMaybeClass(InlineParams p) {
		using namespace code;
		p.allocRegs(0);
		*p.state->l << mov(ptrRel(p.regParam(0), Offset()), ptrConst(Offset()));
	}

	// Expects Maybe<X *> *, X ** or Maybe<X *> *, Maybe<X *> *
	static void copyMaybeClass(InlineParams p) {
		using namespace code;
		p.allocRegs(0, 1);
		Reg dest = p.regParam(0);
		Reg src = p.regParam(1);

		*p.state->l << mov(ptrRel(dest, Offset()), ptrRel(src, Offset()));

		// See if we should return something (necessary since we can be used as an assignment).
		if (p.result->needed()) {
			if (!p.result->suggest(p.state, p.originalParam(0))) {
				*p.state->l << mov(p.result->location(p.state), dest);
			}
		}
	}

	// Expects Maybe<X *> *, X *
	static void copyMaybeClassValue(InlineParams p) {
		using namespace code;
		p.allocRegs(0);
		Reg dest = p.regParam(0);

		*p.state->l << mov(ptrRel(dest, Offset()), p.param(1));

		// See if we should return something (necessary since we can be used as an assignment).
		if (p.result->needed()) {
			if (!p.result->suggest(p.state, p.originalParam(0))) {
				*p.state->l << mov(p.result->location(p.state), dest);
			}
		}
	}

	static void emptyMaybeClass(InlineParams p) {
		using namespace code;
		if (!p.result->needed())
			return;

		*p.state->l << cmp(p.param(0), ptrConst(Offset()));
		*p.state->l << setCond(p.result->location(p.state), ifEqual);
	}

	static void anyMaybeClass(InlineParams p) {
		using namespace code;
		if (!p.result->needed())
			return;

		*p.state->l << cmp(p.param(0), ptrConst(Offset()));
		*p.state->l << setCond(p.result->location(p.state), ifNotEqual);
	}

	static Str *maybeToSClass(EnginePtr e, RootObject *o) {
		if (!o)
			return new (e.v) Str(L"null");
		else
			return o->toS();
	}

	static void maybeToSBufClass(RootObject *o, StrBuf *buf) {
		if (o)
			o->toS(buf);
		else
			*buf << L"null";
	}

	static void maybeCloneClass(Object **o, CloneEnv *env) {
		// Note: Since copying a Maybe<T> does not make a copy of the object, we need to call clone
		// here rather than just deepCopy. This is similar to how a regular deepCopy is implemented.
		if (*o)
			cloned(*o, env);
	}

	code::TypeDesc *MaybeClassType::createTypeDesc() {
		return engine.ptrDesc();
	}

	Bool MaybeClassType::loadAll() {
		Engine &e = engine;
		Value b = Value(StormInfo<Bool>::type(e));

		Array<Value> *v = new (e) Array<Value>(1, Value(this, false));
		Array<Value> *r = new (e) Array<Value>(1, Value(this, true));
		Array<Value> *rr = new (e) Array<Value>(2, Value(this, true));
		Array<Value> *rp = new (e) Array<Value>(2, Value(this, true));
		rp->at(1) = param();

		add(inlinedFunction(e, Value(), CTOR, r, fnPtr(e, &initMaybeClass)));
		// Note: We could take a value as the second parameter, but since that is non-standard,
		// assumptions elsewhere in the system breaks from that.
		add(inlinedFunction(e, Value(), CTOR, rr, fnPtr(e, &copyMaybeClass)));
		add(inlinedFunction(e, Value(this, true), S("="), rr, fnPtr(e, &copyMaybeClass)));
		add(inlinedFunction(e, b, S("empty"), v, fnPtr(e, &emptyMaybeClass)));
		add(inlinedFunction(e, b, S("any"), v, fnPtr(e, &anyMaybeClass)));

		// Cast constructor.
		add(inlinedFunction(e, Value(), CTOR, rp, fnPtr(e, &copyMaybeClassValue))->makeAutoCast());

		add(nativeEngineFunction(e, Value(Str::stormType(e)), S("toS"), v, address(&maybeToSClass)));

		Array<Value> *strBuf = new (e) Array<Value>(2, Value(this, false));
		strBuf->at(1) = Value(StrBuf::stormType(e));
		add(nativeFunction(e, Value(), S("toS"), strBuf, address(&maybeToSBufClass)));

		if (!contained->isA(TObject::stormType(e))) {
			// Note: other places assume that 'this' ptr is a reference, since this is usually the case.
			Array<Value> *clone = new (e) Array<Value>(2, Value(this, true));
			clone->at(1) = Value(CloneEnv::stormType(e));
			add(nativeFunction(e, Value(), S("deepCopy"), clone, address(&maybeCloneClass)));

			// Check for serialization.
			if (SerializeInfo *info = serializeInfo(contained)) {
				addSerialization(info);
			} else {
				watchFor |= watchSerialization;
			}
		}

		if (watchFor)
			contained->watchAdd(this);

		// Create copy ctors for derived versions of Maybe<T> and T.
		add(new (e) TemplateFn(new (e) Str(CTOR), fnPtr(e, &MaybeClassType::createCopy, this)));

		// Create assignment functions for derived versions of Maybe.
		add(new (e) TemplateFn(new (e) Str(S("=")), fnPtr(e, &MaybeClassType::createAssign, this)));

		return MaybeType::loadAll();
	}

	void MaybeClassType::notifyAdded(NameSet *to, Named *added) {
		if (to != contained)
			return;

		if (watchFor & watchSerialization) {
			if (SerializeInfo *info = serializeInfo(contained)) {
				watchFor &= ~watchSerialization;
				addSerialization(info);
			}
		}

		if (!watchFor)
			contained->watchRemove(this);
	}

	Named *MaybeClassType::createAssign(Str *name, SimplePart *part) {
		if (part->params->count() != 2)
			return null;

		if (part->params->at(0).type != this)
			return null;

		Value o = part->params->at(1);
		if (!isMaybe(o))
			return null;

		Value other = unwrapMaybe(o);
		if (!param().mayReferTo(other))
			return null;

		Array<Value> *rr = new (this) Array<Value>(2, o.asRef(true));
		rr->at(0) = Value(this).asRef(true);
		return inlinedFunction(engine, Value(), S("="), rr, fnPtr(engine, &copyMaybeClass));
	}

	Named *MaybeClassType::createCopy(Str *name, SimplePart *part) {
		if (part->params->count() != 2)
			return null;

		if (part->params->at(0).type != this)
			return null;

		Value o = part->params->at(1);
		if (!isMaybe(o))
			return null;

		Value other = unwrapMaybe(o);

		if (!param().mayReferTo(other))
			return null;

		Array<Value> *rr = new (this) Array<Value>(2, o.asRef(true));
		rr->at(0) = Value(this).asRef(true);
		return inlinedFunction(engine, Value(), CTOR, rr, fnPtr(engine, &copyMaybeClass))->makeAutoCast();
	}

	void MaybeClassType::addSerialization(SerializeInfo *info) {
		Function *ctor = readCtor(info);
		add(ctor);

		SerializedMaybe *type = new (this) SerializedMaybe(this, pointer(ctor), contained);
		add(serializedTypeFn(type));
		add(writeFn(type, info));
		add(serializedReadFn(this));
	}

	Function *MaybeClassType::writeFn(SerializedType *type, SerializeInfo *info) {
		using namespace code;

		Value me = Value(this); // We want to get a value passed to us.
		Value objStream(StormInfo<ObjOStream>::type(engine));
		Value boolType(StormInfo<Bool>::type(engine));

		Function *startValueFn = findStormMemberFn(objStream, S("startValue"),
												Value(StormInfo<SerializedType>::type(engine)));
		Function *endFn = findStormMemberFn(objStream, S("end"));
		Function *byteWriteFn = findStormMemberFn(boolType, S("write"), objStream);

		Listing *l = new (this) Listing(true, engine.voidDesc());
		code::Var meVar = l->createParam(me.desc(engine));
		code::Var streamVar = l->createParam(objStream.desc(engine));

		code::Label lblEnd = l->label();
		code::Label lblEmpty = l->label();

		*l << prolog();

		// Call "start".
		*l << fnParam(objStream.desc(engine), streamVar);
		*l << fnParam(engine.ptrDesc(), objPtr(type));
		*l << fnCall(startValueFn->ref(), true, byteDesc(engine), al);

		*l << cmp(al, byteConst(0));
		*l << jmp(lblEnd, ifEqual);

		*l << mov(ptrA, meVar);
		*l << cmp(ptrA, ptrConst(0));
		*l << jmp(lblEmpty, ifEqual);

		// We have something to write! Write it!
		*l << fnParam(boolType.desc(engine), byteConst(1));
		*l << fnParam(objStream.desc(engine), streamVar);
		*l << fnCall(byteWriteFn->ref(), true);

		// We know it is a pointer...
		*l << fnParam(engine.ptrDesc(), meVar);
		*l << fnParam(objStream.desc(engine), streamVar);
		*l << fnCall(info->write->ref(), true);

		*l << jmp(lblEnd);
		*l << lblEmpty;

		*l << fnParam(boolType.desc(engine), byteConst(0));
		*l << fnParam(objStream.desc(engine), streamVar);
		*l << fnCall(byteWriteFn->ref(), true);

		*l << lblEnd;

		// Call 'end'.
		*l << fnParam(objStream.desc(engine), streamVar);
		*l << fnCall(endFn->ref(), true);

		*l << fnRet();


		Array<Value> *params = new (this) Array<Value>();
		params->reserve(2);
		*params << me << objStream;
		Function *fn = new (this) Function(Value(), new (this) Str(S("write")), params);
		fn->setCode(new (this) DynamicCode(l));
		return fn;
	}

	Function *MaybeClassType::readCtor(SerializeInfo *info) {
		using namespace code;

		Value me = thisPtr(this);
		Value objStream(StormInfo<ObjIStream>::type(engine));
		Value boolType(StormInfo<Bool>::type(engine));

		Function *endFn = findStormMemberFn(objStream, S("end"));
		Function *readBoolFn = findStormFn(boolType, S("read"), objStream);

		Listing *l = new (this) Listing(true, engine.voidDesc());
		code::Var meVar = l->createParam(me.desc(engine));
		code::Var streamVar = l->createParam(objStream.desc(engine));

		code::Label lblEnd = l->label();
		code::Label lblEmpty = l->label();

		*l << prolog();

		// Read the bool variable.
		*l << fnParam(objStream.desc(engine), streamVar);
		*l << fnCall(readBoolFn->ref(), false, boolType.desc(engine), al);

		*l << cmp(al, byteConst(0));
		*l << jmp(lblEmpty, ifEqual);

		// Read the value.
		*l << fnParam(objStream.desc(engine), streamVar);
		*l << fnCall(info->read->ref(), false, engine.ptrDesc(), ptrA);
		*l << mov(ptrC, meVar);
		*l << mov(ptrRel(ptrC, Offset()), ptrA);
		*l << jmp(lblEnd);

		// Empty?
		*l << lblEmpty;
		*l << mov(ptrA, meVar);
		*l << mov(ptrRel(ptrA, Offset()), ptrConst(0));

		*l << lblEnd;

		// Call 'end'.
		*l << fnParam(objStream.desc(engine), streamVar);
		*l << fnCall(endFn->ref(), true);

		*l << fnRet();

		Array<Value> *params = new (this) Array<Value>();
		params->reserve(2);
		*params << me << objStream;
		Function *fn = new (this) Function(Value(), new (this) Str(Type::CTOR), params);
		fn->setCode(new (this) DynamicCode(l));
		fn->visibility = typePrivate(engine);
		return fn;
	}


	/**
	 * For values.
	 */

	static Size maybeValSize(Type *type) {
		return (type->size() + Size::sByte).aligned();
	}

	static GcType *allocTypeValue(Type *type) {
		Size size = maybeValSize(type);

		GcType *t = type->engine.gc.allocType(type->gcType());
		t->stride = size.current();
		return t;
	}

	MaybeValueType::MaybeValueType(Str *name, Type *param)
		: MaybeType(name, param, typeValue | typeFinal, maybeValSize(param), allocTypeValue(param)) {}

	code::TypeDesc *MaybeValueType::createTypeDesc() {
		using namespace code;
		TypeDesc *original = contained->typeDesc();

		if (PrimitiveDesc *p = as<PrimitiveDesc>(original)) {
			// Very simple. Struct with a primitive and a boolean.
			SimpleDesc *result = new (this) SimpleDesc(size(), 2);
			result->at(0) = p->v;
			result->at(1) = Primitive(primitive::integer, Size::sByte, Offset(original->size()));
			return result;
		} else if (SimpleDesc *s = as<SimpleDesc>(original)) {
			// Just add another primitive. We don't need a copy-ctor if the underlying type doesn't!
			SimpleDesc *result = new (this) SimpleDesc(size(), s->count() + 1);
			for (Nat i = 0; i < s->count(); i++)
				result->at(i) = s->at(i);

			result->at(s->count()) = Primitive(primitive::integer, Size::sByte, Offset(original->size()));
			return result;
		} else if (as<ComplexDesc>(original)) {
			// Complex description is the easiest actually... We just sort everything out in our copy-ctor.
			Ref ctor = engine.ref(builtin::fnNull), dtor = engine.ref(builtin::fnNull);
			if (Function *f = copyCtor())
				ctor = f->ref();
			if (Function *f = destructor())
				dtor = f->ref();

			return new (this) ComplexDesc(size(), ctor, dtor);
		} else {
			throw new (this) InternalError(TO_S(engine, S("Unknown type description found for ") << contained->identifier()));
		}
	}

	Bool MaybeValueType::loadAll() {
		Engine &e = engine;
		Value t = thisPtr(this);
		Value b = Value(StormInfo<Bool>::type(e));

		Array<Value> *r = new (e) Array<Value>(1, t.asRef(true));
		Array<Value> *rr = new (e) Array<Value>(2, t.asRef(true));
		Array<Value> *rp = new (e) Array<Value>(2, t.asRef(true));
		rp->at(1) = param().asRef(true);

		handle = &contained->handle();
		offset = boolOffset().current();

		add(inlinedFunction(e, Value(), CTOR, r, fnPtr(e, &MaybeValueType::initMaybe, this)));
		add(inlinedFunction(e, Value(), CTOR, rr, fnPtr(e, &MaybeValueType::copyMaybe, this)));
		add(inlinedFunction(e, t.asRef(true), S("="), rr, fnPtr(e, &MaybeValueType::copyMaybe, this)));
		add(inlinedFunction(e, b, S("empty"), r, fnPtr(e, &MaybeValueType::emptyMaybe, this)));
		add(inlinedFunction(e, b, S("any"), r, fnPtr(e, &MaybeValueType::anyMaybe, this)));

		// Cast constructor.
		add(inlinedFunction(e, Value(), CTOR, rp, fnPtr(e, &MaybeValueType::castMaybe, this))->makeAutoCast());

		if (handle->toSFn) {
			add(inlinedFunction(e, Value(Str::stormType(e)), S("toS"), r, fnPtr(e, &MaybeValueType::toSMaybe, this)));

			Array<Value> *strBuf = new (e) Array<Value>(2, t);
			strBuf->at(1) = Value(StrBuf::stormType(e));
			add(inlinedFunction(e, Value(), S("toS"), strBuf, fnPtr(e, &MaybeValueType::toSMaybe, this)));
		}

		if (contained->deepCopyFn()) {
			Array<Value> *clone = new (e) Array<Value>(2, t);
			clone->at(1) = Value(CloneEnv::stormType(e));
			add(inlinedFunction(e, Value(), S("deepCopy"), clone, fnPtr(e, &MaybeValueType::cloneMaybe, this)));
		}

		// Check for serialization.
		if (SerializeInfo *info = serializeInfo(contained)) {
			addSerialization(info);
		} else {
			watchFor |= watchSerialization;
		}

		if (watchFor)
			contained->watchAdd(this);

		// Create copy ctors for derived versions of Maybe<T> and T.
		add(new (e) TemplateFn(new (e) Str(CTOR), fnPtr(e, &MaybeValueType::createCopy, this)));

		// Create assignment functions for derived versions of Maybe.
		add(new (e) TemplateFn(new (e) Str(S("=")), fnPtr(e, &MaybeValueType::createAssign, this)));

		return MaybeType::loadAll();
	}

	void MaybeValueType::notifyAdded(NameSet *to, Named *added) {
		if (to != contained)
			return;

		if (watchFor & watchSerialization) {
			if (SerializeInfo *info = serializeInfo(contained)) {
				watchFor &= ~watchSerialization;
				addSerialization(info);
			}
		}

		if (!watchFor)
			contained->watchRemove(this);
	}

	void MaybeValueType::initMaybe(InlineParams p) {
		using namespace code;
		p.allocRegs(0);
		*p.state->l << mov(byteRel(p.regParam(0), boolOffset()), byteConst(0));
	}

	void MaybeValueType::copyMaybe(InlineParams p) {
		using namespace code;
		p.allocRegs(0, 1);

		Reg dest = p.regParam(0);
		Reg src = p.regParam(1);

		Reg tmp = asSize(freeReg(dest, src), Size::sByte);


		// Store the result before we trash it.
		if (p.result->needed()) {
			*p.state->l << mov(p.result->location(p.state), dest);
		}

		Label empty = p.state->l->label();

		*p.state->l << mov(tmp, byteRel(src, boolOffset()));
		*p.state->l << mov(byteRel(dest, boolOffset()), tmp);
		*p.state->l << cmp(tmp, byteConst(0));
		*p.state->l << jmp(empty, ifEqual);

		// Copy.
		if (Value(contained).isAsmType()) {
			// Just move the value.
			Size sz = contained->size();
			*p.state->l << mov(xRel(sz, dest, Offset()), xRel(sz, src, Offset()));
		} else {
			Function *copyCtor = contained->copyCtor();
			if (!copyCtor) {
				Str *msg = TO_S(engine, S("The type ") << contained->identifier()
								<< S(" does not provide a copy constructor!"));
				throw new (this) TypedefError(contained->pos, msg);
			}

			// Call the regular constructor! (TODO? Inline it?)
			*p.state->l << fnParam(engine.ptrDesc(), dest);
			*p.state->l << fnParam(engine.ptrDesc(), src);
			*p.state->l << fnCall(copyCtor->ref(), false);
		}

		*p.state->l << empty;
	}

	void MaybeValueType::castMaybe(InlineParams p) {
		using namespace code;
		p.allocRegs(0, 1);

		Reg dest = p.regParam(0);
		Reg src = p.regParam(1);

		// Store the result before we trash it.
		if (p.result->needed()) {
			*p.state->l << mov(p.result->location(p.state), dest);
		}

		*p.state->l << mov(byteRel(dest, boolOffset()), byteConst(1));

		// Copy.
		if (Value(contained).isAsmType()) {
			// Just move the value.
			Size sz = contained->size();
			*p.state->l << mov(xRel(sz, dest, Offset()), xRel(sz, src, Offset()));
		} else {
			Function *copyCtor = contained->copyCtor();
			if (!copyCtor) {
				// TODO: We could fall back to a memcpy implementation... We can't inline it,
				// however, as we don't know the exact size of the type (could be either 32- or 64-bit).
				Str *msg = TO_S(engine, S("The type ") << contained->identifier() << S(" does not provide a copy constructor!"));
				throw new (this) TypedefError(contained->pos, msg);
			}

			// Call the regular constructor! (TODO? Inline it?)
			*p.state->l << fnParam(engine.ptrDesc(), dest);
			*p.state->l << fnParam(engine.ptrDesc(), src);
			*p.state->l << fnCall(copyCtor->ref(), false);
		}
	}

	void MaybeValueType::emptyMaybe(InlineParams p) {
		using namespace code;
		if (!p.result->needed())
			return;

		p.allocRegs(0);
		*p.state->l << cmp(byteRel(p.regParam(0), boolOffset()), byteConst(0));
		*p.state->l << setCond(p.result->location(p.state), ifEqual);
	}

	void MaybeValueType::anyMaybe(InlineParams p) {
		using namespace code;
		if (!p.result->needed())
			return;

		p.allocRegs(0);
		*p.state->l << mov(p.result->location(p.state), byteRel(p.regParam(0), boolOffset()));
	}

	void MaybeValueType::toSMaybe(InlineParams p) {
		using namespace code;
		if (!p.result->needed())
			return;

		*p.state->l << fnParam(engine.ptrDesc(), typeRef());
		*p.state->l << fnParam(engine.ptrDesc(), p.param(0));
		*p.state->l << fnParam(engine.ptrDesc(), ptrConst(0));
		*p.state->l << fnCall(engine.ref(builtin::maybeToS), false, engine.ptrDesc(), p.result->location(p.state));
	}

	void MaybeValueType::toSMaybeBuf(InlineParams p) {
		using namespace code;
		if (!p.result->needed())
			return;

		*p.state->l << fnParam(engine.ptrDesc(), typeRef());
		*p.state->l << fnParam(engine.ptrDesc(), p.param(0));
		*p.state->l << fnParam(engine.ptrDesc(), p.param(1));
		*p.state->l << fnCall(engine.ref(builtin::maybeToS), false);
	}

	void *MaybeValueType::toSHelper(MaybeValueType *me, void *value, StrBuf *out) {
		StrBuf *to = out;
		if (!to)
			to = new (me) StrBuf();

		// Does this object represent 'null'?
		byte *data = (byte *)value;
		if (data[me->offset]) {
			(*me->handle->toSFn)(value, to);
		} else {
			*to << S("null");
		}

		// Were we called as 'toS()' or 'toS(StrBuf)'?
		if (out)
			return null;
		else
			return to->toS();
	}

	void MaybeValueType::cloneMaybe(InlineParams p) {
		using namespace code;

		Function *call = contained->deepCopyFn();
		if (!call) {
			Str *msg = TO_S(engine, S("The deep copy function was removed from ") << contained->identifier());
			throw new (this) InternalError(msg);
		}

		Label end = p.state->l->label();

		p.allocRegs(0);
		Reg dest = p.regParam(0);

		*p.state->l << cmp(byteRel(dest, boolOffset()), byteConst(0));
		*p.state->l << jmp(end, ifEqual);

		*p.state->l << fnParam(engine.ptrDesc(), dest);
		*p.state->l << fnParam(engine.ptrDesc(), p.param(1));
		*p.state->l << fnCall(contained->deepCopyFn()->ref(), true);

		*p.state->l << end;
	}

	Named *MaybeValueType::createAssign(Str *name, SimplePart *part) {
		if (part->params->count() != 2)
			return null;

		if (part->params->at(0).type != this)
			return null;

		Value o = part->params->at(1);
		if (!isMaybe(o))
			return null;

		Value other = unwrapMaybe(o);
		if (!param().mayReferTo(other))
			return null;

		Array<Value> *rr = new (this) Array<Value>(2, o.asRef(true));
		rr->at(0) = Value(this).asRef(true);
		return inlinedFunction(engine, Value(), S("="), rr, fnPtr(engine, &MaybeValueType::copyMaybe, this));
	}

	Named *MaybeValueType::createCopy(Str *name, SimplePart *part) {
		if (part->params->count() != 2)
			return null;

		if (part->params->at(0).type != this)
			return null;

		Value o = part->params->at(1);
		if (!isMaybe(o))
			return null;

		Value other = unwrapMaybe(o);

		if (!param().mayReferTo(other))
			return null;

		Array<Value> *rr = new (this) Array<Value>(2, o.asRef(true));
		rr->at(0) = Value(this).asRef(true);
		return inlinedFunction(engine, Value(), S("="), rr, fnPtr(engine, &MaybeValueType::copyMaybe, this))->makeAutoCast();
	}

	void MaybeValueType::addSerialization(SerializeInfo *info) {
		Function *ctor = readCtor(info);
		add(ctor);

		SerializedMaybe *type = new (this) SerializedMaybe(this, pointer(ctor), contained);
		add(serializedTypeFn(type));
		add(writeFn(type, info));
		add(serializedReadFn(this));
	}

	Function *MaybeValueType::writeFn(SerializedType *type, SerializeInfo *info) {
		using namespace code;

		Value me = Value(this, true);
		Value objStream(StormInfo<ObjOStream>::type(engine));
		Value boolType(StormInfo<Bool>::type(engine));

		Function *startValueFn = findStormMemberFn(objStream, S("startValue"),
												Value(StormInfo<SerializedType>::type(engine)));
		Function *endFn = findStormMemberFn(objStream, S("end"));
		Function *byteWriteFn = findStormMemberFn(boolType, S("write"), objStream);

		Listing *l = new (this) Listing(true, engine.voidDesc());
		code::Var meVar = l->createParam(me.desc(engine));
		code::Var streamVar = l->createParam(objStream.desc(engine));

		code::Label lblEnd = l->label();
		code::Label lblEmpty = l->label();

		*l << prolog();

		// Call "start".
		*l << fnParam(objStream.desc(engine), streamVar);
		*l << fnParam(engine.ptrDesc(), objPtr(type));
		*l << fnCall(startValueFn->ref(), true, byteDesc(engine), al);

		*l << cmp(al, byteConst(0));
		*l << jmp(lblEnd, ifEqual);

		*l << mov(ptrA, meVar);
		*l << cmp(byteRel(ptrA, boolOffset()), byteConst(0));
		*l << jmp(lblEmpty, ifEqual);

		// We have something to write! Write it!
		*l << fnParam(boolType.desc(engine), byteConst(1));
		*l << fnParam(objStream.desc(engine), streamVar);
		*l << fnCall(byteWriteFn->ref(), true);

		// Call 'write'.
		if (info->write->params->at(0).ref)
			*l << fnParam(engine.ptrDesc(), meVar);
		else
			*l << fnParamRef(info->write->params->at(0).desc(engine), meVar);
		*l << fnParam(objStream.desc(engine), streamVar);
		*l << fnCall(info->write->ref(), true);

		*l << jmp(lblEnd);
		*l << lblEmpty;

		*l << fnParam(boolType.desc(engine), byteConst(0));
		*l << fnParam(objStream.desc(engine), streamVar);
		*l << fnCall(byteWriteFn->ref(), true);

		*l << lblEnd;

		// Call 'end'.
		*l << fnParam(objStream.desc(engine), streamVar);
		*l << fnCall(endFn->ref(), true);

		*l << fnRet();


		Array<Value> *params = new (this) Array<Value>();
		params->reserve(2);
		*params << me << objStream;
		Function *fn = new (this) Function(Value(), new (this) Str(S("write")), params);
		fn->setCode(new (this) DynamicCode(l));
		return fn;
	}

	Function *MaybeValueType::readCtor(SerializeInfo *info) {
		using namespace code;

		Value me = thisPtr(this);
		Value objStream(StormInfo<ObjIStream>::type(engine));
		Value boolType(StormInfo<Bool>::type(engine));

		Function *endFn = findStormMemberFn(objStream, S("end"));
		Function *readBoolFn = findStormFn(boolType, S("read"), objStream);

		Listing *l = new (this) Listing(true, engine.voidDesc());
		code::Var meVar = l->createParam(me.desc(engine));
		code::Var streamVar = l->createParam(objStream.desc(engine));

		code::Label lblEnd = l->label();
		code::Label lblEmpty = l->label();

		*l << prolog();

		// Read the bool variable.
		*l << fnParam(objStream.desc(engine), streamVar);
		*l << fnCall(readBoolFn->ref(), false, boolType.desc(engine), al);

		*l << cmp(al, byteConst(0));
		*l << jmp(lblEmpty, ifEqual);

		// Read the value and initialize ourselves with it.
		*l << fnParam(objStream.desc(engine), streamVar);
		*l << fnCallRef(info->read->ref(), false, Value(contained).desc(engine), meVar);
		*l << mov(ptrA, meVar);
		*l << mov(byteRel(ptrA, boolOffset()), byteConst(1));

		*l << jmp(lblEnd);

		// Empty?
		*l << lblEmpty;
		*l << mov(ptrA, meVar);
		*l << mov(byteRel(ptrA, boolOffset()), byteConst(0));

		*l << lblEnd;

		// Call 'end'.
		*l << fnParam(objStream.desc(engine), streamVar);
		*l << fnCall(endFn->ref(), true);

		*l << fnRet();

		Array<Value> *params = new (this) Array<Value>();
		params->reserve(2);
		*params << me << objStream;
		Function *fn = new (this) Function(Value(), new (this) Str(Type::CTOR), params);
		fn->setCode(new (this) DynamicCode(l));
		fn->visibility = typePrivate(engine);
		return fn;
	}

}