walrus 0.8.0

A library for performing WebAssembly transformations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
//! Intermediate representation for expressions.
//!
//! The goal is to match wasm instructions as closely as possible, but translate
//! the stack machine into an expression tree. Additionally all control frames
//! are representd as `Block`s.

pub mod matcher;

use crate::dot::Dot;
use crate::encode::Encoder;
use crate::module::{DisplayExpr, DotExpr};
use crate::{DataId, FunctionId, GlobalId, MemoryId, TableId, TypeId, ValType};
use id_arena::Id;
use std::fmt;
use std::mem;
use walrus_macro::walrus_expr;

/// The id of a local.
pub type LocalId = Id<Local>;

/// A local variable or parameter.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Local {
    id: LocalId,
    ty: ValType,
    /// A human-readable name for this local, often useful when debugging
    pub name: Option<String>,
}

impl Local {
    /// Construct a new local from the given id and type.
    pub fn new(id: LocalId, ty: ValType) -> Local {
        Local { id, ty, name: None }
    }

    /// Get this local's id that is unique across the whole module.
    pub fn id(&self) -> LocalId {
        self.id
    }

    /// Get this local's type.
    pub fn ty(&self) -> ValType {
        self.ty
    }
}

/// An identifier for a particular expression.
pub type ExprId = Id<Expr>;

impl Dot for ExprId {
    fn dot(&self, out: &mut String) {
        out.push_str(&format!("expr_{}", self.index()))
    }
}

/// A trait for anything that is an AST node in our IR.
///
/// Implementations of this trait are generated by `#[walrus_expr]`.
pub trait Ast: Into<Expr> {
    /// The identifier type for this AST node.
    type Id: Into<ExprId>;

    /// Create a new identifier given an `ExprId` that references an `Expr` of
    /// this type.
    fn new_id(id: ExprId) -> Self::Id;
}

/// Different kinds of blocks.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum BlockKind {
    /// A `block` block.
    Block,

    /// A `loop` block.
    Loop,

    /// An `if` or `else` block.
    IfElse,

    /// The entry to a function.
    FunctionEntry,
}

/// An enum of all the different kinds of wasm expressions.
///
/// Note that the `#[walrus_expr]` macro rewrites this enum's variants from
///
/// ```ignore
/// enum Expr {
///     Variant { field: Ty, .. },
///     ...
/// }
/// ```
///
/// into
///
/// ```ignore
/// enum Expr {
///     Variant(Variant),
///     ...
/// }
///
/// struct Variant {
///     field: Ty,
///     ...
/// }
/// ```
#[walrus_expr]
#[derive(Clone, Debug)]
pub enum Expr {
    /// A block of multiple expressions, and also a control frame.
    #[walrus(display_name = display_block_name, dot_name = dot_block_name)]
    Block {
        /// What kind of block is this?
        #[walrus(skip_visit)] // nothing to recurse
        kind: BlockKind,
        /// The types of the expected values on the stack when entering this
        /// block.
        #[walrus(skip_visit)] // nothing to recurse
        params: Box<[ValType]>,
        /// The types of the resulting values added to the stack after this
        /// block is evaluated.
        #[walrus(skip_visit)] // nothing to recurse
        results: Box<[ValType]>,
        /// The expressions that make up the body of this block.
        exprs: Vec<ExprId>,
    },

    /// `call`
    Call {
        /// The function being invoked.
        func: FunctionId,
        /// The arguments to the function.
        args: Box<[ExprId]>,
    },

    /// `call_indirect`
    CallIndirect {
        /// The type signature of the function we're calling
        ty: TypeId,
        /// The table which `func` below is indexing into
        table: TableId,
        /// The index of the function we're invoking
        func: ExprId,
        /// The arguments to the function.
        args: Box<[ExprId]>,
    },

    /// `local.get n`
    LocalGet {
        /// The local being got.
        local: LocalId,
    },

    /// `local.set n`
    LocalSet {
        /// The local being set.
        local: LocalId,
        /// The value to set the local to.
        value: ExprId,
    },

    /// `local.tee n`
    LocalTee {
        /// The local being set.
        local: LocalId,
        /// The value to set the local to and return.
        value: ExprId,
    },

    /// `global.get n`
    GlobalGet {
        /// The global being got.
        global: GlobalId,
    },

    /// `global.set n`
    GlobalSet {
        /// The global being set.
        global: GlobalId,
        /// The value to set the global to.
        value: ExprId,
    },

    /// `*.const`
    Const {
        /// The constant value.
        value: Value,
    },

    /// Binary operations, those requiring two operands
    #[walrus(display_name = display_binop_name, dot_name = dot_binop_name)]
    Binop {
        /// The operation being performed
        #[walrus(skip_visit)]
        op: BinaryOp,
        /// The left-hand operand
        lhs: ExprId,
        /// The right-hand operand
        rhs: ExprId,
    },

    /// Unary operations, those requiring one operand
    #[walrus(display_name = display_unop_name, dot_name = dot_unop_name)]
    Unop {
        /// The operation being performed
        #[walrus(skip_visit)]
        op: UnaryOp,
        /// The input operand
        expr: ExprId,
    },

    /// `select`
    Select {
        /// The condition.
        condition: ExprId,
        /// The value returned when the condition is true. Evaluated regardless
        /// if the condition is true.
        consequent: ExprId,
        /// The value returned when the condition is false. Evaluated regardless
        /// if the condition is false.
        alternative: ExprId,
    },

    /// `unreachable`
    Unreachable {},

    /// `br`
    #[walrus(display_extra = display_br)]
    Br {
        /// The target block to branch to.
        #[walrus(skip_visit)] // should have already been visited
        block: BlockId,
        /// The arguments to the block.
        args: Box<[ExprId]>,
    },

    /// `br_if`
    #[walrus(display_extra = display_br_if)]
    BrIf {
        /// The condition for when to branch.
        condition: ExprId,
        /// The target block to branch to when the condition is met.
        #[walrus(skip_visit)] // should have already been visited
        block: BlockId,
        /// The arguments to the block.
        args: Box<[ExprId]>,
    },

    /// `if ... else ... end`
    IfElse {
        /// The condition.
        condition: ExprId,
        /// The block to execute when the condition is true.
        consequent: BlockId,
        /// The block to execute when the condition is false.
        alternative: BlockId,
    },

    /// `br_table`
    #[walrus(display_extra = display_br_table)]
    BrTable {
        /// The table index of which block to branch to.
        which: ExprId,
        /// The table of target blocks.
        #[walrus(skip_visit)] // should have already been visited
        blocks: Box<[BlockId]>,
        /// The block that is branched to by default when `which` is out of the
        /// table's bounds.
        #[walrus(skip_visit)] // should have already been visited
        default: BlockId,
        /// The arguments to the block.
        args: Box<[ExprId]>,
    },

    /// `drop`
    Drop {
        /// The expression to be evaluated and results ignored.
        expr: ExprId,
    },

    /// `return`
    Return {
        /// The values being returned.
        values: Box<[ExprId]>,
    },

    /// memory.size
    MemorySize {
        /// The memory we're fetching the current size of.
        memory: MemoryId,
    },

    /// memory.grow
    MemoryGrow {
        /// The memory we're growing.
        memory: MemoryId,
        /// The number of pages to grow by.
        pages: ExprId,
    },

    /// memory.init
    MemoryInit {
        /// The memory we're growing.
        memory: MemoryId,
        /// The data to copy in
        data: DataId,
        /// The offset in bytes in memory
        memory_offset: ExprId,
        /// The offset in bytes in the data
        data_offset: ExprId,
        /// The number of bytes to copy
        len: ExprId,
    },

    /// data.drop
    DataDrop {
        /// The data to drop
        data: DataId,
    },

    /// memory.copy
    MemoryCopy {
        /// The source memory
        src: MemoryId,
        /// The destination memory
        dst: MemoryId,
        /// The offset in the destination memory
        dst_offset: ExprId,
        /// The offset in the source memory
        src_offset: ExprId,
        /// The number of bytes to copy
        len: ExprId,
    },

    /// memory.fill
    MemoryFill {
        /// The memory to fill
        memory: MemoryId,
        /// The offset in memory to start filling
        offset: ExprId,
        /// The value to fill
        value: ExprId,
        /// The number of bytes to fill in
        len: ExprId,
    },

    /// Loading a value from memory
    Load {
        /// The memory we're loading from.
        memory: MemoryId,
        /// The kind of memory load this is performing
        #[walrus(skip_visit)]
        kind: LoadKind,
        /// The alignment and offset of this memory load
        #[walrus(skip_visit)]
        arg: MemArg,
        /// The address that we're loading from
        address: ExprId,
    },

    /// Storing a value to memory
    Store {
        /// The memory we're storing to
        memory: MemoryId,
        /// The kind of memory store this is performing
        #[walrus(skip_visit)]
        kind: StoreKind,
        /// The alignment and offset of this memory store
        #[walrus(skip_visit)]
        arg: MemArg,
        /// The address that we're storing to
        address: ExprId,
        /// The value that we're storing
        value: ExprId,
    },

    /// An atomic read/modify/write operation
    AtomicRmw {
        /// The memory we're modifying
        memory: MemoryId,
        /// The atomic operation being performed
        #[walrus(skip_visit)]
        op: AtomicOp,
        /// The atomic operation being performed
        #[walrus(skip_visit)]
        width: AtomicWidth,
        /// The alignment and offset from the base address
        #[walrus(skip_visit)]
        arg: MemArg,
        /// The address at which to perform the operation
        address: ExprId,
        /// The value that's being used to modify what's at the address
        value: ExprId,
    },

    /// An atomic compare exchange operation
    Cmpxchg {
        /// The memory we're modifying
        memory: MemoryId,
        /// The atomic operation being performed
        #[walrus(skip_visit)]
        width: AtomicWidth,
        /// The alignment and offset from the base address
        #[walrus(skip_visit)]
        arg: MemArg,
        /// The address at which to perform the operation
        address: ExprId,
        /// The value that's being used to modify what's at the address
        expected: ExprId,
        /// The value that's being used to modify what's at the address
        replacement: ExprId,
    },

    /// The `atomic.notify` instruction to wake up threads
    AtomicNotify {
        /// The memory we're notifying through
        memory: MemoryId,
        /// The alignment and offset from the base address
        #[walrus(skip_visit)]
        arg: MemArg,
        /// The address at which to perform the operation
        address: ExprId,
        /// The number of threads to wake up
        count: ExprId,
    },

    /// The `*.atomic.wait` instruction to block threads
    AtomicWait {
        /// The memory we're waiting through
        memory: MemoryId,
        /// The alignment and offset from the base address
        #[walrus(skip_visit)]
        arg: MemArg,
        /// The address at which to perform the operation
        address: ExprId,
        /// The expected value at the address above
        expected: ExprId,
        /// The timeout, in nanoseconds, of this wait
        timeout: ExprId,
        /// Whether or not this is an i32 or i64 wait
        #[walrus(skip_visit)]
        sixty_four: bool,
    },

    /// A value followed by one or more stack-neutral, side-effecting
    /// expressions.
    ///
    /// This allows us to express "stacky" and "non-tree-like" expressions such
    /// as:
    ///
    /// ```wasm
    /// ;; Assuming `f` has type `[] -> i32` and potential side effects.
    /// call $f
    /// call $f
    /// call $f
    /// drop
    /// i32.add
    /// ```
    ///
    /// Without `WithSideEffects`, we would need to create a synthetic block
    /// with a temporary local like this:
    ///
    /// ```wasm
    /// (i32.added
    ///   (call $f)
    ///   (block
    ///     (set_local $temp (call $f))
    ///     (drop (call $f))
    ///     (get_local $temp)
    ///   end))
    /// ```
    ///
    /// But using `WithoutSideEffects` we can represent this like so:
    ///
    /// ```wasm
    /// (i32.add
    ///   (with_side_effects
    ///     ;; value
    ///     (call $f)
    ///     ;; side_effects
    ///     (drop (call $f)))
    ///   (call $f))
    /// ```
    WithSideEffects {
        /// The stack-neutral, side-effecting operations before `value`
        before: Vec<ExprId>,
        /// The value.
        value: ExprId,
        /// The stack-neutral, side-effecting operations after `value`
        after: Vec<ExprId>,
    },

    /// table.get
    TableGet {
        /// The table we're fetching from
        table: TableId,
        /// The table index to get
        index: ExprId,
    },

    /// table.set
    TableSet {
        /// The table we're storing to
        table: TableId,
        /// The table index to set
        index: ExprId,
        /// The value we're storing
        value: ExprId,
    },

    /// table.grow
    TableGrow {
        /// The table we're growing
        table: TableId,
        /// The amount we're growing by
        amount: ExprId,
        /// The value to store in all new slots
        value: ExprId,
    },

    /// table.size
    TableSize {
        /// The table we're getting the size of
        table: TableId,
    },

    /// ref.null
    RefNull {},

    /// ref.is_null
    RefIsNull {
        /// The reference that we're testing
        value: ExprId,
    },

    /// `v128.bitselect`
    V128Bitselect {
        /// The bit mask selecting bits from the two operands.
        mask: ExprId,
        /// Operand where bits are selected from if the `mask` bit is 1
        v1: ExprId,
        /// Operand where bits are selected from if the `mask` bit is 0c:w
        v2: ExprId,
    },

    /// `v128.shuffle`
    V128Shuffle {
        /// The indices that are used to create the final vector of this
        /// expression
        #[walrus(skip_visit)]
        indices: ShuffleIndices,
        /// The first 16 bytes to be indxed (with indices 0..15)
        lo: ExprId,
        /// The second 16 bytes to be indxed (with indices 16..31)
        hi: ExprId,
    },
}

/// Argument in `V128Shuffle` of lane indices to select
pub type ShuffleIndices = [u8; 16];

/// Constant values that can show up in WebAssembly
#[derive(Debug, Clone, Copy)]
pub enum Value {
    /// A constant 32-bit integer
    I32(i32),
    /// A constant 64-bit integer
    I64(i64),
    /// A constant 32-bit float
    F32(f32),
    /// A constant 64-bit float
    F64(f64),
    /// A constant 128-bit vector register
    V128(u128),
}

impl Value {
    pub(crate) fn emit(&self, encoder: &mut Encoder) {
        match *self {
            Value::I32(n) => {
                encoder.byte(0x41); // i32.const
                encoder.i32(n);
            }
            Value::I64(n) => {
                encoder.byte(0x42); // i64.const
                encoder.i64(n);
            }
            Value::F32(n) => {
                encoder.byte(0x43); // f32.const
                encoder.f32(n);
            }
            Value::F64(n) => {
                encoder.byte(0x44); // f64.const
                encoder.f64(n);
            }
            Value::V128(n) => {
                encoder.raw(&[0xfd, 0x02]); // v128.const
                for i in 0..16 {
                    encoder.byte((n >> (i * 8)) as u8);
                }
            }
        }
    }
}

impl fmt::Display for Value {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Value::I32(i) => i.fmt(f),
            Value::I64(i) => i.fmt(f),
            Value::F32(i) => i.fmt(f),
            Value::F64(i) => i.fmt(f),
            Value::V128(i) => i.fmt(f),
        }
    }
}

/// Possible binary operations in wasm
#[allow(missing_docs)]
#[derive(Copy, Clone, Debug)]
pub enum BinaryOp {
    I32Eq,
    I32Ne,
    I32LtS,
    I32LtU,
    I32GtS,
    I32GtU,
    I32LeS,
    I32LeU,
    I32GeS,
    I32GeU,

    I64Eq,
    I64Ne,
    I64LtS,
    I64LtU,
    I64GtS,
    I64GtU,
    I64LeS,
    I64LeU,
    I64GeS,
    I64GeU,

    F32Eq,
    F32Ne,
    F32Lt,
    F32Gt,
    F32Le,
    F32Ge,

    F64Eq,
    F64Ne,
    F64Lt,
    F64Gt,
    F64Le,
    F64Ge,

    I32Add,
    I32Sub,
    I32Mul,
    I32DivS,
    I32DivU,
    I32RemS,
    I32RemU,
    I32And,
    I32Or,
    I32Xor,
    I32Shl,
    I32ShrS,
    I32ShrU,
    I32Rotl,
    I32Rotr,

    I64Add,
    I64Sub,
    I64Mul,
    I64DivS,
    I64DivU,
    I64RemS,
    I64RemU,
    I64And,
    I64Or,
    I64Xor,
    I64Shl,
    I64ShrS,
    I64ShrU,
    I64Rotl,
    I64Rotr,

    F32Add,
    F32Sub,
    F32Mul,
    F32Div,
    F32Min,
    F32Max,
    F32Copysign,

    F64Add,
    F64Sub,
    F64Mul,
    F64Div,
    F64Min,
    F64Max,
    F64Copysign,

    I8x16ReplaceLane { idx: u8 },
    I16x8ReplaceLane { idx: u8 },
    I32x4ReplaceLane { idx: u8 },
    I64x2ReplaceLane { idx: u8 },
    F32x4ReplaceLane { idx: u8 },
    F64x2ReplaceLane { idx: u8 },

    I8x16Eq,
    I8x16Ne,
    I8x16LtS,
    I8x16LtU,
    I8x16GtS,
    I8x16GtU,
    I8x16LeS,
    I8x16LeU,
    I8x16GeS,
    I8x16GeU,

    I16x8Eq,
    I16x8Ne,
    I16x8LtS,
    I16x8LtU,
    I16x8GtS,
    I16x8GtU,
    I16x8LeS,
    I16x8LeU,
    I16x8GeS,
    I16x8GeU,

    I32x4Eq,
    I32x4Ne,
    I32x4LtS,
    I32x4LtU,
    I32x4GtS,
    I32x4GtU,
    I32x4LeS,
    I32x4LeU,
    I32x4GeS,
    I32x4GeU,

    F32x4Eq,
    F32x4Ne,
    F32x4Lt,
    F32x4Gt,
    F32x4Le,
    F32x4Ge,

    F64x2Eq,
    F64x2Ne,
    F64x2Lt,
    F64x2Gt,
    F64x2Le,
    F64x2Ge,

    V128And,
    V128Or,
    V128Xor,

    I8x16Shl,
    I8x16ShrS,
    I8x16ShrU,
    I8x16Add,
    I8x16AddSaturateS,
    I8x16AddSaturateU,
    I8x16Sub,
    I8x16SubSaturateS,
    I8x16SubSaturateU,
    I8x16Mul,
    I16x8Shl,
    I16x8ShrS,
    I16x8ShrU,
    I16x8Add,
    I16x8AddSaturateS,
    I16x8AddSaturateU,
    I16x8Sub,
    I16x8SubSaturateS,
    I16x8SubSaturateU,
    I16x8Mul,
    I32x4Shl,
    I32x4ShrS,
    I32x4ShrU,
    I32x4Add,
    I32x4Sub,
    I32x4Mul,
    I64x2Shl,
    I64x2ShrS,
    I64x2ShrU,
    I64x2Add,
    I64x2Sub,

    F32x4Add,
    F32x4Sub,
    F32x4Mul,
    F32x4Div,
    F32x4Min,
    F32x4Max,
    F64x2Add,
    F64x2Sub,
    F64x2Mul,
    F64x2Div,
    F64x2Min,
    F64x2Max,
}

/// Possible unary operations in wasm
#[allow(missing_docs)]
#[derive(Copy, Clone, Debug)]
pub enum UnaryOp {
    I32Eqz,
    I32Clz,
    I32Ctz,
    I32Popcnt,

    I64Eqz,
    I64Clz,
    I64Ctz,
    I64Popcnt,

    F32Abs,
    F32Neg,
    F32Ceil,
    F32Floor,
    F32Trunc,
    F32Nearest,
    F32Sqrt,

    F64Abs,
    F64Neg,
    F64Ceil,
    F64Floor,
    F64Trunc,
    F64Nearest,
    F64Sqrt,

    I32WrapI64,
    I32TruncSF32,
    I32TruncUF32,
    I32TruncSF64,
    I32TruncUF64,
    I64ExtendSI32,
    I64ExtendUI32,
    I64TruncSF32,
    I64TruncUF32,
    I64TruncSF64,
    I64TruncUF64,

    F32ConvertSI32,
    F32ConvertUI32,
    F32ConvertSI64,
    F32ConvertUI64,
    F32DemoteF64,
    F64ConvertSI32,
    F64ConvertUI32,
    F64ConvertSI64,
    F64ConvertUI64,
    F64PromoteF32,

    I32ReinterpretF32,
    I64ReinterpretF64,
    F32ReinterpretI32,
    F64ReinterpretI64,

    I32Extend8S,
    I32Extend16S,
    I64Extend8S,
    I64Extend16S,
    I64Extend32S,

    I8x16Splat,
    I8x16ExtractLaneS { idx: u8 },
    I8x16ExtractLaneU { idx: u8 },
    I16x8Splat,
    I16x8ExtractLaneS { idx: u8 },
    I16x8ExtractLaneU { idx: u8 },
    I32x4Splat,
    I32x4ExtractLane { idx: u8 },
    I64x2Splat,
    I64x2ExtractLane { idx: u8 },
    F32x4Splat,
    F32x4ExtractLane { idx: u8 },
    F64x2Splat,
    F64x2ExtractLane { idx: u8 },

    V128Not,

    I8x16Neg,
    I8x16AnyTrue,
    I8x16AllTrue,
    I16x8Neg,
    I16x8AnyTrue,
    I16x8AllTrue,
    I32x4Neg,
    I32x4AnyTrue,
    I32x4AllTrue,
    I64x2Neg,
    I64x2AnyTrue,
    I64x2AllTrue,

    F32x4Abs,
    F32x4Neg,
    F32x4Sqrt,
    F64x2Abs,
    F64x2Neg,
    F64x2Sqrt,

    I32x4TruncSF32x4Sat,
    I32x4TruncUF32x4Sat,
    I64x2TruncSF64x2Sat,
    I64x2TruncUF64x2Sat,
    F32x4ConvertSI32x4,
    F32x4ConvertUI32x4,
    F64x2ConvertSI64x2,
    F64x2ConvertUI64x2,

    I32TruncSSatF32,
    I32TruncUSatF32,
    I32TruncSSatF64,
    I32TruncUSatF64,
    I64TruncSSatF32,
    I64TruncUSatF32,
    I64TruncSSatF64,
    I64TruncUSatF64,
}

/// The different kinds of load instructions that are part of a `Load` IR node
#[derive(Debug, Copy, Clone)]
#[allow(missing_docs)]
pub enum LoadKind {
    // TODO: much of this is probably redundant with type information already
    // ambiently available, we probably want to trim this down to just "value"
    // and then maybe some sign extensions. We'd then use the type of the node
    // to figure out what kind of store it actually is.
    I32 { atomic: bool },
    I64 { atomic: bool },
    F32,
    F64,
    V128,
    I32_8 { kind: ExtendedLoad },
    I32_16 { kind: ExtendedLoad },
    I64_8 { kind: ExtendedLoad },
    I64_16 { kind: ExtendedLoad },
    I64_32 { kind: ExtendedLoad },
}

/// The kinds of extended loads which can happen
#[derive(Debug, Copy, Clone)]
#[allow(missing_docs)]
pub enum ExtendedLoad {
    SignExtend,
    ZeroExtend,
    ZeroExtendAtomic,
}

impl LoadKind {
    /// Returns the number of bytes loaded
    pub fn width(&self) -> u32 {
        use self::LoadKind::*;
        match self {
            I32_8 { .. } | I64_8 { .. } => 1,
            I32_16 { .. } | I64_16 { .. } => 2,
            I32 { .. } | F32 | I64_32 { .. } => 4,
            I64 { .. } | F64 => 8,
            V128 => 16,
        }
    }

    /// Returns if this is an atomic load
    pub fn atomic(&self) -> bool {
        use self::LoadKind::*;
        match self {
            I32_8 { kind }
            | I32_16 { kind }
            | I64_8 { kind }
            | I64_16 { kind }
            | I64_32 { kind } => kind.atomic(),
            I32 { atomic } | I64 { atomic } => *atomic,
            F32 | F64 | V128 => false,
        }
    }
}

impl ExtendedLoad {
    /// Returns whether this is an atomic extended load
    pub fn atomic(&self) -> bool {
        match self {
            ExtendedLoad::SignExtend | ExtendedLoad::ZeroExtend => false,
            ExtendedLoad::ZeroExtendAtomic => true,
        }
    }
}

/// The different kinds of store instructions that are part of a `Store` IR node
#[derive(Debug, Copy, Clone)]
#[allow(missing_docs)]
pub enum StoreKind {
    I32 { atomic: bool },
    I64 { atomic: bool },
    F32,
    F64,
    V128,
    I32_8 { atomic: bool },
    I32_16 { atomic: bool },
    I64_8 { atomic: bool },
    I64_16 { atomic: bool },
    I64_32 { atomic: bool },
}

impl StoreKind {
    /// Returns the number of bytes stored
    pub fn width(&self) -> u32 {
        use self::StoreKind::*;
        match self {
            I32_8 { .. } | I64_8 { .. } => 1,
            I32_16 { .. } | I64_16 { .. } => 2,
            I32 { .. } | F32 | I64_32 { .. } => 4,
            I64 { .. } | F64 => 8,
            V128 => 16,
        }
    }

    /// Returns whether this is an atomic store
    pub fn atomic(&self) -> bool {
        use self::StoreKind::*;

        match self {
            I32 { atomic }
            | I64 { atomic }
            | I32_8 { atomic }
            | I32_16 { atomic }
            | I64_8 { atomic }
            | I64_16 { atomic }
            | I64_32 { atomic } => *atomic,
            F32 | F64 | V128 => false,
        }
    }
}

/// Arguments to memory operations, containing a constant offset from a dynamic
/// address as well as a predicted alignment.
#[derive(Debug, Copy, Clone)]
pub struct MemArg {
    /// The alignment of the memory operation, must be a power of two
    pub align: u32,
    /// The offset of the memory operation, in bytes from the source address
    pub offset: u32,
}

/// The different kinds of atomic rmw operations
#[derive(Debug, Copy, Clone)]
#[allow(missing_docs)]
pub enum AtomicOp {
    Add,
    Sub,
    And,
    Or,
    Xor,
    Xchg,
}

/// The different kinds of atomic rmw operations
#[derive(Debug, Copy, Clone)]
#[allow(missing_docs)]
pub enum AtomicWidth {
    I32,
    I32_8,
    I32_16,
    I64,
    I64_8,
    I64_16,
    I64_32,
}

impl AtomicWidth {
    /// Returns the size, in bytes, of this atomic operation
    pub fn bytes(&self) -> u32 {
        use self::AtomicWidth::*;
        match self {
            I32_8 | I64_8 => 1,
            I32_16 | I64_16 => 2,
            I32 | I64_32 => 4,
            I64 => 8,
        }
    }
}

impl Expr {
    /// Are any instructions that follow this expression's instruction (within
    /// the current block) unreachable?
    ///
    /// Returns `true` for unconditional branches (`br`, `return`, etc...) and
    /// `unreachable`. Returns `false` for all other "normal" instructions
    /// (`i32.add`, etc...).
    pub fn following_instructions_are_unreachable(&self) -> bool {
        match *self {
            Expr::Unreachable(..) | Expr::Br(..) | Expr::BrTable(..) | Expr::Return(..) => true,

            // No `_` arm to make sure that we properly update this function as
            // we add support for new instructions.
            Expr::Block(..)
            | Expr::Call(..)
            | Expr::LocalGet(..)
            | Expr::LocalSet(..)
            | Expr::LocalTee(..)
            | Expr::GlobalGet(..)
            | Expr::GlobalSet(..)
            | Expr::Const(..)
            | Expr::Binop(..)
            | Expr::Unop(..)
            | Expr::Select(..)
            | Expr::BrIf(..)
            | Expr::IfElse(..)
            | Expr::MemorySize(..)
            | Expr::MemoryGrow(..)
            | Expr::MemoryInit(..)
            | Expr::DataDrop(..)
            | Expr::MemoryCopy(..)
            | Expr::MemoryFill(..)
            | Expr::CallIndirect(..)
            | Expr::Load(..)
            | Expr::Store(..)
            | Expr::AtomicRmw(..)
            | Expr::Cmpxchg(..)
            | Expr::AtomicNotify(..)
            | Expr::AtomicWait(..)
            | Expr::WithSideEffects(..)
            | Expr::TableGet(..)
            | Expr::TableSet(..)
            | Expr::TableGrow(..)
            | Expr::TableSize(..)
            | Expr::RefNull(..)
            | Expr::RefIsNull(..)
            | Expr::V128Bitselect(..)
            | Expr::V128Shuffle(..)
            | Expr::Drop(..) => false,
        }
    }
}

impl Block {
    /// Construct a new block.
    pub fn new(kind: BlockKind, params: Box<[ValType]>, results: Box<[ValType]>) -> Block {
        let exprs = vec![];
        Block {
            kind,
            params,
            results,
            exprs,
        }
    }
}

/// Anything that can be visited by a `Visitor`.
pub trait Visit<'expr> {
    /// Visit this thing with the given visitor.
    fn visit<V>(&self, visitor: &mut V)
    where
        V: Visitor<'expr>;
}

/// Anything that can be visited by a `Visitor`.
pub trait VisitMut {
    /// Visit this thing with the given visitor.
    fn visit_mut<V>(&mut self, visitor: &mut V)
    where
        V: VisitorMut;
}

impl<'expr> Visit<'expr> for ExprId {
    fn visit<V>(&self, visitor: &mut V)
    where
        V: Visitor<'expr>,
    {
        visitor.visit_expr(&visitor.local_function().get(*self))
    }
}

impl VisitMut for ExprId {
    fn visit_mut<V>(&mut self, visitor: &mut V)
    where
        V: VisitorMut,
    {
        // TODO: this is somewhat unfortunate and seems like it's susceptible to
        // being bug-prone when we have a DAG of an IR. We'll need to for sure
        // use graph traversal instead of a simple tree walk like we have today
        // when that comes about.
        let mut expr = mem::replace(
            visitor.local_function_mut().get_mut(*self),
            Expr::Unreachable(Unreachable {}),
        );
        visitor.visit_expr_mut(&mut expr);
        *visitor.local_function_mut().get_mut(*self) = expr;
    }
}

fn display_block_name(block: &Block, out: &mut DisplayExpr) {
    match block.kind {
        BlockKind::Loop => out.f.push_str("loop"),
        _ => out.f.push_str("block"),
    }
}

fn dot_block_name(block: &Block, out: &mut DotExpr<'_, '_>) {
    match block.kind {
        BlockKind::Loop => out.out.push_str("loop"),
        BlockKind::IfElse => out.out.push_str("if_else"),
        BlockKind::FunctionEntry => out.out.push_str("entry"),
        BlockKind::Block => out.out.push_str("block"),
    }
}

fn display_br(e: &Br, out: &mut DisplayExpr) {
    out.f
        .push_str(&format!(" (;e{};)", ExprId::from(e.block).index()))
}

fn display_br_if(e: &BrIf, out: &mut DisplayExpr) {
    out.f
        .push_str(&format!(" (;e{};)", ExprId::from(e.block).index()))
}

fn display_br_table(e: &BrTable, out: &mut DisplayExpr) {
    let blocks = e
        .blocks
        .iter()
        .map(|b| format!("e{}", ExprId::from(*b).index()))
        .collect::<Vec<_>>()
        .join(" ");
    out.f.push_str(&format!(
        " (;default:e{}  [{}];)",
        ExprId::from(e.default).index(),
        blocks
    ))
}

fn display_binop_name(e: &Binop, out: &mut DisplayExpr) {
    out.f.push_str(&format!("{:?}", e.op))
}

fn dot_binop_name(e: &Binop, out: &mut DotExpr<'_, '_>) {
    out.out.push_str(&format!("{:?}", e.op))
}

fn display_unop_name(e: &Unop, out: &mut DisplayExpr) {
    out.f.push_str(&format!("{:?}", e.op))
}

fn dot_unop_name(e: &Unop, out: &mut DotExpr<'_, '_>) {
    out.out.push_str(&format!("{:?}", e.op))
}