git @ Cat's Eye Technologies Matchbox / master src / matchbox.js
master

Tree @master (Download .tar.gz)

matchbox.js @masterraw · history · blame

  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
"use strict";

/*
 * The contents of this file are in the public domain.
 * See the file UNLICENSE in the root directory for more information. 
 */

var matchboxScanner;
function initScanner() {
    matchboxScanner = (new yoob.Scanner()).init({
      table: [
        ['immediate', "^(\\d+)"],
        ['register',  "^([rR]\\d+)"],
        ['memory',    "^([mM]\\d+)"],
        ['opcode',    "^([a-zA-Z]+)"],
        ['comma',     "^(,)"]
      ]
    });
}

/*
 * Each instruction is an object with some fields:
 *   `opcode`: what the instruction does
 *   `srcType`: 'R' or 'M'
 *   `src`: location in registers or memory
 *   `destType`: 'R' or 'M'
 *   `dest`: location in registers or memory
 *   `reg`: index into an array of yoob.Tapes (not stored here)
 *          that it will use for its registers
 */
var Instruction = function() {
    this.init = function(cfg) {
        cfg = cfg || {};
        this.reg = cfg.reg;
        this.opcode = cfg.opcode;
        this.srcType = cfg.srcType;
        this.src = cfg.src;
        this.destType = cfg.destType;
        this.dest = cfg.dest;
        return this;
    };

    this.serialize = function() {
        return {
            reg: this.reg,
            opcode: this.opcode,
            srcType: this.srcType,
            src: this.src,
            destType: this.destType,
            dest: this.dest
        };
    };

    this.parse = function(str) {
        this.opcode = undefined;
        this.srcType = undefined;
        this.src = undefined;
        this.destType = undefined;
        this.dest = undefined;
        var s = matchboxScanner;
        s.reset(str);
        if (s.onType('opcode')) {
            this.opcode = s.token;
            s.scan();
            if (s.onType('immediate')) {
                this.srcType = 'I';
                this.src = parseInt(s.token, 10);
                s.scan();
            } else if (s.onType('register')) {
                this.srcType = 'R';
                this.src = parseInt(s.token.substr(1), 10);
                s.scan();
            } else if (s.onType('memory')) {
                this.srcType = 'M';
                this.src = parseInt(s.token.substr(1), 10);
                s.scan();
            }
            if (s.consume(',')) {
                if (s.onType('immediate')) {
                    this.destType = 'I';
                    this.dest = parseInt(s.token, 10);
                    s.scan();
                } else if (s.onType('register')) {
                    this.destType = 'R';
                    this.dest = parseInt(s.token.substr(1), 10);
                    s.scan();
                } else if (s.onType('memory')) {
                    this.destType = 'M';
                    this.dest = parseInt(s.token.substr(1), 10);
                    s.scan();
                }
            }
        }
        return this;
    };

    /*
     * Return an error message string if this Instruction is not well-formed.
     */
    this.check = function() {
        var srcType = this.srcType;
        var destType = this.destType;
        if (this.opcode === 'MOV') {
            if (srcType !== 'I' && srcType !== 'R' && srcType !== 'M') {
                return "Illegal source reference";
            }
            if (destType !== 'R' && destType !== 'M') {
                return "Illegal destination reference";
            }
            return true;
        } else if (this.opcode === 'INC') {
            if (srcType !== 'R' && srcType !== 'M') {
                return "Illegal source reference";
            }
            return true;
        } else if (this.opcode === 'WAIT') {
            if (srcType !== 'M') {
                return "Illegal source reference";
            }
            if (destType !== 'I') {
                return "Illegal destination reference";
            }
            return true;
        } else {
            return "Illegal opcode";
        }
    };

    /*
     * Given a yoob.Tape that represents shared memory, and an array of
     * yoob.Tapes that represent private register contexts, execute this
     * Instruction.  May return:
     *
     * true, to indicate that the instruction executed successfully;
     * an error string, to indicate that the instruction was malformed; or
     * null, to indicate this interleaving would not be possible.
     */
    this.execute = function(mem, regs) {
        if (this.opcode === 'MOV') {
            var val;
            if (this.srcType === 'I') {
                val = this.src;
            } else if (this.srcType === 'R') {
                val = regs[this.reg].get(this.src);
            } else if (this.srcType === 'M') {
                val = mem.get(this.src);
            } else {
                return "Illegal source reference";
            }
            if (this.destType === 'R') {
                regs[this.reg].put(this.dest, val);
            } else if (this.destType === 'M') {
                mem.put(this.dest, val);
            } else {
                return "Illegal destination reference";
            }
            return true;
        } else if (this.opcode === 'INC') {
            if (this.srcType === 'R') {
                regs[this.reg].put(this.src, regs[this.reg].get(this.src) + 1);
            } else if (this.srcType === 'M') {
                mem.put(this.src, mem.get(this.src) + 1);
            } else {
                return "Illegal source reference";
            }
            return true;
        } else if (this.opcode === 'WAIT') {
            if (this.srcType === 'M' && this.destType === 'I') {
                if (mem.get(this.src) !== this.dest) {
                    return null;
                }
            } else {
                return "Illegal source/destination reference";
            }
            return true;
        } else {
            return "Illegal opcode";
        }
    };

    this.toString = function() {
        return (
            this.opcode + ' ' +
            (this.srcType === 'I' ? '' : this.srcType) + this.src +
            (this.destType === undefined ? '' : (', ' + this.destType + this.dest))
        );
    };

    this.toHTML = function(regs) {
        return (
            '<span style="' + regs[this.reg].style + '">' +
            this.toString() +
            '</span>'
        );
    };
};

/*
 * Objects of this class can actually represent 2 interleaved programs,
 * as well as a single program.
 */
var Program = function() {
    this.init = function(cfg) {
        cfg = cfg || {};
        this.code = cfg.code || [];
        return this;
    };

    this.serialize = function() {
        var list = [];
        var code = this.code;

        for (var i = 0; i < code.length; i++) {
            list.push(code[i].serialize());
        }

        return list;
    };

    /*
     * Initialize this Program from a list of serialized Instruction objects.
     * Chainable.
     */
    this.reconstitute = function(list) {
        var code = [];

        for (var i = 0; i < list.length; i++) {
            code.push((new Instruction()).init(list[i]));
        }

        this.code = code;
        return this;
    };

    this.addInstruction = function(instr) {
        this.code.push(instr);
    };

    /*
     * Update all Instructions in this Program to use the given
     * register context index.  Chainable.
     */
    this.setRegistersIndex = function(reg) {
        var code = this.code;

        for (var i = 0; i < code.length; i++) {
            code[i].reg = reg;
        }

        return this;
    };

    /*
     * May return:
     *
     * true, to indicate that the program checked successfully; or
     * an error string, to indicate that an instruction was malformed.
     */
    this.check = function() {
        var code = this.code;

        for (var i = 0; i < code.length; i++) {
            var result = code[i].check();
            if (typeof result === "string") {
                return result;
            }
        }

        return true;
    };

    /*
     * May return:
     *
     * true, to indicate that the program executed successfully;
     * an error string, to indicate that an instruction was malformed; or
     * null, to indicate this interleaving would not be possible.
     */
    this.run = function(mem, regs) {
        var code = this.code;

        for (var i = 0; i < code.length; i++) {
            var result = code[i].execute(mem, regs);
            if (result === null || typeof result === "string") {
                return result;
            }
        }

        return true;
    };

    this.toHTML = function(regs) {
        var s = '';
        var code = this.code;

        for (var i = 0; i < code.length; i++) {
            s += code[i].toHTML(regs) + '<br/>';
        }

        return s;
    };
};

var Matchbox = function() {
    /*
     * `workerURL` should be the URL for `matchbox-worker.js`.
     * `progStyles` should be an array of CSS styles, one for each program.
     * `displayInterleaving` should be a function which takes a string
     *                       containing HTML, and (presumably) displays it
     * `updateStatus` should be a function which takes a string
     *                containing HTML, and (presumably) displays it
     */
    this.init = function(cfg) {
        cfg = cfg || {};
        this.workerURL = cfg.workerURL;
        this.progStyles = cfg.progStyles;
        this.displayInterleaving = cfg.displayInterleaving;
        this.updateStatus = cfg.updateStatus;
        initScanner();
        return this;
    };

    /* ------------------- parsing and loading ------------------- */

    this.parse = function(str) {
        var prog = (new Program()).init();
        var lines = str.split("\n");
        this.code = [];
        for (var i = 0; i < lines.length; i++) {
            if (!lines[i] || lines[i].charAt(0) === ';') continue;
            var instr = (new Instruction()).init();
            if (instr.parse(lines[i])) {
                prog.addInstruction(instr);
            } else {
                alert("Syntax error on line " + (i+1));
            }
        }
        return prog;
    };

    /*
     * Given a full Matchbox source (containing multiple programs,)
     * split into a three-elementary array which contains
     * - descriptive text
     * - text of program 0
     * - text of program 1
     */
    this.splitIntoProgramTexts = function(str) {
        var s = matchboxScanner;
        var texts = [[], [], []];
        var progNum = 0;
        var lines = str.split("\n");
        this.code = [];
        for (var i = 0; i < lines.length; i++) {
            var str = lines[i];
            if (progNum === 0 && str.trim().length === 0) {
                continue;
            }
            s.reset(str);
            if (s.onType('opcode') && s.token === 'PROG') {
                s.scan();
                if (s.onType('immediate')) {
                    progNum = parseInt(s.token, 10) + 1;
                    continue;
                }
            } else if (s.onType('opcode') && s.token === 'DESC') {
                s.scan();
                progNum = 0;
                continue;
            }
            texts[progNum].push(str);
        }
        return [texts[0].join("\n"), texts[1].join("\n"), texts[2].join("\n")];
    };

    /* -------------- recording results -------------- */

    this.clearResults = function() {
        this.results = {};
    };

    this.registerResult = function(mem) {
        var key = this.tapeToString(mem);
        if (this.results[key] === undefined) {
            this.results[key] = mem.clone();
        }
    };

    this.reportResults = function() {
        var resultCount = 0;

        for (var key in this.results) {
            if (this.results.hasOwnProperty(key)) {
                resultCount += 1;
            }
        }

        if (resultCount === 1) {
            this.updateStatus('<span style="color: white; background: green;">PASS</span>');
        } else {
            this.updateStatus('<span style="color: white; background: red;">FAIL</span>');
        }

        for (var key in this.results) {
            if (this.results.hasOwnProperty(key)) {
                this.reportState(this.results[key], []);
            }
        }
    };

    this.reportState = function(mem, regs) {
        this.updateStatus('<span style="color: black; background: blue;">MEM</span> ' + this.tapeToString(mem, 'M'));
        for (var i = 0; i < regs.length; i++) {
            this.updateStatus('<span style="color: black; background: blue;">REGS ' + i + '</span> ' + this.tapeToString(regs[i], 'R'));
        }
    };

    /* -------------- running and race-condition finding -------------- */

    this.runSingleProgram = function(progText) {
        var regs = [(new yoob.Tape()).init({ default: 0 })];
        regs[0].style = this.progStyles[0];
        var prog = this.parse(progText).setRegistersIndex(0);

        this.updateStatus("Running...");

        this.displayInterleaving(prog.toHTML(regs));

        var mem = (new yoob.Tape()).init({ default: 0 });

        var html = ''
        var result = prog.run(mem, regs);
        if (typeof result === 'string') {
            this.updateStatus('<span style="color: yellow; background: red;">ERROR</span> ' + result);
        } else if (result === null) {
            this.updateStatus('<span style="color: black; background: yellow;">WAIT</span> Program hung on WAIT');
            this.reportState(mem, regs);
        } else {
            this.updateStatus('Finished.');
            this.reportState(mem, regs);
        }
    };

    this.findRaceConditions = function(prog1text, prog2text) {
        this.stop();
        var regs = [
            (new yoob.Tape()).init({ default: 0 }),
            (new yoob.Tape()).init({ default: 0 })
        ];
        regs[0].style = this.progStyles[0];
        var prog1 = this.parse(prog1text).setRegistersIndex(0);
        var result = prog1.check();
        if (typeof result === 'string') {
            this.updateStatus('<span style="color: yellow; background: red;">ERROR</span> ' + result);
            return;
        }

        regs[1].style = this.progStyles[1];
        var prog2 = this.parse(prog2text).setRegistersIndex(1);
        var result = prog2.check();
        if (typeof result === 'string') {
            this.updateStatus('<span style="color: yellow; background: red;">ERROR</span> ' + result);
            return;
        }

        var $this = this;
        this.worker = new Worker(this.workerURL);
        this.worker.addEventListener('message', function(e) {
            var interleavings = e.data;
            /* reconstitute Programs from interleavings */
            for (var i = 0; i < interleavings.length; i++) {
                interleavings[i] = (new Program()).reconstitute(interleavings[i]);
            }
            $this.updateStatus("Running " + interleavings.length + " interleavings...");
            $this.startRunningInterleavedPrograms(interleavings, regs);
        });
        this.updateStatus("Computing all interleavings...");
        this.worker.postMessage(["interleave", prog1.serialize(), prog2.serialize()]);
    };

    this.startRunningInterleavedPrograms = function(interleavings, regs) {
        if (this.intervalId !== undefined)
            return;
        this.clearResults();
        this.interleavingCounter = 0;
        var $this = this;
        this.intervalId = setInterval(function() {
            if ($this.interleavingCounter >= interleavings.length) {
                $this.reportResults();
                $this.stop();
                return;
            }
            $this.runInterleavedProgram(interleavings[$this.interleavingCounter], regs);
            $this.interleavingCounter += 1;
        }, 1000/60);
    };

    this.stop = function() {
        if (this.worker) {
            this.worker.terminate();
            this.worker = undefined;
            this.updateStatus("Stopped web worker.");
        }
        if (this.intervalId) {
            clearInterval(this.intervalId);
            this.intervalId = undefined;
            this.updateStatus("Stopped.");
        }
        this.interleavingCounter = 0;
    };

    this.reset = function() {
        this.stop();
        this.displayInterleaving("");
        this.updateStatus("Reset.");
    };

    this.runInterleavedProgram = function(prog, regs) {
        var mem = (new yoob.Tape()).init({ default: 0 });

        var html = '';

        html += prog.toHTML(regs);
        regs[0].clear();
        regs[1].clear();

        var result = prog.run(mem, regs);
        if (typeof result === 'string') {
            html += "Error: " + result + "<br/>";
        } else if (result === null) {
            html += "(can't happen)";
        } else {
            this.registerResult(mem);
        }
        html += '<br/>';

        this.displayInterleaving(html);
    };

    /* ------------------- utility methods ------------------- */

    this.tapeToString = function(tape, prefix) {
        var s = '';
        var v = [];
        tape.foreach(function(pos, val) {
            v.push(prefix + pos + "=" + val);
        });
        return v.join(", ");
    };
};