1// WebAssemblyInstrTable.td - WebAssembly Table codegen support -*- tablegen -*-
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// WebAssembly Table operand code-gen constructs.
11/// Instructions that handle tables
12//===----------------------------------------------------------------------===//
13
14def WebAssemblyTableSet_t : SDTypeProfile<0, 3, [SDTCisPtrTy<1>]>;
15def WebAssemblyTableSet : SDNode<"WebAssemblyISD::TABLE_SET", WebAssemblyTableSet_t,
16                                 [SDNPHasChain, SDNPMayStore, SDNPMemOperand]>;
17
18def WebAssemblyTableGet_t : SDTypeProfile<1, 2, [SDTCisPtrTy<1>]>;
19def WebAssemblyTableGet : SDNode<"WebAssemblyISD::TABLE_GET", WebAssemblyTableGet_t,
20                                 [SDNPHasChain, SDNPMayLoad, SDNPMemOperand]>;
21
22
23multiclass TABLE<WebAssemblyRegClass rc, string suffix> {
24  let mayLoad = 1 in
25  defm TABLE_GET_#rc : I<(outs rc:$res), (ins table32_op:$table, I32:$i),
26                         (outs), (ins table32_op:$table),
27                         [(set rc:$res, (!cast<Intrinsic>("int_wasm_table_get_" # suffix) (WebAssemblyWrapper tglobaladdr:$table), I32:$i))],
28                         "table.get\t$res, $table, $i",
29                         "table.get\t$table",
30                         0x25>;
31
32  let mayStore = 1 in
33  defm TABLE_SET_#rc : I<(outs), (ins table32_op:$table, I32:$i, rc:$val),
34                         (outs), (ins table32_op:$table),
35                         [(!cast<Intrinsic>("int_wasm_table_set_" # suffix) (WebAssemblyWrapper tglobaladdr:$table), I32:$i, rc:$val)],
36                         "table.set\t$table, $i, $val",
37                         "table.set\t$table",
38                         0x26>;
39
40  defm TABLE_GROW_#rc : I<(outs I32:$sz), (ins table32_op:$table, rc:$val, I32:$n),
41                          (outs), (ins table32_op:$table),
42                          [(set I32:$sz, (!cast<Intrinsic>("int_wasm_table_grow_" # suffix) (WebAssemblyWrapper tglobaladdr:$table), rc:$val, I32:$n))],
43                          "table.grow\t$sz, $table, $val, $n",
44                          "table.grow\t$table",
45                          0xfc0f>;
46
47  defm TABLE_FILL_#rc : I<(outs), (ins table32_op:$table, I32:$i, rc:$val, I32:$n),
48                          (outs), (ins table32_op:$table),
49                          [(!cast<Intrinsic>("int_wasm_table_fill_" # suffix) (WebAssemblyWrapper tglobaladdr:$table), I32:$i, rc:$val, I32:$n)],
50                          "table.fill\t$table, $i, $val, $n",
51                          "table.fill\t$table",
52                          0xfc11>;
53
54  foreach vt = rc.RegTypes in {
55    def : Pat<(vt (WebAssemblyTableGet (WebAssemblyWrapper tglobaladdr:$table), i32:$idx)),
56              (!cast<NI>("TABLE_GET_" # rc) tglobaladdr:$table, i32:$idx)>;
57    def : Pat<(WebAssemblyTableSet
58               (WebAssemblyWrapper tglobaladdr:$table),
59               i32:$idx,
60               vt:$src),
61              (!cast<NI>("TABLE_SET_" # rc) tglobaladdr:$table, i32:$idx, vt:$src)>;
62  }
63}
64
65defm "" : TABLE<FUNCREF, "funcref">, Requires<[HasReferenceTypes]>;
66defm "" : TABLE<EXTERNREF, "externref">, Requires<[HasReferenceTypes]>;
67
68def : Pat<(WebAssemblyTableSet mcsym:$table, i32:$idx, funcref:$r),
69          (TABLE_SET_FUNCREF mcsym:$table, i32:$idx, funcref:$r)>,
70          Requires<[HasReferenceTypes]>;
71
72defm TABLE_SIZE : I<(outs I32:$sz), (ins table32_op:$table),
73                    (outs), (ins table32_op:$table),
74                    [(set I32:$sz, (int_wasm_table_size (WebAssemblyWrapper tglobaladdr:$table)))],
75                    "table.size\t$sz, $table",
76                    "table.size\t$table",
77                    0xfc10>,
78                    Requires<[HasReferenceTypes]>;
79
80
81defm TABLE_COPY : I<(outs), (ins table32_op:$table1, table32_op:$table2, I32:$d, I32:$s, I32:$n),
82                    (outs), (ins table32_op:$table1, table32_op:$table2),
83                    [(int_wasm_table_copy (WebAssemblyWrapper tglobaladdr:$table1),
84                                          (WebAssemblyWrapper tglobaladdr:$table2),
85                                          I32:$d, I32:$s, I32:$n)],
86                    "table.copy\t$table1, $table2, $d, $s, $n",
87                    "table.copy\t$table1, $table2",
88                    0xfc0e>,
89                    Requires<[HasReferenceTypes]>;
90