File: Array.cpp

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; 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 (636 lines) | stat: -rw-r--r-- 21,334 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
#include "stdafx.h"
#include "Array.h"
#include "Engine.h"
#include "Exception.h"
#include "Fn.h"
#include "Core/Str.h"
#include "Serialization.h"

namespace storm {

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

		Value param = params->at(0);
		if (param.ref)
			return null;

		return new (params) ArrayType(name, param.type);
	}

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

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

	Value wrapArray(Value v) {
		if (!v.type)
			return v;
		if (v.ref)
			return v;

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

	static void CODECALL createArrayRaw(void *mem) {
		ArrayType *t = (ArrayType *)runtime::typeOf((RootObject *)mem);
		ArrayBase *o = new (Place(mem)) ArrayBase(t->param().type->handle());
		runtime::setVTable(o);
	}

	static void CODECALL copyArray(void *mem, ArrayBase *from) {
		ArrayBase *o = new (Place(mem)) ArrayBase(*from);
		runtime::setVTable(o);
	}

	static void CODECALL createArrayClassCount(void *mem, Nat count, const RootObject *data) {
		ArrayType *t = (ArrayType *)runtime::typeOf((RootObject *)mem);
		ArrayBase *o = new (Place(mem)) ArrayBase(t->param().type->handle(), count, &data);
		runtime::setVTable(o);
	}

	static void CODECALL createArrayValCount(void *mem, Nat count, const void *data) {
		ArrayType *t = (ArrayType *)runtime::typeOf((RootObject *)mem);
		ArrayBase *o = new (Place(mem)) ArrayBase(t->param().type->handle(), count, data);
		runtime::setVTable(o);
	}

	static ArrayBase *CODECALL pushClass(ArrayBase *to, const void *src) {
		to->pushRaw(&src);
		return to;
	}

	static void CODECALL insertClass(ArrayBase *to, Nat pos, const void *src) {
		to->insertRaw(pos, &src);
	}

	static ArrayBase *CODECALL pushValue(ArrayBase *to, const void *src) {
		to->pushRaw(src);
		return to;
	}

	static ArrayBase *CODECALL sortedRaw(ArrayBase *src) {
		Type *t = runtime::typeOf(src);
		ArrayBase *copy = (ArrayBase *)runtime::allocObject(sizeof(ArrayBase), t);
		copyArray(copy, src);
		copy->sortRaw();
		return copy;
	}

	static ArrayBase *CODECALL sortedRawPred(ArrayBase *src, FnBase *compare) {
		Type *t = runtime::typeOf(src);
		ArrayBase *copy = (ArrayBase *)runtime::allocObject(sizeof(ArrayBase), t);
		copyArray(copy, src);
		copy->sortRawPred(compare);
		return copy;
	}

	ArrayType::ArrayType(Str *name, Type *contents) : Type(name, typeClass), contents(contents), watchFor(0) {
		if (engine.has(bootTemplates))
			lateInit();

		setSuper(ArrayBase::stormType(engine));
		// Avoid problems while loading Array<Value>, as Array<Value> is required to compute the
		// GcType of Array<Value>...
		useSuperGcType();
	}

	void ArrayType::lateInit() {
		if (!params)
			params = new (engine) Array<Value>();
		if (params->count() < 1)
			params->push(Value(contents));

		Type::lateInit();
	}

	Value ArrayType::param() const {
		return Value(contents);
	}

	Bool ArrayType::loadAll() {
		Type *iter = new (this) ArrayIterType(contents);
		add(iter);

		if (param().isObject())
			loadClassFns();
		else
			loadValueFns();

		// Shared functions.
		Engine &e = engine;
		Value t = thisPtr(this);
		Value ref = param().asRef();
		Value natType = Value(StormInfo<Nat>::type(e));

		add(nativeFunction(e, Value(), Type::CTOR, valList(e, 2, t, t), address(&copyArray))->makePure());
		add(nativeFunction(e, ref, S("[]"), valList(e, 2, t, natType), address(&ArrayBase::getRaw))->makePure());
		add(nativeFunction(e, ref, S("last"), valList(e, 1, t), address(&ArrayBase::lastRaw))->makePure());
		add(nativeFunction(e, ref, S("first"), valList(e, 1, t), address(&ArrayBase::firstRaw))->makePure());
		add(nativeFunction(e, ref, S("random"), valList(e, 1, t), address(&ArrayBase::randomRaw)));
		add(nativeFunction(e, t, S("append"), valList(e, 2, t, t), address(&ArrayBase::appendRaw)));
		add(nativeFunction(e, Value(iter), S("begin"), valList(e, 1, t), address(&ArrayBase::beginRaw))->makePure());
		add(nativeFunction(e, Value(iter), S("end"), valList(e, 1, t), address(&ArrayBase::endRaw))->makePure());

		// Sort with operator <.
		if (param().type->handle().lessFn) {
			addSort();
			addBinarySearch();
		} else {
			watchFor |= watchLess;
		}

		// Remove duplicates.
		if (param().type->handle().lessFn || param().type->handle().equalFn) {
			addRemoveDuplicates();
		} else {
			watchFor |= watchEquality;
		}

		// Sort and remove duplicates with provided predicate function.
		{
			Array<Value> *pParams = new (e) Array<Value>();
			*pParams << Value(StormInfo<Bool>::type(e));
			*pParams << param() << param();
			Value predicate = Value(fnType(pParams));

			add(nativeFunction(e, Value(), S("sort"), valList(e, 2, t, predicate), address(&ArrayBase::sortRawPred)));
			add(nativeFunction(e, t, S("sorted"), valList(e, 2, t, predicate), address(&sortedRawPred))->makePure());

			add(nativeFunction(e, Value(), S("removeDuplicates"), valList(e, 2, t, predicate), address(&ArrayBase::removeDuplicatesRawPred)));
			add(nativeFunction(e, t, S("withoutDuplicates"), valList(e, 2, t, predicate), address(&ArrayBase::withoutDuplicatesRawPred))->makePure());

			add(new (e) TemplateFn(new (e) Str(S("lowerBound")), fnPtr(e, &ArrayType::createLowerBound, this)));
			add(new (e) TemplateFn(new (e) Str(S("upperBound")), fnPtr(e, &ArrayType::createUpperBound, this)));
		}

		if (!param().type->isA(StormInfo<TObject>::type(engine))) {
			if (SerializeInfo *info = serializeInfo(param().type)) {
				addSerialization(info);
			} else {
				watchFor |= watchSerialization;
			}
		}

		if (watchFor)
			param().type->watchAdd(this);

		return Type::loadAll();
	}

	void ArrayType::loadClassFns() {
		Engine &e = engine;
		Value t = thisPtr(this);
		Value natType = Value(StormInfo<Nat>::type(e));

		add(nativeFunction(e, Value(), Type::CTOR, valList(e, 1, t), address(&createArrayRaw))->makePure());
		add(nativeFunction(e, Value(), Type::CTOR, valList(e, 3, t, natType, param()), address(&createArrayClassCount))->makePure());
		add(nativeFunction(e, t, S("<<"), valList(e, 2, t, param()), address(&pushClass)));
		add(nativeFunction(e, t, S("push"), valList(e, 2, t, param()), address(&pushClass)));
		add(nativeFunction(e, Value(), S("insert"), valList(e, 3, t, natType, param()), address(&insertClass)));
	}

	void ArrayType::loadValueFns() {
		Engine &e = engine;
		Value t = thisPtr(this);
		Value ref = param().asRef(true);
		Value natType = Value(StormInfo<Nat>::type(e));

		add(nativeFunction(e, Value(), Type::CTOR, valList(e, 1, t), address(&createArrayRaw))->makePure());
		add(nativeFunction(e, Value(), Type::CTOR, valList(e, 3, t, natType, ref), address(&createArrayValCount))->makePure());
		add(nativeFunction(e, t, S("<<"), valList(e, 2, t, ref), address(&pushValue)));
		add(nativeFunction(e, t, S("push"), valList(e, 2, t, ref), address(&pushValue)));
		add(nativeFunction(e, Value(), S("insert"), valList(e, 3, t, natType, ref), address(&ArrayBase::insertRaw)));
	}

	void ArrayType::notifyAdded(NameSet *to, Named *added) {
		if (to != param().type)
			return;

		Function *fn = as<Function>(added);
		if (!fn)
			return;

		if (*fn->name == S("<") &&
			fn->result == Value(StormInfo<Bool>::type(engine)) &&
			fn->params->count() == 2 &&
			fn->params->at(0).type == to &&
			fn->params->at(1).type == to) {

			watchFor &= ~watchLess;
			addSort();
			addBinarySearch();
			if (watchFor & watchEquality) {
				watchFor &= ~watchEquality;
				addRemoveDuplicates();
			}
		} else if (*fn->name == S("==") &&
				fn->result == Value(StormInfo<Bool>::type(engine)) &&
				fn->params->count() == 2 &&
				fn->params->at(0).type == to &&
				fn->params->at(1).type == to) {

			watchFor &= ~watchEquality;
			addRemoveDuplicates();
		} else if (SerializeInfo *info = serializeInfo(param().type)) {
			if (watchFor & watchSerialization) {
				watchFor &= ~watchSerialization;
				addSerialization(info);
			}
		}


		if (watchFor == watchNone)
			to->watchRemove(this);
	}

	void ArrayType::addSort() {
		Engine &e = engine;
		Array<Value> *params = valList(e, 1, thisPtr(this));

		// Sort using <.
		add(nativeFunction(e, Value(), S("sort"), params, address(&ArrayBase::sortRaw)));
		add(nativeFunction(e, Value(this), S("sorted"), params, address(&sortedRaw))->makePure());

		// < for comparing.
		Value b(StormInfo<Bool>::type(e));
		add(nativeFunction(e, b, S("<"), new (e) Array<Value>(2, thisPtr(this)), address(&ArrayBase::lessRaw)));
	}

	void ArrayType::addRemoveDuplicates() {
		Engine &e = engine;
		Array<Value> *params = valList(e, 1, thisPtr(this));

		// Compare with == or <, whichever is present.
		add(nativeFunction(e, Value(), S("removeDuplicates"), params, address(&ArrayBase::removeDuplicatesRaw)));
		add(nativeFunction(e, Value(this), S("withoutDuplicates"), params, address(&ArrayBase::withoutDuplicatesRaw))->makePure());

		// == for comparing.
		Value b(StormInfo<Bool>::type(e));
		add(nativeFunction(e, b, S("=="), new (e) Array<Value>(2, thisPtr(this)), address(&ArrayBase::equalRaw)));
	}

	void ArrayType::addBinarySearch() {
		Engine &e = engine;
		Value n(StormInfo<Nat>::type(e));
		Array<Value> *params = valList(e, 2, thisPtr(this), param().asRef());

		add(nativeFunction(e, n, S("lowerBound"), params, address(&ArrayBase::lowerBoundRaw)));
		add(nativeFunction(e, n, S("upperBound"), params, address(&ArrayBase::upperBoundRaw)));
	}

	MAYBE(Named *) ArrayType::createLowerBound(Str *name, SimplePart *part) {
		if (part->params->count() != 3)
			return null;

		Value findType = part->params->at(1).asRef(false);
		if (!findType.type)
			return null;

		// Note: To help automatic deduction of template parameters, we ignore parameter #2 and
		// produce a suitable match based on parameter 1 (parameter 2 is often deduced to 'void' for
		// anonymous lambdas). To make this work, we need to re-use existing entities whenever
		// possible.
		NameOverloads *candidates = allOverloads(new (this) Str(S("lowerBound")));
		for (Nat i = 0; i < candidates->count(); i++) {
			Named *candidate = candidates->at(i);
			if (candidate->params->count() == 3)
				if (candidate->params->at(1).type == findType.type)
					return candidate;
		}

		// Otherwise, create a new entity:
		Value n(StormInfo<Nat>::type(engine));
		Array<Value> *predParams = new (engine) Array<Value>(3, Value(StormInfo<Bool>::type(engine)));
		predParams->at(1) = param();
		predParams->at(2) = findType;

		Array<Value> *params = new (engine) Array<Value>(3, thisPtr(this));
		params->at(1) = findType.asRef();
		params->at(2) = Value(fnType(predParams));

		Function *created = nativeFunction(engine, n, S("lowerBound"), params, address(&ArrayBase::lowerBoundRawPred));
		created->flags |= namedMatchNoInheritance;
		return created;
	}

	MAYBE(Named *) ArrayType::createUpperBound(Str *name, SimplePart *part) {
		if (part->params->count() != 3)
			return null;

		Value findType = part->params->at(1).asRef(false);
		if (!findType.type)
			return null;

		// Note: To help automatic deduction of template parameters, we ignore parameter #2 and
		// produce a suitable match based on parameter 1 (parameter 2 is often deduced to 'void' for
		// anonymous lambdas). To make this work, we need to re-use existing entities whenever
		// possible.
		NameOverloads *candidates = allOverloads(new (this) Str(S("upperBound")));
		for (Nat i = 0; i < candidates->count(); i++) {
			Named *candidate = candidates->at(i);
			if (candidate->params->count() == 3)
				if (candidate->params->at(1).type == findType.type)
					return candidate;
		}

		// Otherwise, create a new entity:
		Value n(StormInfo<Nat>::type(engine));
		Array<Value> *predParams = new (engine) Array<Value>(3, Value(StormInfo<Bool>::type(engine)));
		predParams->at(1) = findType;
		predParams->at(2) = param();

		Array<Value> *params = new (engine) Array<Value>(3, thisPtr(this));
		params->at(1) = findType.asRef();
		params->at(2) = Value(fnType(predParams));

		Function *created = nativeFunction(engine, n, S("upperBound"), params, address(&ArrayBase::upperBoundRawPred));
		created->flags |= namedMatchNoInheritance;
		return created;
	}

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

		SerializedTuples *type = new (this) SerializedTuples(this, pointer(ctor));
		type->add(param().type);
		add(serializedTypeFn(type));

		// Add the 'write' function.
		add(writeFn(type, info));

		// Add the 'read' function.
		add(serializedReadFn(this));
	}

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

		Value me = thisPtr(this);
		Value objStream(StormInfo<ObjOStream>::type(engine));

		Function *startFn = findStormMemberFn(objStream, S("startClass"),
											Value(StormInfo<SerializedType>::type(engine)),
											Value(StormInfo<Object>::type(engine)));
		Function *endFn = findStormMemberFn(objStream, S("end"));
		Function *natWriteFn = findStormMemberFn(Value(StormInfo<Nat>::type(engine)), S("write"), objStream);
		Function *countFn = findStormMemberFn(me, S("count"));
		Function *atFn = findStormMemberFn(me, S("[]"), Value(StormInfo<Nat>::type(engine)));

		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::Var count = l->createVar(l->root(), Size::sInt);
		code::Var curr = l->createVar(l->root(), Size::sInt);

		TypeDesc *natDesc = code::intDesc(engine); // int === nat in this context.
		TypeDesc *ptrDesc = code::ptrDesc(engine);

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

		*l << prolog();

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

		// See if we need to serialize ourselves.
		*l << cmp(al, byteConst(0));
		*l << jmp(lblEnd, ifEqual);

		// Find number of elements.
		*l << fnParam(me.desc(engine), meVar);
		*l << fnCall(countFn->ref(), true, natDesc, count);

		// Write number of elements.
		*l << fnParam(natDesc, count);
		*l << fnParam(objStream.desc(engine), streamVar);
		*l << fnCall(natWriteFn->ref(), true);

		// Serialize each element.
		*l << lblLoop;
		*l << cmp(curr, count);
		*l << jmp(lblLoopEnd, ifAboveEqual);

		// Get the element.
		*l << fnParam(me.desc(engine), meVar);
		*l << fnParam(natDesc, curr);
		*l << fnCall(atFn->ref(), true, ptrDesc, ptrA);

		// If it is a pointer, we need to read the actual pointer.
		if (param().isObject())
			*l << mov(ptrA, ptrRel(ptrA, Offset()));

		// Call 'write'. If the object is a value, and does not take a reference parameter as the
		// 'this' parameter (some primitive types do that), we need to dereference it.
		if (!param().isObject() && !info->write->params->at(0).ref)
			*l << fnParamRef(param().desc(engine), ptrA);
		else
			*l << fnParam(ptrDesc, ptrA);
		*l << fnParam(objStream.desc(engine), streamVar);
		*l << fnCall(info->write->ref(), true);

		*l << code::add(curr, natConst(1));
		*l << jmp(lblLoop);

		*l << lblLoopEnd;

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

		*l << lblEnd;
		*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 *ArrayType::readCtor(SerializeInfo *info) {
		using namespace code;

		Value me = thisPtr(this);
		Value param = this->param();
		Value objStream(StormInfo<ObjIStream>::type(engine));
		Value natType(StormInfo<Nat>::type(engine));

		Function *initFn = findStormMemberFn(me, Type::CTOR);
		Function *endFn = findStormMemberFn(objStream, S("end"));
		Function *checkArrayFn = findStormMemberFn(objStream, S("checkArrayAlloc"), natType, natType);
		Function *natReadFn = findStormFn(Value(natType), S("read"), objStream);
		Function *reserveFn = findStormMemberFn(me, S("reserve"), Value(StormInfo<Nat>::type(engine)));
		Function *pushFn = findStormMemberFn(me, S("<<"), param.asRef(true));

		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::Var count = l->createVar(l->root(), Size::sInt);
		code::Var curr = l->createVar(l->root(), Size::sInt);

		TypeDesc *natDesc = code::intDesc(engine); // int === nat in this context.
		TypeDesc *ptrDesc = code::ptrDesc(engine);

		code::Label lblLoop = l->label();
		code::Label lblLoopEnd = l->label();

		*l << prolog();

		// Call the default constructor.
		*l << fnParam(me.desc(engine), meVar);
		*l << fnCall(initFn->ref(), true);

		// Find number of elements.
		*l << fnParam(objStream.desc(engine), streamVar);
		*l << fnCall(natReadFn->ref(), false, natDesc, count);

		// Check size of array.
		*l << fnParam(objStream.desc(engine), streamVar);
		*l << fnParam(natDesc, natConst(param.size()));
		*l << fnParam(natDesc, count);
		*l << fnCall(checkArrayFn->ref(), true);

		// Reserve elements.
		*l << fnParam(me.desc(engine), meVar);
		*l << fnParam(natDesc, count);
		*l << fnCall(reserveFn->ref(), true);

		// Read each element.
		*l << lblLoop;
		*l << cmp(curr, count);
		*l << jmp(lblLoopEnd, ifAboveEqual);

		if (param.isObject()) {
			// Get the element.
			*l << fnParam(objStream.desc(engine), streamVar);
			*l << fnCall(info->read->ref(), false, param.desc(engine), ptrA);

			// Store it.
			*l << fnParam(me.desc(engine), meVar);
			*l << fnParam(ptrDesc, ptrA);
			*l << fnCall(pushFn->ref(), true, ptrDesc, ptrA); // It returns a ptr to itself, but we don't need that.
		} else {
			code::Block sub = l->createBlock(l->root());
			code::Var tmp = l->createVar(sub, param.size(), param.destructor(), code::freeDef | code::freeInactive);

			*l << code::begin(sub);

			// Get the element.
			*l << fnParam(objStream.desc(engine), streamVar);
			*l << fnCall(info->read->ref(), false, param.desc(engine), tmp);

			// Make sure it is properly destroyed if we get an exception.
			*l << code::activate(tmp);

			// Write it to the array by calling 'push'.
			*l << lea(ptrA, tmp);
			*l << fnParam(me.desc(engine), meVar);
			*l << fnParam(ptrDesc, ptrA);
			*l << fnCall(pushFn->ref(), true, ptrDesc, ptrA); // It returns a ptr to itself, but we don't need that.

			// Now, we're done with 'tmp'.
			*l << code::end(sub);
		}

		// Repeat!
		*l << code::add(curr, natConst(1));
		*l << jmp(lblLoop);

		// Call "end".
		*l << lblLoopEnd;
		*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;
	}

	/**
	 * Iterator.
	 */

	static void CODECALL copyIterator(void *to, const ArrayBase::Iter *from) {
		new (Place(to)) ArrayBase::Iter(*from);
	}

	static void *CODECALL assignIterator(ArrayBase::Iter *to, const ArrayBase::Iter *from) {
		*to = *from;
		return to;
	}

	static bool CODECALL iteratorEq(ArrayBase::Iter &a, ArrayBase::Iter &b) {
		return a == b;
	}

	static bool CODECALL iteratorNeq(ArrayBase::Iter &a, ArrayBase::Iter &b) {
		return a != b;
	}

	static void *CODECALL iteratorGet(const ArrayBase::Iter &v) {
		return v.getRaw();
	}

	static Nat CODECALL iteratorGetKey(const ArrayBase::Iter &v) {
		return v.getIndex();
	}

	ArrayIterType::ArrayIterType(Type *param)
		: Type(new (param) Str(S("Iter")), new (param) Array<Value>(), typeValue),
		  contents(param) {

		setSuper(ArrayBase::Iter::stormType(engine));
	}

	Bool ArrayIterType::loadAll() {
		Engine &e = engine;
		Value v(this, false);
		Value r(this, true);
		Value param(contents, true);

		Array<Value> *ref = valList(e, 1, r);
		Array<Value> *refref = valList(e, 2, r, r);

		Value vBool = Value(StormInfo<Bool>::type(e));
		Value vNat = Value(StormInfo<Nat>::type(e));
		add(nativeFunction(e, Value(), Type::CTOR, refref, address(&copyIterator))->makePure());
		add(nativeFunction(e, r, S("="), refref, address(&assignIterator))->makePure());
		add(nativeFunction(e, vBool, S("=="), refref, address(&iteratorEq))->makePure());
		add(nativeFunction(e, vBool, S("!="), refref, address(&iteratorNeq))->makePure());
		add(nativeFunction(e, r, S("++*"), ref, address(&ArrayBase::Iter::preIncRaw)));
		add(nativeFunction(e, v, S("*++"), ref, address(&ArrayBase::Iter::postIncRaw)));
		add(nativeFunction(e, vNat, S("k"), ref, address(&iteratorGetKey))->makePure());
		add(nativeFunction(e, param, S("v"), ref, address(&iteratorGet))->makePure());

		return Type::loadAll();
	}

}