summaryrefslogtreecommitdiff
path: root/src/wasm-interpreter.h
blob: 1cdd86387b6a20e3e4400825be5415b531892147 (plain)
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

//
// Simple WebAssembly interpreter. This operates directly on the AST,
// for simplicity and clarity. A goal is for it to be possible for
// people to read this code and understand WebAssembly semantics.
//

#include <limits.h>

#include "wasm.h"

namespace wasm {

using namespace cashew;

// Utilities

IString WASM("wasm");

int32_t safe_clz(int32_t v) {
  if (v == 0) return 32;
  return __builtin_clz(v);
}

int32_t safe_ctz(int32_t v) {
  if (v == 0) return 32;
  return __builtin_ctz(v);
}

enum {
  pageSize = 64*1024,
  maxCallDepth = 250
};

//
// An instance of a WebAssembly module, which can execute it via AST interpretation.
//
// To embed this interpreter, you need to provide an ExternalInterface instance
// (see below) which provides the embedding-specific details, that is, how to
// connect to the embedding implementation.
//
// To call into the interpreter, use callExport.
//

class ModuleInstance {
public:
  typedef std::vector<Literal> LiteralList;

  //
  // You need to implement one of these to create a concrete interpreter. The
  // ExternalInterface provides embedding-specific functionality like calling
  // an imported function or accessing memory.
  //
  struct ExternalInterface {
    virtual void init(Module& wasm) {}
    virtual Literal callImport(Import* import, LiteralList& arguments) = 0;
    virtual Literal load(Load* load, size_t addr) = 0;
    virtual void store(Store* store, size_t addr, Literal value) = 0;
    virtual void growMemory(size_t oldSize, size_t newSize) = 0;
    virtual void trap() = 0;
  };

  Module& wasm;

  ModuleInstance(Module& wasm, ExternalInterface* externalInterface) : wasm(wasm), externalInterface(externalInterface) {
    memorySize = wasm.memory.initial;
    externalInterface->init(wasm);
  }

  Literal callExport(IString name, LiteralList& arguments) {
    Export *export_ = wasm.exportsMap[name];
    if (!export_) externalInterface->trap();
    return callFunction(export_->value, arguments);
  }

private:

  size_t callDepth = 0;

#ifdef WASM_INTERPRETER_DEBUG
  int indent = 0;
#endif

  //
  // Calls a function. This can be used both internally (calls from
  // the interpreter to another method), or when you want to call into
  // the module.
  //
  Literal callFunction(IString name, LiteralList& arguments) {

    class FunctionScope {
    public:
      std::map<IString, Literal> locals;
      Function* function;

      FunctionScope(Function* function, LiteralList& arguments) : function(function) {
        assert(function->params.size() == arguments.size());
        for (size_t i = 0; i < arguments.size(); i++) {
          assert(function->params[i].type == arguments[i].type);
          locals[function->params[i].name] = arguments[i];
        }
        for (auto& local : function->locals) {
          locals[local.name].type = local.type;
        }
      }
    };

    // Stuff that flows around during executing expressions: a literal, or a change in control flow
    class Flow {
    public:
      Flow() {}
      Flow(Literal value) : value(value) {}
      Flow(IString breakTo) : breakTo(breakTo) {}

      Literal value;
      IString breakTo; // if non-null, a break is going on

      bool breaking() { return breakTo.is(); }

      void clearIf(IString target) {
        if (breakTo == target) {
          breakTo.clear();
        }
      }

      std::ostream& print(std::ostream& o) {
        o << "(flow " << (breakTo.is() ? breakTo.str : "-") << " : " << value << ')';
        return o;
      }
    };

#ifdef WASM_INTERPRETER_DEBUG
    struct IndentHandler {
      int& indent;
      const char *name;
      IndentHandler(int& indent, const char *name, Expression *expression) : indent(indent), name(name) {
        doIndent(std::cout, indent);
        std::cout << "visit " << name << " :\n";
        indent++;
        //doIndent(std::cout, indent);
        //expression->print(std::cout, indent) << '\n';
        //indent++;
      }
      ~IndentHandler() {
        //indent--;
        indent--;
        doIndent(std::cout, indent);
        std::cout << "exit " << name << '\n';
      }
    };
    #define NOTE_ENTER(x) IndentHandler indentHandler(instance.indent, x, curr);
    #define NOTE_EVAL1(p0) { doIndent(std::cout, instance.indent); std::cout << "eval in " << indentHandler.name << '('  << p0 << ")\n"; }
    #define NOTE_EVAL2(p0, p1) { doIndent(std::cout, instance.indent); std::cout << "eval in " << indentHandler.name << '('  << p0 << ", " << p1 << ")\n"; }
#else
    #define NOTE_ENTER(x)
    #define NOTE_EVAL1(p0)
    #define NOTE_EVAL2(p0, p1)
#endif

    // Execute a statement
    class ExpressionRunner : public WasmVisitor<Flow> {
      ModuleInstance& instance;
      FunctionScope& scope;

    public:
      ExpressionRunner(ModuleInstance& instance, FunctionScope& scope) : instance(instance), scope(scope) {}

      Flow visitBlock(Block *curr) override {
        NOTE_ENTER("Block");
        Flow flow;
        for (auto expression : curr->list) {
          flow = visit(expression);
          if (flow.breaking()) {
            flow.clearIf(curr->name);
            return flow;
          }
        }
        return flow;
      }
      Flow visitIf(If *curr) override {
        NOTE_ENTER("If");
        Flow flow = visit(curr->condition);
        if (flow.breaking()) return flow;
        NOTE_EVAL1(flow.value);
        if (flow.value.geti32()) return visit(curr->ifTrue);
        if (curr->ifFalse) return visit(curr->ifFalse);
        return Flow();
      }
      Flow visitLoop(Loop *curr) override {
        NOTE_ENTER("Loop");
        while (1) {
          Flow flow = visit(curr->body);
          if (flow.breaking()) {
            if (flow.breakTo == curr->in) continue; // lol
            flow.clearIf(curr->out);
            return flow;
          }
        }
      }
      Flow visitLabel(Label *curr) override {
        NOTE_ENTER("Label");
        Flow flow = visit(curr->body);
        flow.clearIf(curr->name);
        return flow;
      }
      Flow visitBreak(Break *curr) override {
        NOTE_ENTER("Break");
        if (curr->value) {
          Flow flow = visit(curr->value);
          if (!flow.breaking()) {
            flow.breakTo = curr->name;
          }
          return flow;
        }
        return Flow(curr->name);
      }
      Flow visitSwitch(Switch *curr) override {
        NOTE_ENTER("Switch");
        Flow flow = visit(curr->value);
        if (flow.breaking()) {
          flow.clearIf(curr->name);
          return flow;
        }
        NOTE_EVAL1(flow.value);
        int64_t index = flow.value.getInteger();
        Name target = curr->default_;
        if (index >= 0 && index < curr->targets.size()) {
          target = curr->targets[index];
        }
        // This is obviously very inefficient. This should be a cached data structure
        std::map<Name, size_t> caseMap; // name => index in cases
        for (size_t i = 0; i < curr->cases.size(); i++) {
          caseMap[curr->cases[i].name] = i;
        }
        auto iter = caseMap.find(target);
        if (iter == caseMap.end()) {
          // not in the cases, so this is a break outside
          return Flow(target);
        }
        size_t caseIndex = iter->second;
        assert(caseIndex < curr->cases.size());
        while (caseIndex < curr->cases.size()) {
          Switch::Case& c = curr->cases[caseIndex];
          flow = visit(c.body);
          if (flow.breaking()) {
            flow.clearIf(curr->name);
            break;
          }
          caseIndex++;
        }
        return flow;
      }

      Flow generateArguments(const ExpressionList& operands, LiteralList& arguments) {
        arguments.reserve(operands.size());
        for (auto expression : operands) {
          Flow flow = visit(expression);
          if (flow.breaking()) return flow;
          arguments.push_back(flow.value);
        }
        return Flow();
      }

      Flow visitCall(Call *curr) override {
        NOTE_ENTER("Call");
        LiteralList arguments;
        Flow flow = generateArguments(curr->operands, arguments);
        if (flow.breaking()) return flow;
        Flow ret = instance.callFunction(curr->target, arguments);
#ifdef WASM_INTERPRETER_DEBUG
        std::cout << "(returned to " << scope.function->name << ")\n";
#endif
        return ret;
      }
      Flow visitCallImport(CallImport *curr) override {
        NOTE_ENTER("CallImport");
        LiteralList arguments;
        Flow flow = generateArguments(curr->operands, arguments);
        if (flow.breaking()) return flow;
        return instance.externalInterface->callImport(instance.wasm.importsMap[curr->target], arguments);
      }
      Flow visitCallIndirect(CallIndirect *curr) override {
        NOTE_ENTER("CallIndirect");
        Flow target = visit(curr->target);
        if (target.breaking()) return target;
        size_t index = target.value.geti32();
        if (index >= instance.wasm.table.names.size()) trap();
        Name name = instance.wasm.table.names[index];
        Function *func = instance.wasm.functionsMap[name];
        if (func->type.is() && func->type != curr->type->name) trap();
        LiteralList arguments;
        Flow flow = generateArguments(curr->operands, arguments);
        if (flow.breaking()) return flow;
        return instance.callFunction(name, arguments);
      }

      Flow visitGetLocal(GetLocal *curr) override {
        NOTE_ENTER("GetLocal");
        IString name = curr->name;
        NOTE_EVAL1(scope.locals[name]);
        return scope.locals[name];
      }
      Flow visitSetLocal(SetLocal *curr) override {
        NOTE_ENTER("SetLocal");
        IString name = curr->name;
        Flow flow = visit(curr->value);
        if (flow.breaking()) return flow;
        NOTE_EVAL1(flow.value);
        scope.locals[name] = flow.value;
        return flow;
      }
      Flow visitLoad(Load *curr) override {
        NOTE_ENTER("Load");
        Flow flow = visit(curr->ptr);
        if (flow.breaking()) return flow;
        return instance.externalInterface->load(curr, instance.getFinalAddress(curr, flow.value));
      }
      Flow visitStore(Store *curr) override {
        NOTE_ENTER("Store");
        Flow ptr = visit(curr->ptr);
        if (ptr.breaking()) return ptr;
        Flow value = visit(curr->value);
        if (value.breaking()) return value;
        instance.externalInterface->store(curr, instance.getFinalAddress(curr, ptr.value), value.value);
        return value;
      }
      Flow visitConst(Const *curr) override {
        NOTE_ENTER("Const");
        NOTE_EVAL1(curr->value);
        return Flow(curr->value); // heh
      }
      Flow visitUnary(Unary *curr) override {
        NOTE_ENTER("Unary");
        Flow flow = visit(curr->value);
        if (flow.breaking()) return flow;
        Literal value = flow.value;
        NOTE_EVAL1(value);
        if (value.type == i32) {
          int32_t v = value.geti32();
          switch (curr->op) {
            case Clz: return Literal(safe_clz(v));
            case Ctz: {
              if (v == 0) return Literal(32);
              return Literal((int32_t)safe_ctz(v));
            }
            case Popcnt: return Literal((int32_t)__builtin_popcount(v));
            default: abort();
          }
        }
        if (value.type == i64) {
          int64_t v = value.geti64();
          int32_t high = v >> 32, low = v;
          switch (curr->op) {
            case Clz: {
              if (v == 0) return Literal((int64_t)64);
              if (high == 0) return Literal(32+(int64_t)safe_clz(low));
              return Literal((int64_t)safe_clz(high));
            }
            case Ctz: {
              if (v == 0) return Literal((int64_t)64);
              if (low == 0) return Literal(32+(int64_t)safe_ctz(high));
              return Literal((int64_t)safe_ctz(low));
            }
            case Popcnt: return Literal(int64_t(__builtin_popcount(low) + __builtin_popcount(high)));
            default: abort();
          }
        }
        if (value.type == f32) {
          float v = value.getf32();
          float ret;
          switch (curr->op) {
            case Neg:     ret = -v; break;
            case Abs:     ret = std::abs(v); break;
            case Ceil:    ret = std::ceil(v); break;
            case Floor:   ret = std::floor(v); break;
            case Trunc:   ret = std::trunc(v); break;
            case Nearest: ret = std::nearbyint(v); break;
            case Sqrt:    ret = std::sqrt(v); break;
            default: abort();
          }
          return Literal(fixNaN(v, ret));
        }
        if (value.type == f64) {
          double v = value.getf64();
          double ret;
          switch (curr->op) {
            case Neg:     ret = -v; break;
            case Abs:     ret = std::abs(v); break;
            case Ceil:    ret = std::ceil(v); break;
            case Floor:   ret = std::floor(v); break;
            case Trunc:   ret = std::trunc(v); break;
            case Nearest: ret = std::nearbyint(v); break;
            case Sqrt:    ret = std::sqrt(v); break;
            default: abort();
          }
          return Literal(fixNaN(v, ret));
        }
        abort();
      }
      Flow visitBinary(Binary *curr) override {
        NOTE_ENTER("Binary");
        Flow flow = visit(curr->left);
        if (flow.breaking()) return flow;
        Literal left = flow.value;
        flow = visit(curr->right);
        if (flow.breaking()) return flow;
        Literal right = flow.value;
        NOTE_EVAL2(left, right);
        if (left.type == i32) {
          int32_t l = left.geti32(), r = right.geti32();
          switch (curr->op) {
            case Add:      return Literal(l + r);
            case Sub:      return Literal(l - r);
            case Mul:      return Literal(l * r);
            case DivS: {
              if (r == 0) trap();
              if (l == INT32_MIN && r == -1) trap(); // signed division overflow
              return Literal(l / r);
            }
            case DivU: {
              if (r == 0) trap();
              return Literal(int32_t(uint32_t(l) / uint32_t(r)));
            }
            case RemS: {
              if (r == 0) trap();
              if (l == INT32_MIN && r == -1) return Literal(int32_t(0));
              return Literal(l % r);
            }
            case RemU: {
              if (r == 0) trap();
              return Literal(int32_t(uint32_t(l) % uint32_t(r)));
            }
            case And:      return Literal(l & r);
            case Or:       return Literal(l | r);
            case Xor:      return Literal(l ^ r);
            case Shl: {
              r = r & 31;
              return Literal(l << r);
            }
            case ShrU: {
              r = r & 31;
              return Literal(int32_t(uint32_t(l) >> uint32_t(r)));
            }
            case ShrS: {
              r = r & 31;
              return Literal(l >> r);
            }
            default: abort();
          }
        } else if (left.type == i64) {
          int64_t l = left.geti64(), r = right.geti64();
          switch (curr->op) {
            case Add:      return Literal(l + r);
            case Sub:      return Literal(l - r);
            case Mul:      return Literal(l * r);
            case DivS: {
              if (r == 0) trap();
              if (l == LLONG_MIN && r == -1) trap(); // signed division overflow
              return Literal(l / r);
            }
            case DivU: {
              if (r == 0) trap();
              return Literal(int64_t(uint64_t(l) / uint64_t(r)));
            }
            case RemS: {
              if (r == 0) trap();
              if (l == LLONG_MIN && r == -1) return Literal(int64_t(0));
              return Literal(l % r);
            }
            case RemU: {
              if (r == 0) trap();
              return Literal(int64_t(uint64_t(l) % uint64_t(r)));
            }
            case And:      return Literal(l & r);
            case Or:       return Literal(l | r);
            case Xor:      return Literal(l ^ r);
            case Shl: {
              r = r & 63;
              return Literal(l << r);
            }
            case ShrU: {
              r = r & 63;
              return Literal(int64_t(uint64_t(l) >> uint64_t(r)));
            }
            case ShrS: {
              r = r & 63;
              return Literal(l >> r);
            }
            default: abort();
          }
        } else if (left.type == f32) {
          float l = left.getf32(), r = right.getf32();
          float ret;
          switch (curr->op) {
            case Add:      ret = l + r; break;
            case Sub:      ret = l - r; break;
            case Mul:      ret = l * r; break;
            case Div:      ret = l / r; break;
            case CopySign: {
              ret = std::copysign(l, r);
              return Literal(ret);
            }
            case Min: {
              if (l == r && l == 0) ret = 1/l < 0 ? l : r;
              else ret = std::min(l, r);
              break;
            }
            case Max: {
              if (l == r && l == 0) ret = 1/l < 0 ? r : l;
              else ret = std::max(l, r);
              break;
            }
            default: abort();
          }
          return Literal(fixNaN(l, r, ret));
        } else if (left.type == f64) {
          double l = left.getf64(), r = right.getf64();
          double ret;
          switch (curr->op) {
            case Add:      ret = l + r; break;
            case Sub:      ret = l - r; break;
            case Mul:      ret = l * r; break;
            case Div:      ret = l / r; break;
            case CopySign: {
              ret = std::copysign(l, r);
              return Literal(ret);
            }
            case Min: {
              if (l == r && l == 0) ret = 1/l < 0 ? l : r;
              else ret = std::min(l, r);
              break;
            }
            case Max: {
              if (l == r && l == 0) ret = 1/l < 0 ? r : l;
              else ret = std::max(l, r);
              break;
            }
            default: abort();
          }
          return Literal(fixNaN(l, r, ret));
        }
        abort();
      }
      Flow visitCompare(Compare *curr) override {
        NOTE_ENTER("Compare");
        Flow flow = visit(curr->left);
        if (flow.breaking()) return flow;
        Literal left = flow.value;
        flow = visit(curr->right);
        if (flow.breaking()) return flow;
        Literal right = flow.value;
        NOTE_EVAL2(left, right);
        if (left.type == i32) {
          int32_t l = left.geti32(), r = right.geti32();
          switch (curr->op) {
            case Eq:  return Literal(l == r);
            case Ne:  return Literal(l != r);
            case LtS: return Literal(l < r);
            case LtU: return Literal(uint32_t(l) < uint32_t(r));
            case LeS: return Literal(l <= r);
            case LeU: return Literal(uint32_t(l) <= uint32_t(r));
            case GtS: return Literal(l > r);
            case GtU: return Literal(uint32_t(l) > uint32_t(r));
            case GeS: return Literal(l >= r);
            case GeU: return Literal(uint32_t(l) >= uint32_t(r));
            default: abort();
          }
        } else if (left.type == i64) {
          int64_t l = left.geti64(), r = right.geti64();
          switch (curr->op) {
            case Eq:  return Literal(l == r);
            case Ne:  return Literal(l != r);
            case LtS: return Literal(l < r);
            case LtU: return Literal(uint64_t(l) < uint64_t(r));
            case LeS: return Literal(l <= r);
            case LeU: return Literal(uint64_t(l) <= uint64_t(r));
            case GtS: return Literal(l > r);
            case GtU: return Literal(uint64_t(l) > uint64_t(r));
            case GeS: return Literal(l >= r);
            case GeU: return Literal(uint64_t(l) >= uint64_t(r));
            default: abort();
          }
        } else if (left.type == f32) {
          float l = left.getf32(), r = right.getf32();
          switch (curr->op) {
            case Eq:  return Literal(l == r);
            case Ne:  return Literal(l != r);
            case Lt:  return Literal(l <  r);
            case Le:  return Literal(l <= r);
            case Gt:  return Literal(l >  r);
            case Ge:  return Literal(l >= r);
            default: abort();
          }
        } else if (left.type == f64) {
          double l = left.getf64(), r = right.getf64();
          switch (curr->op) {
            case Eq:  return Literal(l == r);
            case Ne:  return Literal(l != r);
            case Lt:  return Literal(l <  r);
            case Le:  return Literal(l <= r);
            case Gt:  return Literal(l >  r);
            case Ge:  return Literal(l >= r);
            default: abort();
          }
        }
        abort();
      }
      Flow visitConvert(Convert *curr) override {
        NOTE_ENTER("Convert");
        Flow flow = visit(curr->value);
        if (flow.breaking()) return flow;
        Literal value = flow.value;
        switch (curr->op) { // :-)
          case ExtendSInt32:     return Literal(int64_t(value.geti32()));
          case ExtendUInt32:     return Literal(uint64_t((uint32_t)value.geti32()));
          case WrapInt64:        return Literal(int32_t(value.geti64()));
          case TruncSFloat32:
          case TruncSFloat64: {
            double val = curr->op == TruncSFloat32 ? value.getf32() : value.getf64();
            if (isnan(val)) trap();
            if (curr->type == i32) {
              if (val > (double)INT_MAX || val < (double)INT_MIN) trap();
              return Literal(int32_t(val));
            } else {
              int64_t converted = val;
              if ((val >= 1 && converted <= 0) || val < (double)LLONG_MIN) trap();
              return Literal(converted);
            }
          }
          case TruncUFloat32:
          case TruncUFloat64: {
            double val = curr->op == TruncUFloat32 ? value.getf32() : value.getf64();
            if (isnan(val)) trap();
            if (curr->type == i32) {
              if (val > (double)UINT_MAX || val <= (double)-1) trap();
              return Literal(uint32_t(val));
            } else {
              uint64_t converted = val;
              if (converted < val - 1 || val <= (double)-1) trap();
              return Literal(converted);
            }
          }
          case ReinterpretFloat: {
            return curr->type == i32 ? Literal(value.reinterpreti32()) : Literal(value.reinterpreti64());
          }
          case ConvertUInt32:    return curr->type == f32 ? Literal(float(uint32_t(value.geti32()))) : Literal(double(uint32_t(value.geti32())));
          case ConvertSInt32:    return curr->type == f32 ? Literal(float(int32_t(value.geti32()))) : Literal(double(int32_t(value.geti32())));
          case ConvertUInt64:    return curr->type == f32 ? Literal(float((uint64_t)value.geti64())) : Literal(double((uint64_t)value.geti64()));
          case ConvertSInt64:    return curr->type == f32 ? Literal(float(value.geti64())) : Literal(double(value.geti64()));
          case PromoteFloat32:   return Literal(double(value.getf32()));
          case DemoteFloat64:    return Literal(float(value.getf64()));
          case ReinterpretInt: {
            if (curr->type == f32) {
              float v = value.reinterpretf32();
              if (isnan(v)) {
                return Literal(Literal(value.geti32() | 0x7f800000).reinterpretf32());
              }
              return Literal(value.reinterpretf32());
            } else {
              return Literal(value.reinterpretf64());
            }
          }
          default: abort();
        }
      }
      Flow visitSelect(Select *curr) override {
        NOTE_ENTER("Select");
        Flow condition = visit(curr->condition);
        if (condition.breaking()) return condition;
        NOTE_EVAL1(condition.value);
        Flow ifTrue = visit(curr->ifTrue);
        if (ifTrue.breaking()) return ifTrue;
        Flow ifFalse = visit(curr->ifFalse);
        if (ifFalse.breaking()) return ifFalse;
        return condition.value.geti32() ? ifTrue : ifFalse; // ;-)
      }
      Flow visitHost(Host *curr) override {
        NOTE_ENTER("Host");
        switch (curr->op) {
          case PageSize:   return Literal((int32_t)pageSize);
          case MemorySize: return Literal((int32_t)instance.memorySize);
          case GrowMemory: {
            Flow flow = visit(curr->operands[0]);
            if (flow.breaking()) return flow;
            uint32_t delta = flow.value.geti32();
            if (delta % pageSize != 0) trap();
            if (delta > uint32_t(-1) - pageSize) trap();
            if (instance.memorySize >= uint32_t(-1) - delta) trap();
            uint32_t newSize = instance.memorySize + delta;
            if (newSize > instance.wasm.memory.max) trap();
            instance.externalInterface->growMemory(instance.memorySize, newSize);
            instance.memorySize = newSize;
            return Literal();
          }
          case HasFeature: {
            IString id = curr->nameOperand;
            if (id == WASM) return Literal(1);
            return Literal((int32_t)0);
          }
          default: abort();
        }
      }
      Flow visitNop(Nop *curr) override {
        NOTE_ENTER("Nop");
        return Flow();
      }
      Flow visitUnreachable(Unreachable *curr) override {
        NOTE_ENTER("Unreachable");
        trap();
        return Flow();
      }

      float fixNaN(float u, float result) {
        if (!isnan(result)) return result;
        bool unan = isnan(u);
        if (!unan) {
          return Literal((int32_t)0x7fc00000).reinterpretf32();
        }
        return result;
      }

      double fixNaN(double u, double result) {
        if (!isnan(result)) return result;
        bool unan = isnan(u);
        if (!unan) {
          return Literal((int64_t)0x7ff8000000000000LL).reinterpretf64();
        }
        return result;
      }

      float fixNaN(float l, float r, float result) {
        bool lnan = isnan(l), rnan = isnan(r);
        if (!isnan(result) && !lnan && !rnan) return result;
        if (!lnan && !rnan) {
          return Literal((int32_t)0x7fc00000).reinterpretf32();
        }
        return Literal(Literal(lnan ? l : r).reinterpreti32() | 0xc00000).reinterpretf32();
      }

      double fixNaN(double l, double r, double result) {
        bool lnan = isnan(l), rnan = isnan(r);
        if (!isnan(result) && !lnan && !rnan) return result;
        if (!lnan && !rnan) {
          return Literal((int64_t)0x7ff8000000000000LL).reinterpretf64();
        }
        return Literal(int64_t(Literal(lnan ? l : r).reinterpreti64() | 0x8000000000000LL)).reinterpretf64();
      }

      void trap() {
        instance.externalInterface->trap();
      }
    };

    if (callDepth > maxCallDepth) externalInterface->trap();
    callDepth++;

    Function *function = wasm.functionsMap[name];
    assert(function);
    FunctionScope scope(function, arguments);

#ifdef WASM_INTERPRETER_DEBUG
    std::cout << "entering " << function->name << '\n';
#endif

    Literal ret = ExpressionRunner(*this, scope).visit(function->body).value;
    if (function->result == none) ret = Literal();
    assert(function->result == ret.type);
    callDepth--;
#ifdef WASM_INTERPRETER_DEBUG
    std::cout << "exiting " << function->name << '\n';
#endif
    return ret;
  }

  size_t memorySize;

  template<class LS>
  size_t getFinalAddress(LS *curr, Literal ptr) {
    uint64_t addr = ptr.type == i32 ? ptr.geti32() : ptr.geti64();
    if (memorySize < curr->offset) externalInterface->trap();
    if (addr > memorySize - curr->offset) externalInterface->trap();
    addr += curr->offset;
    if (curr->bytes > memorySize) externalInterface->trap();
    if (addr > memorySize - curr->bytes) externalInterface->trap();
    return addr;
  }

  ExternalInterface* externalInterface;
};

} // namespace wasm