1//===- WebAssemblyInstrControl.td-WebAssembly control-flow ------*- tablegen -*-
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// WebAssembly control-flow code-gen constructs.
12///
13//===----------------------------------------------------------------------===//
14
15let isBranch = 1, isTerminator = 1, hasCtrlDep = 1 in {
16// The condition operand is a boolean value which WebAssembly represents as i32.
17defm BR_IF : I<(outs), (ins bb_op:$dst, I32:$cond),
18               (outs), (ins bb_op:$dst),
19               [(brcond I32:$cond, bb:$dst)],
20                "br_if   \t$dst, $cond", "br_if   \t$dst", 0x0d>;
21let isCodeGenOnly = 1 in
22defm BR_UNLESS : I<(outs), (ins bb_op:$dst, I32:$cond),
23                   (outs), (ins bb_op:$dst), []>;
24let isBarrier = 1 in {
25defm BR   : NRI<(outs), (ins bb_op:$dst),
26                [(br bb:$dst)],
27                "br      \t$dst", 0x0c>;
28} // isBarrier = 1
29} // isBranch = 1, isTerminator = 1, hasCtrlDep = 1
30
31def : Pat<(brcond (i32 (setne I32:$cond, 0)), bb:$dst),
32          (BR_IF bb_op:$dst, I32:$cond)>;
33def : Pat<(brcond (i32 (seteq I32:$cond, 0)), bb:$dst),
34          (BR_UNLESS bb_op:$dst, I32:$cond)>;
35
36// A list of branch targets enclosed in {} and separated by comma.
37// Used by br_table only.
38def BrListAsmOperand : AsmOperandClass { let Name = "BrList"; }
39let OperandNamespace = "WebAssembly" in {
40let OperandType = "OPERAND_BRLIST" in {
41def brlist : Operand<i32> {
42  let ParserMatchClass = BrListAsmOperand;
43  let PrintMethod = "printBrList";
44}
45}  // OPERAND_BRLIST
46}  // OperandNamespace = "WebAssembly"
47
48// TODO: SelectionDAG's lowering insists on using a pointer as the index for
49// jump tables, so in practice we don't ever use BR_TABLE_I64 in wasm32 mode
50// currently.
51// FIXME: this can't inherit from I<> since there is no way to inherit from a
52// multiclass and still have the let statements.
53let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
54let isCodeGenOnly = 1 in
55def BR_TABLE_I32 : NI<(outs), (ins I32:$index, variable_ops),
56                      [(WebAssemblybr_table I32:$index)], "false",
57                      "br_table \t$index", 0x0e> {
58}
59let BaseName = "BR_TABLE_I32" in
60def BR_TABLE_I32_S : NI<(outs), (ins brlist:$brl), [], "true",
61                        "br_table \t$brl", 0x0e> {
62}
63let isCodeGenOnly = 1 in
64def BR_TABLE_I64 : NI<(outs), (ins I64:$index, variable_ops),
65                      [(WebAssemblybr_table I64:$index)], "false",
66                      "br_table \t$index"> {
67}
68let BaseName = "BR_TABLE_I64" in
69def BR_TABLE_I64_S : NI<(outs), (ins brlist:$brl), [], "true",
70                        "br_table \t$brl"> {
71}
72} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
73
74// This is technically a control-flow instruction, since all it affects is the
75// IP.
76defm NOP : NRI<(outs), (ins), [], "nop", 0x01>;
77
78// Placemarkers to indicate the start or end of a block or loop scope.
79// These use/clobber VALUE_STACK to prevent them from being moved into the
80// middle of an expression tree.
81let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
82defm BLOCK : NRI<(outs), (ins Signature:$sig), [], "block   \t$sig", 0x02>;
83defm LOOP  : NRI<(outs), (ins Signature:$sig), [], "loop    \t$sig", 0x03>;
84
85defm IF : I<(outs), (ins Signature:$sig, I32:$cond),
86            (outs), (ins Signature:$sig),
87            [], "if    \t$sig, $cond", "if    \t$sig", 0x04>;
88defm ELSE : NRI<(outs), (ins), [], "else", 0x05>;
89
90// END_BLOCK, END_LOOP, END_IF and END_FUNCTION are represented with the same
91// opcode in wasm.
92defm END_BLOCK : NRI<(outs), (ins), [], "end_block", 0x0b>;
93defm END_LOOP  : NRI<(outs), (ins), [], "end_loop", 0x0b>;
94defm END_IF    : NRI<(outs), (ins), [], "end_if", 0x0b>;
95let isTerminator = 1, isBarrier = 1 in
96defm END_FUNCTION : NRI<(outs), (ins), [], "end_function", 0x0b>;
97} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
98
99multiclass RETURN<WebAssemblyRegClass vt> {
100  defm RETURN_#vt : I<(outs), (ins vt:$val), (outs), (ins),
101                      [(WebAssemblyreturn vt:$val)],
102                      "return  \t$val", "return", 0x0f>;
103  // Equivalent to RETURN_#vt, for use at the end of a function when wasm
104  // semantics return by falling off the end of the block.
105  let isCodeGenOnly = 1 in
106  defm FALLTHROUGH_RETURN_#vt : I<(outs), (ins vt:$val), (outs), (ins), []>;
107}
108
109multiclass SIMD_RETURN<ValueType vt> {
110  defm RETURN_#vt : I<(outs), (ins V128:$val), (outs), (ins),
111                      [(WebAssemblyreturn (vt V128:$val))],
112                      "return  \t$val", "return", 0x0f>,
113                    Requires<[HasSIMD128]>;
114  // Equivalent to RETURN_#vt, for use at the end of a function when wasm
115  // semantics return by falling off the end of the block.
116  let isCodeGenOnly = 1 in
117  defm FALLTHROUGH_RETURN_#vt : I<(outs), (ins V128:$val), (outs), (ins),
118                                  []>,
119                                Requires<[HasSIMD128]>;
120}
121
122let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
123
124let isReturn = 1 in {
125  defm "": RETURN<I32>;
126  defm "": RETURN<I64>;
127  defm "": RETURN<F32>;
128  defm "": RETURN<F64>;
129  defm "": RETURN<EXCEPT_REF>;
130  defm "": SIMD_RETURN<v16i8>;
131  defm "": SIMD_RETURN<v8i16>;
132  defm "": SIMD_RETURN<v4i32>;
133  defm "": SIMD_RETURN<v2i64>;
134  defm "": SIMD_RETURN<v4f32>;
135  defm "": SIMD_RETURN<v2f64>;
136
137  defm RETURN_VOID : NRI<(outs), (ins), [(WebAssemblyreturn)], "return", 0x0f>;
138
139  // This is to RETURN_VOID what FALLTHROUGH_RETURN_#vt is to RETURN_#vt.
140  let isCodeGenOnly = 1 in
141  defm FALLTHROUGH_RETURN_VOID : NRI<(outs), (ins), []>;
142} // isReturn = 1
143
144defm UNREACHABLE : NRI<(outs), (ins), [(trap)], "unreachable", 0x00>;
145} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
146
147//===----------------------------------------------------------------------===//
148// Exception handling instructions
149//===----------------------------------------------------------------------===//
150
151let Predicates = [HasExceptionHandling] in {
152
153// Throwing an exception: throw / rethrow
154let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
155defm THROW_I32 : I<(outs), (ins event_op:$tag, I32:$val),
156                   (outs), (ins event_op:$tag),
157                   [(WebAssemblythrow (WebAssemblywrapper texternalsym:$tag),
158                                      I32:$val)],
159                   "throw   \t$tag, $val", "throw   \t$tag",
160                   0x08>;
161defm THROW_I64 : I<(outs), (ins event_op:$tag, I64:$val),
162                   (outs), (ins event_op:$tag),
163                   [(WebAssemblythrow (WebAssemblywrapper texternalsym:$tag),
164                                      I64:$val)],
165                   "throw   \t$tag, $val", "throw   \t$tag",
166                   0x08>;
167defm RETHROW : NRI<(outs), (ins bb_op:$dst), [], "rethrow \t$dst", 0x09>;
168let isCodeGenOnly = 1 in
169// This is used when the destination for rethrow is the caller function. This
170// will be converted to a rethrow in CFGStackify.
171defm RETHROW_TO_CALLER : NRI<(outs), (ins), [], "rethrow">;
172} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
173
174// Region within which an exception is caught: try / end_try
175let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
176defm TRY     : NRI<(outs), (ins Signature:$sig), [], "try     \t$sig", 0x06>;
177defm END_TRY : NRI<(outs), (ins), [], "end_try", 0x0b>;
178} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
179
180// Catching an exception: catch / catch_all
181let hasCtrlDep = 1, hasSideEffects = 1 in {
182defm CATCH_I32 : I<(outs I32:$dst), (ins i32imm:$tag),
183                   (outs), (ins i32imm:$tag),
184                   [(set I32:$dst, (int_wasm_catch imm:$tag))],
185                   "i32.catch   \t$dst, $tag", "i32.catch   \t$tag", 0x07>;
186defm CATCH_I64 : I<(outs I64:$dst), (ins i32imm:$tag),
187                   (outs), (ins i32imm:$tag),
188                   [(set I64:$dst, (int_wasm_catch imm:$tag))],
189                   "i64.catch   \t$dst, $tag", "i64.catch   \t$tag", 0x07>;
190defm CATCH_ALL : NRI<(outs), (ins), [], "catch_all", 0x05>;
191}
192
193// Pseudo instructions: cleanupret / catchret
194let isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1,
195    isCodeGenOnly = 1, isEHScopeReturn = 1 in {
196  defm CLEANUPRET : NRI<(outs), (ins), [(cleanupret)], "", 0>;
197  defm CATCHRET : NRI<(outs), (ins bb_op:$dst, bb_op:$from),
198                   [(catchret bb:$dst, bb:$from)], "", 0>;
199}
200}
201