blob: 1afc9a8790dcd941e8055b4407f568bf19679665 [file] [log] [blame]
JF Bastien600aee92015-07-31 17:53:38 +00001//===- WebAssemblyInstrControl.td-WebAssembly control-flow ------*- tablegen -*-
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
JF Bastien600aee92015-07-31 17:53:38 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// WebAssembly control-flow code-gen constructs.
JF Bastien600aee92015-07-31 17:53:38 +000011///
12//===----------------------------------------------------------------------===//
13
Dan Gohman950a13c2015-09-16 16:51:30 +000014let isBranch = 1, isTerminator = 1, hasCtrlDep = 1 in {
Dan Gohmanf0b165a2015-12-05 03:03:35 +000015// The condition operand is a boolean value which WebAssembly represents as i32.
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000016defm BR_IF : I<(outs), (ins bb_op:$dst, I32:$cond),
17 (outs), (ins bb_op:$dst),
18 [(brcond I32:$cond, bb:$dst)],
19 "br_if \t$dst, $cond", "br_if \t$dst", 0x0d>;
Dan Gohmanf0b165a2015-12-05 03:03:35 +000020let isCodeGenOnly = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000021defm BR_UNLESS : I<(outs), (ins bb_op:$dst, I32:$cond),
22 (outs), (ins bb_op:$dst), []>;
Heejin Ahn43675872019-02-06 00:17:03 +000023let isBarrier = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000024defm BR : NRI<(outs), (ins bb_op:$dst),
25 [(br bb:$dst)],
26 "br \t$dst", 0x0c>;
Dan Gohman950a13c2015-09-16 16:51:30 +000027} // isBranch = 1, isTerminator = 1, hasCtrlDep = 1
28
Dan Gohmanf0b165a2015-12-05 03:03:35 +000029def : Pat<(brcond (i32 (setne I32:$cond, 0)), bb:$dst),
Dan Gohman06b49582016-02-08 21:50:13 +000030 (BR_IF bb_op:$dst, I32:$cond)>;
Dan Gohmanf0b165a2015-12-05 03:03:35 +000031def : Pat<(brcond (i32 (seteq I32:$cond, 0)), bb:$dst),
Dan Gohman06b49582016-02-08 21:50:13 +000032 (BR_UNLESS bb_op:$dst, I32:$cond)>;
Dan Gohmanf0b165a2015-12-05 03:03:35 +000033
Wouter van Oortmerssend3c544a2018-12-17 22:04:44 +000034// A list of branch targets enclosed in {} and separated by comma.
35// Used by br_table only.
36def BrListAsmOperand : AsmOperandClass { let Name = "BrList"; }
Heejin Ahn43675872019-02-06 00:17:03 +000037let OperandNamespace = "WebAssembly", OperandType = "OPERAND_BRLIST" in
Wouter van Oortmerssend3c544a2018-12-17 22:04:44 +000038def brlist : Operand<i32> {
39 let ParserMatchClass = BrListAsmOperand;
40 let PrintMethod = "printBrList";
41}
42
Dan Gohman950a13c2015-09-16 16:51:30 +000043// TODO: SelectionDAG's lowering insists on using a pointer as the index for
Dan Gohman14026062016-03-08 03:18:12 +000044// jump tables, so in practice we don't ever use BR_TABLE_I64 in wasm32 mode
Dan Gohman950a13c2015-09-16 16:51:30 +000045// currently.
46let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
Heejin Ahn8e2bac82019-01-08 01:15:15 +000047defm BR_TABLE_I32 : I<(outs), (ins I32:$index, variable_ops),
48 (outs), (ins brlist:$brl),
49 [(WebAssemblybr_table I32:$index)],
50 "br_table \t$index", "br_table \t$brl",
51 0x0e>;
52defm BR_TABLE_I64 : I<(outs), (ins I64:$index, variable_ops),
53 (outs), (ins brlist:$brl),
54 [(WebAssemblybr_table I64:$index)],
55 "br_table \t$index", "br_table \t$brl",
56 0x0e>;
Dan Gohman950a13c2015-09-16 16:51:30 +000057} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
58
Sam Clegg16c16822018-05-10 22:16:44 +000059// This is technically a control-flow instruction, since all it affects is the
60// IP.
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000061defm NOP : NRI<(outs), (ins), [], "nop", 0x01>;
Sam Clegg16c16822018-05-10 22:16:44 +000062
Heejin Ahne4a8dee2018-03-02 01:03:40 +000063// Placemarkers to indicate the start or end of a block or loop scope.
Heejin Ahnac62b052017-06-30 00:43:15 +000064// These use/clobber VALUE_STACK to prevent them from being moved into the
65// middle of an expression tree.
Dan Gohmane0405332016-10-03 22:43:53 +000066let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
Wouter van Oortmerssenf2276212018-12-26 22:55:26 +000067defm BLOCK : NRI<(outs), (ins Signature:$sig), [], "block \t$sig", 0x02>;
68defm LOOP : NRI<(outs), (ins Signature:$sig), [], "loop \t$sig", 0x03>;
Dan Gohman4becc582016-10-24 20:32:04 +000069
Wouter van Oortmerssenf2276212018-12-26 22:55:26 +000070defm IF : I<(outs), (ins Signature:$sig, I32:$cond),
71 (outs), (ins Signature:$sig),
72 [], "if \t$sig, $cond", "if \t$sig", 0x04>;
73defm ELSE : NRI<(outs), (ins), [], "else", 0x05>;
74
75// END_BLOCK, END_LOOP, END_IF and END_FUNCTION are represented with the same
76// opcode in wasm.
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000077defm END_BLOCK : NRI<(outs), (ins), [], "end_block", 0x0b>;
78defm END_LOOP : NRI<(outs), (ins), [], "end_loop", 0x0b>;
Wouter van Oortmerssenf2276212018-12-26 22:55:26 +000079defm END_IF : NRI<(outs), (ins), [], "end_if", 0x0b>;
Wouter van Oortmerssen1a91cb02019-02-05 01:19:45 +000080// Generic instruction, for disassembler.
Heejin Ahn43675872019-02-06 00:17:03 +000081let IsCanonical = 1 in
Wouter van Oortmerssen1a91cb02019-02-05 01:19:45 +000082defm END : NRI<(outs), (ins), [], "end", 0x0b>;
Dan Gohmand934cb82017-02-24 23:18:00 +000083let isTerminator = 1, isBarrier = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000084defm END_FUNCTION : NRI<(outs), (ins), [], "end_function", 0x0b>;
Dan Gohmane0405332016-10-03 22:43:53 +000085} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
Dan Gohman950a13c2015-09-16 16:51:30 +000086
Derek Schuff39bf39f2016-08-02 23:16:09 +000087
Derek Schuffffa143c2015-11-10 00:30:57 +000088let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
Dan Gohman9850e872016-10-03 21:33:09 +000089
Derek Schuffffa143c2015-11-10 00:30:57 +000090let isReturn = 1 in {
Derek Schuff39bf39f2016-08-02 23:16:09 +000091
Thomas Lively00f9e5a2019-10-09 21:42:08 +000092defm RETURN : I<(outs), (ins variable_ops), (outs), (ins),
93 [(WebAssemblyreturn)],
94 "return", "return", 0x0f>;
95// Equivalent to RETURN, for use at the end of a function when wasm
96// semantics return by falling off the end of the block.
97let isCodeGenOnly = 1 in
98defm FALLTHROUGH_RETURN : I<(outs), (ins variable_ops), (outs), (ins), []>;
Dan Gohmanb7c24002016-05-21 00:21:56 +000099
Derek Schuffffa143c2015-11-10 00:30:57 +0000100} // isReturn = 1
Dan Gohman9850e872016-10-03 21:33:09 +0000101
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000102defm UNREACHABLE : NRI<(outs), (ins), [(trap)], "unreachable", 0x00>;
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000103} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
Dan Gohman9850e872016-10-03 21:33:09 +0000104
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000105//===----------------------------------------------------------------------===//
106// Exception handling instructions
107//===----------------------------------------------------------------------===//
108
Heejin Ahn47068a42018-07-18 21:42:22 +0000109let Predicates = [HasExceptionHandling] in {
110
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000111// Throwing an exception: throw / rethrow
112let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
Heejin Ahnd6f48782019-01-30 03:21:57 +0000113defm THROW : I<(outs), (ins event_op:$tag, variable_ops),
114 (outs), (ins event_op:$tag),
115 [(WebAssemblythrow (WebAssemblywrapper texternalsym:$tag))],
116 "throw \t$tag", "throw \t$tag", 0x08>;
Heejin Ahn9f96a582019-07-15 22:49:25 +0000117defm RETHROW : I<(outs), (ins EXNREF:$exn), (outs), (ins), [],
118 "rethrow \t$exn", "rethrow", 0x09>;
Heejin Ahn66ce4192019-03-16 05:38:57 +0000119// Pseudo instruction to be the lowering target of int_wasm_rethrow_in_catch
120// intrinsic. Will be converted to the real rethrow instruction later.
121let isPseudo = 1 in
122defm RETHROW_IN_CATCH : NRI<(outs), (ins), [(int_wasm_rethrow_in_catch)],
123 "rethrow_in_catch", 0>;
Derek Schuffffa143c2015-11-10 00:30:57 +0000124} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
Dan Gohmanfb3e0592015-11-25 19:36:19 +0000125
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000126// Region within which an exception is caught: try / end_try
127let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000128defm TRY : NRI<(outs), (ins Signature:$sig), [], "try \t$sig", 0x06>;
129defm END_TRY : NRI<(outs), (ins), [], "end_try", 0x0b>;
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000130} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
131
Heejin Ahnd6f48782019-01-30 03:21:57 +0000132// Catching an exception: catch / extract_exception
133let hasCtrlDep = 1, hasSideEffects = 1 in
Heejin Ahn9f96a582019-07-15 22:49:25 +0000134defm CATCH : I<(outs EXNREF:$dst), (ins), (outs), (ins), [],
Heejin Ahnd6f48782019-01-30 03:21:57 +0000135 "catch \t$dst", "catch", 0x07>;
136
137// Querying / extracing exception: br_on_exn
Heejin Ahn9f96a582019-07-15 22:49:25 +0000138// br_on_exn queries an exnref to see if it matches the corresponding exception
139// tag index. If true it branches to the given label and pushes the
Heejin Ahnd6f48782019-01-30 03:21:57 +0000140// corresponding argument values of the exception onto the stack.
141let isBranch = 1, isTerminator = 1, hasCtrlDep = 1 in
Heejin Ahn9f96a582019-07-15 22:49:25 +0000142defm BR_ON_EXN : I<(outs), (ins bb_op:$dst, event_op:$tag, EXNREF:$exn),
Heejin Ahnd6f48782019-01-30 03:21:57 +0000143 (outs), (ins bb_op:$dst, event_op:$tag), [],
144 "br_on_exn \t$dst, $tag, $exn", "br_on_exn \t$dst, $tag",
145 0x0a>;
146// This is a pseudo instruction that simulates popping a value from stack, which
147// has been pushed by br_on_exn
148let isCodeGenOnly = 1, hasSideEffects = 1 in
149defm EXTRACT_EXCEPTION_I32 : NRI<(outs I32:$dst), (ins),
150 [(set I32:$dst, (int_wasm_extract_exception))],
151 "extract_exception\t$dst">;
Heejin Ahnac62b052017-06-30 00:43:15 +0000152
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000153// Pseudo instructions: cleanupret / catchret
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000154let isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1,
Heejin Ahnd6f48782019-01-30 03:21:57 +0000155 isPseudo = 1, isEHScopeReturn = 1 in {
156 defm CLEANUPRET : NRI<(outs), (ins), [(cleanupret)], "cleanupret", 0>;
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000157 defm CATCHRET : NRI<(outs), (ins bb_op:$dst, bb_op:$from),
Heejin Ahnd6f48782019-01-30 03:21:57 +0000158 [(catchret bb:$dst, bb:$from)], "catchret", 0>;
159} // isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1,
160 // isPseudo = 1, isEHScopeReturn = 1
Heejin Ahn43675872019-02-06 00:17:03 +0000161} // Predicates = [HasExceptionHandling]