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
|
// === Auto-generated preamble library stuff ===
//========================================
// Runtime code shared with compiler
//========================================
{{RUNTIME}}
#if SAFE_HEAP
//========================================
// Debugging tools - Heap
//========================================
var HEAP_WATCHED = [];
var HEAP_HISTORY = [];
function SAFE_HEAP_CLEAR(dest) {
#if SAFE_HEAP_LOG
print('SAFE_HEAP clear: ' + dest);
#endif
HEAP_HISTORY[dest] = [];
}
var SAFE_HEAP_ERRORS = 0;
var ACCEPTABLE_SAFE_HEAP_ERRORS = 0;
function SAFE_HEAP_ACCESS(dest, type, store, ignore) {
//if (dest === A_NUMBER) print ([dest, type, store] + ' ' + new Error().stack); // Something like this may be useful, in debugging
if (type && type[type.length-1] == '*') type = 'i32'; // pointers are ints, for our purposes here
// Note that this will pass even with unions: You can store X, load X, then store Y and load Y.
// You cannot, however, do the nonportable act of store X and load Y!
if (store) {
HEAP_HISTORY[dest] = ignore ? null : type;
} else {
#if USE_TYPED_ARRAYS == 0
if (!HEAP[dest] && HEAP[dest] !== 0 && HEAP[dest] !== false) { // false can be the result of a mathop comparator
throw('Warning: Reading an invalid value at ' + dest + ' :: ' + new Error().stack + '\n');
}
#endif
if (type === null) return;
var history = HEAP_HISTORY[dest];
if (history === null) return;
if (!ignore)
assert(history, 'Must have a history for a safe heap load! ' + dest + ':' + type); // Warning - bit fields in C structs cause loads+stores for each store, so
// they will show up here...
// assert((history && history[0]) /* || HEAP[dest] === 0 */, "Loading from where there was no store! " + dest + ',' + HEAP[dest] + ',' + type + ', \n\n' + new Error().stack + '\n');
// if (history[0].type !== type) {
if (history !== type && !ignore) {
print('Load-store consistency assumption failure! ' + dest);
print('\n');
print(JSON.stringify(history));
print('\n');
print('LOAD: ' + type + ', ' + new Error().stack);
print('\n');
SAFE_HEAP_ERRORS++;
assert(SAFE_HEAP_ERRORS <= ACCEPTABLE_SAFE_HEAP_ERRORS, 'Load-store consistency assumption failure!');
}
}
}
#if USE_TYPED_ARRAYS == 2
var warned64 = false;
function warn64() {
if (!warned64) {
__ATEXIT__.push({ func: function() {
print('Warning: using a 64-bit type with USE_TYPED_ARRAYS == 2. This is emulated as a 32-bit value, and will likely fail horribly.');
} });
warned64 = true;
}
}
#endif
function SAFE_HEAP_STORE(dest, value, type, ignore) {
#if SAFE_HEAP_LOG
print('SAFE_HEAP store: ' + [dest, type, value, ignore]);
#endif
if (!ignore && !value && value !== 0 && value !== false) { // false can be the result of a mathop comparator
throw('Warning: Writing an invalid value of ' + JSON.stringify(value) + ' at ' + dest + ' :: ' + new Error().stack + '\n');
}
SAFE_HEAP_ACCESS(dest, type, true, ignore);
if (dest in HEAP_WATCHED) {
print((new Error()).stack);
throw "Bad store!" + dest;
}
#if USE_TYPED_ARRAYS == 1
if (type === null) {
IHEAP[dest] = value;
FHEAP[dest] = value;
} else if (type in Runtime.FLOAT_TYPES) {
FHEAP[dest] = value;
} else {
IHEAP[dest] = value;
}
#else
#if USE_TYPED_ARRAYS == 2
assert(type != 'null', 'typed arrays 2 with null type!');
if (type[type.length-1] === '*') type = 'i32'; // hardcoded pointers as 32-bit
switch(type) {
case 'i1': case 'i8': HEAP8[dest] = value; break;
case 'i16': assert(dest % 2 === 0, type + ' loads must be aligned'); HEAP16[dest>>1] = value; break;
case 'i32': assert(dest % 4 === 0, type + ' loads must be aligned'); HEAP32[dest>>2] = value; break;
case 'i64': assert(dest % 4 === 0, type + ' loads must be aligned'); warn64(); HEAP32[dest>>2] = value; break; // XXX store int64 as int32
case 'float': assert(dest % 4 === 0, type + ' loads must be aligned'); HEAPF32[dest>>2] = value; break;
case 'double': assert(dest % 4 === 0, type + ' loads must be aligned'); warn64(); HEAPF32[dest>>2] = value; break; // XXX store doubles as floats
default: throw 'weird type for typed array II: ' + type + new Error().stack;
}
#else
HEAP[dest] = value;
#endif
#endif
}
function SAFE_HEAP_LOAD(dest, type, unsigned, ignore) {
SAFE_HEAP_ACCESS(dest, type, ignore);
#if USE_TYPED_ARRAYS == 1
if (type in Runtime.FLOAT_TYPES) {
#if SAFE_HEAP_LOG
print('SAFE_HEAP load: ' + [dest, type, FHEAP[dest], ignore]);
#endif
return FHEAP[dest];
} else {
#if SAFE_HEAP_LOG
print('SAFE_HEAP load: ' + [dest, type, IHEAP[dest], ignore]);
#endif
return IHEAP[dest];
}
#else
#if USE_TYPED_ARRAYS == 2
#if SAFE_HEAP_LOG
var originalType = type;
#endif
var ret;
if (type[type.length-1] === '*') type = 'i32'; // hardcoded pointers as 32-bit
switch(type) {
case 'i1': case 'i8': {
ret = (unsigned ? HEAPU8 : HEAP8)[dest];
break;
}
case 'i16': {
assert(dest % 2 === 0, type + ' loads must be aligned');
ret = (unsigned ? HEAPU16 : HEAP16)[dest>>1];
break;
}
case 'i32': case 'i64': { // XXX store int64 as int32
assert(dest % 4 === 0, type + ' loads must be aligned');
if (type === 'i64') warn64();
ret = (unsigned ? HEAPU32 : HEAP32)[dest>>2];
break;
}
case 'float': case 'double': { // XXX store doubles as floats
assert(dest % 4 === 0, type + ' loads must be aligned');
if (type === 'double') warn64();
ret = HEAPF32[dest>>2];
break;
}
default: throw 'weird type for typed array II: ' + type;
}
#if SAFE_HEAP_LOG
print('SAFE_HEAP load: ' + [dest, originalType, ret, unsigned, ignore]);
#endif
return ret;
#else
#if SAFE_HEAP_LOG
print('SAFE_HEAP load: ' + [dest, type, HEAP[dest], ignore]);
#endif
return HEAP[dest];
#endif
#endif
}
function SAFE_HEAP_COPY_HISTORY(dest, src) {
#if SAFE_HEAP_LOG
print('SAFE_HEAP copy: ' + [dest, src]);
#endif
HEAP_HISTORY[dest] = HEAP_HISTORY[src];
SAFE_HEAP_ACCESS(dest, HEAP_HISTORY[dest] || null, true, false);
}
//==========================================
#endif
var CorrectionsMonitor = {
#if AUTO_OPTIMIZE
MAX_ALLOWED: Infinity,
#else
MAX_ALLOWED: 0, // XXX
#endif
corrections: 0,
sigs: {},
note: function(type, succeed, sig) {
if (!succeed) {
this.corrections++;
if (this.corrections >= this.MAX_ALLOWED) abort('\n\nToo many corrections!');
}
#if AUTO_OPTIMIZE
if (!sig)
sig = (new Error().stack).toString().split('\n')[2].split(':').slice(-1)[0]; // Spidermonkey-specific FIXME
sig = type + '|' + sig;
if (!this.sigs[sig]) {
//print('Correction: ' + sig);
this.sigs[sig] = [0, 0]; // fail, succeed
}
this.sigs[sig][succeed ? 1 : 0]++;
#endif
},
print: function() {
var items = [];
for (var sig in this.sigs) {
items.push({
sig: sig,
fails: this.sigs[sig][0],
succeeds: this.sigs[sig][1],
total: this.sigs[sig][0] + this.sigs[sig][1]
});
}
items.sort(function(x, y) { return y.total - x.total; });
for (var i = 0; i < items.length; i++) {
var item = items[i];
print(item.sig + ' : ' + item.total + ' hits, %' + (Math.floor(100*item.fails/item.total)) + ' failures');
}
}
};
#if CORRECT_ROUNDINGS
function cRound(x) {
return x >= 0 ? Math.floor(x) : Math.ceil(x);
}
#endif
#if CHECK_OVERFLOWS
//========================================
// Debugging tools - Mathop overflows
//========================================
function CHECK_OVERFLOW(value, bits, ignore, sig) {
if (ignore) return value;
var twopbits = Math.pow(2, bits);
var twopbits1 = Math.pow(2, bits-1);
// For signedness issue here, see settings.js, CHECK_SIGNED_OVERFLOWS
#if CHECK_SIGNED_OVERFLOWS
if (value === Infinity || value === -Infinity || value >= twopbits1 || value < -twopbits1) {
CorrectionsMonitor.note('SignedOverflow', 0, sig);
if (value === Infinity || value === -Infinity || Math.abs(value) >= twopbits) CorrectionsMonitor.note('Overflow');
#else
if (value === Infinity || value === -Infinity || Math.abs(value) >= twopbits) {
CorrectionsMonitor.note('Overflow', 0, sig);
#endif
#if CORRECT_OVERFLOWS
// Fail on >32 bits - we warned at compile time
if (bits <= 32) {
value = value & (twopbits - 1);
}
#endif
} else {
#if CHECK_SIGNED_OVERFLOWS
CorrectionsMonitor.note('SignedOverflow', 1, sig);
#endif
CorrectionsMonitor.note('Overflow', 1, sig);
}
return value;
}
#endif
#if LABEL_DEBUG
//========================================
// Debugging tools - Code flow progress
//========================================
var INDENT = '';
#endif
#if EXECUTION_TIMEOUT
//========================================
// Debugging tools - Execution timeout
//========================================
var START_TIME = Date.now();
#endif
//========================================
// Runtime essentials
//========================================
var __globalConstructor__ = function globalConstructor() {
};
var __THREW__ = false; // Used in checking for thrown exceptions.
var __ATEXIT__ = [];
var ABORT = false;
var undef = 0;
function abort(text) {
print(text + ':\n' + (new Error).stack);
ABORT = true;
throw "Assertion: " + text;
}
function assert(condition, text) {
if (!condition) {
abort('Assertion failed: ' + text);
}
}
// Allocates memory for some data and initializes it properly.
var ALLOC_NORMAL = 0; // Tries to use _malloc()
var ALLOC_STACK = 1; // Lives for the duration of the current function call
var ALLOC_STATIC = 2; // Cannot be freed
function allocate(slab, types, allocator) {
var zeroinit, size;
if (typeof slab === 'number') {
zeroinit = true;
size = slab;
} else {
zeroinit = false;
size = slab.length;
}
var ret = [_malloc, Runtime.stackAlloc, Runtime.staticAlloc][allocator ? allocator : ALLOC_STATIC](Math.max(size, 1));
var singleType = typeof types === 'string' ? types : null;
var i = 0, type;
while (i < size) {
var curr = zeroinit ? 0 : slab[i];
if (typeof curr === 'function') {
curr = Runtime.getFunctionIndex(curr);
}
type = singleType || types[i];
if (type === 0) {
i++;
continue;
}
#if ASSERTIONS
assert(type, 'Must know what type to store in allocate!');
#endif
if (type === 'i1') {
{{{ makeSetValue(0, 'ret+i', 'curr', 'i1') }}}
i += {{{ getNativeFieldSize('i1', true) }}};
} else if (type === 'i8') {
{{{ makeSetValue(0, 'ret+i', 'curr', 'i8') }}}
i += {{{ getNativeFieldSize('i8', true) }}};
} else if (type === 'i16') {
{{{ makeSetValue(0, 'ret+i', 'curr', 'i16') }}}
i += {{{ getNativeFieldSize('i16', true) }}};
} else if (type === 'i32' || type[type.length-1] === '*') { // hardcoded pointers as 32-bit
{{{ makeSetValue(0, 'ret+i', 'curr', 'i32') }}}
i += {{{ getNativeFieldSize('i32', true) }}};
} else if (type === 'float') {
{{{ makeSetValue(0, 'ret+i', 'curr', 'float') }}}
i += {{{ getNativeFieldSize('float', true) }}};
} else if (type === 'i64') {
{{{ makeSetValue(0, 'ret+i', 'curr', 'i64') }}}
i += {{{ getNativeFieldSize('i64', true) }}};
} else if (type === 'double') {
{{{ makeSetValue(0, 'ret+i', 'curr', 'double') }}}
i += {{{ getNativeFieldSize('double', true) }}};
} else {
abort('invalid type for allocate: ' + type);
}
}
return ret;
}
Module['allocate'] = allocate;
function Pointer_stringify(ptr) {
var ret = "";
var i = 0;
var t;
while (1) {
t = String.fromCharCode({{{ makeGetValue('ptr', 'i', 'i8', 0, 1) }}});
if (t == "\0") { break; } else {}
ret += t;
i += 1;
}
return ret;
}
function Array_stringify(array) {
var ret = "";
for (var i = 0; i < array.length; i++) {
ret += String.fromCharCode(array[i]);
}
return ret;
}
// Memory management
var PAGE_SIZE = 4096;
function alignMemoryPage(x) {
return Math.ceil(x/PAGE_SIZE)*PAGE_SIZE;
}
var HEAP;
#if USE_TYPED_ARRAYS == 1
var IHEAP, FHEAP;
#endif
#if USE_TYPED_ARRAYS == 2
var HEAP8, HEAPU8, HEAP16, HEAPU16, HEAP32, HEAPU32, HEAPF32;
#endif
var STACK_ROOT, STACKTOP, STACK_MAX;
var STATICTOP;
var HAS_TYPED_ARRAYS = false;
var TOTAL_MEMORY = 50*1024*1024;
// Initialize the runtime's memory
#if USE_TYPED_ARRAYS
HAS_TYPED_ARRAYS = false;
try {
HAS_TYPED_ARRAYS = !!Int32Array && !!Float64Array && !!(new Int32Array(1)['subarray']); // check for full engine support (use string 'subarray' to avoid closure compiler confusion)
} catch(e) {}
if (HAS_TYPED_ARRAYS) {
#if USE_TYPED_ARRAYS == 1
HEAP = IHEAP = new Int32Array(TOTAL_MEMORY);
FHEAP = new Float64Array(TOTAL_MEMORY);
#endif
#if USE_TYPED_ARRAYS == 2
var buffer = new ArrayBuffer(TOTAL_MEMORY);
HEAP8 = new Int8Array(buffer);
HEAP16 = new Int16Array(buffer);
HEAP32 = new Int32Array(buffer);
HEAPU8 = new Uint8Array(buffer);
HEAPU16 = new Uint16Array(buffer);
HEAPU32 = new Uint32Array(buffer);
HEAPF32 = new Float32Array(buffer);
#endif
} else
#endif
{
// Without this optimization, Chrome is slow. Sadly, the constant here needs to be tweaked depending on the code being run...
var FAST_MEMORY = TOTAL_MEMORY/32;
HEAP = new Array(FAST_MEMORY);
for (var i = 0; i < FAST_MEMORY; i++) {
HEAP[i] = 0; // XXX We do *not* use {{| makeSetValue(0, 'i', 0, 'null') |}} here, since this is done just to optimize runtime speed
}
#if USE_TYPED_ARRAYS == 1
IHEAP = FHEAP = HEAP;
#endif
#if USE_TYPED_ARRAYS == 2
abort('Cannot fallback to non-typed array case in USE_TYPED_ARRAYS == 2: Code is too specialized');
#endif
}
var base = intArrayFromString('(null)'); // So printing %s of NULL gives '(null)'
// Also this ensures we leave 0 as an invalid address, 'NULL'
for (var i = 0; i < base.length; i++) {
{{{ makeSetValue(0, 'i', 'base[i]', 'i8') }}}
}
Module['HEAP'] = HEAP;
#if USE_TYPED_ARRAYS == 1
Module['IHEAP'] = IHEAP;
Module['FHEAP'] = FHEAP;
#endif
#if USE_TYPED_ARRAYS == 2
Module['HEAP8'] = HEAP8;
Module['HEAP16'] = HEAP16;
Module['HEAP32'] = HEAP32;
Module['HEAPU8'] = HEAPU8;
Module['HEAPU16'] = HEAPU16;
Module['HEAPU32'] = HEAPU32;
Module['HEAPF32'] = HEAPF32;
#endif
STACK_ROOT = STACKTOP = alignMemoryPage(10);
var TOTAL_STACK = 1024*1024; // XXX: Changing this value can lead to bad perf on v8!
STACK_MAX = STACK_ROOT + TOTAL_STACK;
STATICTOP = alignMemoryPage(STACK_MAX);
function __shutdownRuntime__() {
while(__ATEXIT__.length > 0) {
var atexit = __ATEXIT__.pop();
var func = atexit.func;
if (typeof func === 'number') {
func = FUNCTION_TABLE[func];
}
func(atexit.arg || null);
}
// allow browser to GC, set heaps to null?
// Print summary of correction activity
CorrectionsMonitor.print();
}
// Copies a list of num items on the HEAP into a
// a normal JavaScript array of numbers
function Array_copy(ptr, num) {
// TODO: In the SAFE_HEAP case, do some reading here, for debugging purposes - currently this is an 'unnoticed read'.
#if USE_TYPED_ARRAYS == 1
if (HAS_TYPED_ARRAYS) {
return Array.prototype.slice.call(IHEAP.subarray(ptr, ptr+num)); // Make a normal array out of the typed 'view'
// Consider making a typed array here, for speed?
} else {
return IHEAP.slice(ptr, ptr+num);
}
#endif
#if USE_TYPED_ARRAYS == 2
if (HAS_TYPED_ARRAYS) {
return Array.prototype.slice.call(HEAP8.subarray(ptr, ptr+num)); // Make a normal array out of the typed 'view'
// Consider making a typed array here, for speed?
} else {
return HEAP8.slice(ptr, ptr+num);
}
#endif
return HEAP.slice(ptr, ptr+num);
}
function String_len(ptr) {
var i = 0;
while ({{{ makeGetValue('ptr', 'i', 'i8') }}}) i++; // Note: should be |!= 0|, technically. But this helps catch bugs with undefineds
return i;
}
// Copies a C-style string, terminated by a zero, from the HEAP into
// a normal JavaScript array of numbers
function String_copy(ptr, addZero) {
var len = String_len(ptr);
if (addZero) len++;
var ret = Array_copy(ptr, len);
if (addZero) ret[len-1] = 0;
return ret;
}
// Tools
var PRINTBUFFER = '';
function __print__(text) {
if (text === null) {
// Flush
print(PRINTBUFFER);
PRINTBUFFER = '';
return;
}
// We print only when we see a '\n', as console JS engines always add
// one anyhow.
PRINTBUFFER = PRINTBUFFER + text;
var endIndex;
while ((endIndex = PRINTBUFFER.indexOf('\n')) != -1) {
print(PRINTBUFFER.substr(0, endIndex));
PRINTBUFFER = PRINTBUFFER.substr(endIndex + 1);
}
}
function jrint(label, obj) { // XXX manual debugging
if (!obj) {
obj = label;
label = '';
} else
label = label + ' : ';
print(label + JSON.stringify(obj));
}
// This processes a JS string into a C-line array of numbers, 0-terminated.
// For LLVM-originating strings, see parser.js:parseLLVMString function
function intArrayFromString(stringy, dontAddNull) {
var ret = [];
var t;
var i = 0;
while (i < stringy.length) {
ret.push(stringy.charCodeAt(i));
i = i + 1;
}
if (!dontAddNull) {
ret.push(0);
}
return ret;
}
Module['intArrayFromString'] = intArrayFromString;
function intArrayToString(array) {
var ret = '';
for (var i = 0; i < array.length; i++) {
ret += String.fromCharCode(array[i]);
}
return ret;
}
{{{ unSign }}}
{{{ reSign }}}
// Use console read if available, otherwise we are in a browser, use an XHR
if (!this['read']) {
this['read'] = function(url) {
// TODO: use mozResponseArrayBuffer/responseStream/etc. if available
var xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
xhr.overrideMimeType('text/plain; charset=x-user-defined'); // ask for binary data
xhr.send(null);
if (xhr.status != 200 && xhr.status != 0) throw 'failed to open: ' + url;
return xhr.responseText;
};
}
function readBinary(filename) {
var stringy = read(filename);
var data = new Array(stringy.length);
for (var i = 0; i < stringy.length; i++) {
data[i] = stringy.charCodeAt(i) & 0xff;
}
return data;
}
// === Body ===
|