JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 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 |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 11 | /// WebAssembly control-flow code-gen constructs. |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Dan Gohman | fb3e059 | 2015-11-25 19:36:19 +0000 | [diff] [blame] | 15 | let Defs = [ARGUMENTS] in { |
| 16 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 17 | let isBranch = 1, isTerminator = 1, hasCtrlDep = 1 in { |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 18 | // The condition operand is a boolean value which WebAssembly represents as i32. |
Dan Gohman | 06b4958 | 2016-02-08 21:50:13 +0000 | [diff] [blame] | 19 | def BR_IF : I<(outs), (ins bb_op:$dst, I32:$cond), |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 20 | [(brcond I32:$cond, bb:$dst)], |
Dan Gohman | c968297 | 2016-10-24 20:21:49 +0000 | [diff] [blame] | 21 | "br_if \t$dst, $cond", 0x0d>; |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 22 | let isCodeGenOnly = 1 in |
Dan Gohman | 5a68ec7 | 2016-10-05 21:24:08 +0000 | [diff] [blame] | 23 | def BR_UNLESS : I<(outs), (ins bb_op:$dst, I32:$cond), []>; |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 24 | let isBarrier = 1 in { |
| 25 | def BR : I<(outs), (ins bb_op:$dst), |
Dan Gohman | af29bd4 | 2015-11-05 20:42:30 +0000 | [diff] [blame] | 26 | [(br bb:$dst)], |
Dan Gohman | c968297 | 2016-10-24 20:21:49 +0000 | [diff] [blame] | 27 | "br \t$dst", 0x0c>; |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 28 | } // isBarrier = 1 |
| 29 | } // isBranch = 1, isTerminator = 1, hasCtrlDep = 1 |
| 30 | |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 31 | } // Defs = [ARGUMENTS] |
| 32 | |
| 33 | def : Pat<(brcond (i32 (setne I32:$cond, 0)), bb:$dst), |
Dan Gohman | 06b4958 | 2016-02-08 21:50:13 +0000 | [diff] [blame] | 34 | (BR_IF bb_op:$dst, I32:$cond)>; |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 35 | def : Pat<(brcond (i32 (seteq I32:$cond, 0)), bb:$dst), |
Dan Gohman | 06b4958 | 2016-02-08 21:50:13 +0000 | [diff] [blame] | 36 | (BR_UNLESS bb_op:$dst, I32:$cond)>; |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 37 | |
| 38 | let Defs = [ARGUMENTS] in { |
| 39 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 40 | // TODO: SelectionDAG's lowering insists on using a pointer as the index for |
Dan Gohman | 1402606 | 2016-03-08 03:18:12 +0000 | [diff] [blame] | 41 | // jump tables, so in practice we don't ever use BR_TABLE_I64 in wasm32 mode |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 42 | // currently. |
Dan Gohman | 85159ca | 2016-01-12 01:45:12 +0000 | [diff] [blame] | 43 | // Set TSFlags{0} to 1 to indicate that the variable_ops are immediates. |
Dan Gohman | 3469ee1 | 2016-01-12 20:30:51 +0000 | [diff] [blame] | 44 | // Set TSFlags{1} to 1 to indicate that the immediates represent labels. |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 45 | let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in { |
Dan Gohman | 1402606 | 2016-03-08 03:18:12 +0000 | [diff] [blame] | 46 | def BR_TABLE_I32 : I<(outs), (ins I32:$index, variable_ops), |
| 47 | [(WebAssemblybr_table I32:$index)], |
Dan Gohman | c968297 | 2016-10-24 20:21:49 +0000 | [diff] [blame] | 48 | "br_table \t$index", 0x0e> { |
Dan Gohman | 85159ca | 2016-01-12 01:45:12 +0000 | [diff] [blame] | 49 | let TSFlags{0} = 1; |
Dan Gohman | 3469ee1 | 2016-01-12 20:30:51 +0000 | [diff] [blame] | 50 | let TSFlags{1} = 1; |
Dan Gohman | 85159ca | 2016-01-12 01:45:12 +0000 | [diff] [blame] | 51 | } |
Dan Gohman | 1402606 | 2016-03-08 03:18:12 +0000 | [diff] [blame] | 52 | def BR_TABLE_I64 : I<(outs), (ins I64:$index, variable_ops), |
| 53 | [(WebAssemblybr_table I64:$index)], |
| 54 | "br_table \t$index"> { |
Dan Gohman | 85159ca | 2016-01-12 01:45:12 +0000 | [diff] [blame] | 55 | let TSFlags{0} = 1; |
Dan Gohman | 3469ee1 | 2016-01-12 20:30:51 +0000 | [diff] [blame] | 56 | let TSFlags{1} = 1; |
Dan Gohman | 85159ca | 2016-01-12 01:45:12 +0000 | [diff] [blame] | 57 | } |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 58 | } // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 |
| 59 | |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 60 | // This is technically a control-flow instruction, since all it affects is the |
| 61 | // IP. |
| 62 | def NOP : I<(outs), (ins), [], "nop", 0x01>; |
| 63 | |
Heejin Ahn | e4a8dee | 2018-03-02 01:03:40 +0000 | [diff] [blame] | 64 | // Placemarkers to indicate the start or end of a block or loop scope. |
Heejin Ahn | ac62b05 | 2017-06-30 00:43:15 +0000 | [diff] [blame] | 65 | // These use/clobber VALUE_STACK to prevent them from being moved into the |
| 66 | // middle of an expression tree. |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 67 | let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in { |
Dan Gohman | c968297 | 2016-10-24 20:21:49 +0000 | [diff] [blame] | 68 | def BLOCK : I<(outs), (ins Signature:$sig), [], "block \t$sig", 0x02>; |
| 69 | def LOOP : I<(outs), (ins Signature:$sig), [], "loop \t$sig", 0x03>; |
Dan Gohman | 4becc58 | 2016-10-24 20:32:04 +0000 | [diff] [blame] | 70 | |
Heejin Ahn | e4a8dee | 2018-03-02 01:03:40 +0000 | [diff] [blame] | 71 | // END_BLOCK, END_LOOP, and END_FUNCTION are represented with the same opcode in |
| 72 | // wasm. |
Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 73 | def END_BLOCK : I<(outs), (ins), [], "end_block", 0x0b>; |
| 74 | def END_LOOP : I<(outs), (ins), [], "end_loop", 0x0b>; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 75 | let isTerminator = 1, isBarrier = 1 in |
| 76 | def END_FUNCTION : I<(outs), (ins), [], "end_function", 0x0b>; |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 77 | } // Uses = [VALUE_STACK], Defs = [VALUE_STACK] |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 78 | |
JF Bastien | 8f9aea0 | 2015-08-01 04:48:44 +0000 | [diff] [blame] | 79 | multiclass RETURN<WebAssemblyRegClass vt> { |
Dan Gohman | af29bd4 | 2015-11-05 20:42:30 +0000 | [diff] [blame] | 80 | def RETURN_#vt : I<(outs), (ins vt:$val), [(WebAssemblyreturn vt:$val)], |
Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 81 | "return \t$val", 0x0f>; |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 82 | // Equivalent to RETURN_#vt, for use at the end of a function when wasm |
| 83 | // semantics return by falling off the end of the block. |
| 84 | let isCodeGenOnly = 1 in |
| 85 | def FALLTHROUGH_RETURN_#vt : I<(outs), (ins vt:$val), []>; |
JF Bastien | 8f9aea0 | 2015-08-01 04:48:44 +0000 | [diff] [blame] | 86 | } |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 87 | |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 88 | multiclass SIMD_RETURN<ValueType vt> { |
| 89 | def RETURN_#vt : SIMD_I<(outs), (ins V128:$val), |
| 90 | [(WebAssemblyreturn (vt V128:$val))], |
Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 91 | "return \t$val", 0x0f>; |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 92 | // Equivalent to RETURN_#vt, for use at the end of a function when wasm |
| 93 | // semantics return by falling off the end of the block. |
| 94 | let isCodeGenOnly = 1 in |
| 95 | def FALLTHROUGH_RETURN_#vt : SIMD_I<(outs), (ins V128:$val), []>; |
| 96 | } |
| 97 | |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 98 | let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in { |
Dan Gohman | 9850e87 | 2016-10-03 21:33:09 +0000 | [diff] [blame] | 99 | |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 100 | let isReturn = 1 in { |
Dan Gohman | d0bf981 | 2015-09-26 01:09:44 +0000 | [diff] [blame] | 101 | defm : RETURN<I32>; |
| 102 | defm : RETURN<I64>; |
| 103 | defm : RETURN<F32>; |
| 104 | defm : RETURN<F64>; |
Heejin Ahn | 0de5872 | 2018-03-08 04:05:37 +0000 | [diff] [blame] | 105 | defm : RETURN<EXCEPT_REF>; |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 106 | defm : SIMD_RETURN<v16i8>; |
| 107 | defm : SIMD_RETURN<v8i16>; |
| 108 | defm : SIMD_RETURN<v4i32>; |
| 109 | defm : SIMD_RETURN<v4f32>; |
| 110 | |
Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 111 | def RETURN_VOID : I<(outs), (ins), [(WebAssemblyreturn)], "return", 0x0f>; |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 112 | |
| 113 | // This is to RETURN_VOID what FALLTHROUGH_RETURN_#vt is to RETURN_#vt. |
| 114 | let isCodeGenOnly = 1 in |
| 115 | def FALLTHROUGH_RETURN_VOID : I<(outs), (ins), []>; |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 116 | } // isReturn = 1 |
Dan Gohman | 9850e87 | 2016-10-03 21:33:09 +0000 | [diff] [blame] | 117 | |
Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 118 | def UNREACHABLE : I<(outs), (ins), [(trap)], "unreachable", 0x00>; |
Heejin Ahn | e4a8dee | 2018-03-02 01:03:40 +0000 | [diff] [blame] | 119 | } // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 |
Dan Gohman | 9850e87 | 2016-10-03 21:33:09 +0000 | [diff] [blame] | 120 | |
Heejin Ahn | e4a8dee | 2018-03-02 01:03:40 +0000 | [diff] [blame] | 121 | //===----------------------------------------------------------------------===// |
| 122 | // Exception handling instructions |
| 123 | //===----------------------------------------------------------------------===// |
| 124 | |
| 125 | // Throwing an exception: throw / rethrow |
| 126 | let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in { |
| 127 | def THROW_I32 : I<(outs), (ins i32imm:$tag, I32:$val), |
| 128 | [(int_wasm_throw imm:$tag, I32:$val)], "throw \t$tag, $val", |
Heejin Ahn | ac62b05 | 2017-06-30 00:43:15 +0000 | [diff] [blame] | 129 | 0x08>; |
Heejin Ahn | e4a8dee | 2018-03-02 01:03:40 +0000 | [diff] [blame] | 130 | def THROW_I64 : I<(outs), (ins i32imm:$tag, I64:$val), |
| 131 | [(int_wasm_throw imm:$tag, I64:$val)], "throw \t$tag, $val", |
Heejin Ahn | ac62b05 | 2017-06-30 00:43:15 +0000 | [diff] [blame] | 132 | 0x08>; |
| 133 | def RETHROW : I<(outs), (ins i32imm:$rel_depth), [], "rethrow \t$rel_depth", |
| 134 | 0x09>; |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 135 | } // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 |
Dan Gohman | fb3e059 | 2015-11-25 19:36:19 +0000 | [diff] [blame] | 136 | |
Heejin Ahn | e4a8dee | 2018-03-02 01:03:40 +0000 | [diff] [blame] | 137 | // Region within which an exception is caught: try / end_try |
| 138 | let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in { |
| 139 | def TRY : I<(outs), (ins Signature:$sig), [], "try \t$sig", 0x06>; |
| 140 | def END_TRY : I<(outs), (ins), [], "end_try", 0x0b>; |
| 141 | } // Uses = [VALUE_STACK], Defs = [VALUE_STACK] |
| 142 | |
Heejin Ahn | 5ef4d5f | 2018-05-31 22:25:54 +0000 | [diff] [blame^] | 143 | // Catching an exception: catch / catch_all |
| 144 | let hasCtrlDep = 1 in { |
| 145 | def CATCH_I32 : I<(outs I32:$dst), (ins i32imm:$tag), |
| 146 | [(set I32:$dst, (int_wasm_catch imm:$tag))], |
| 147 | "i32.catch \t$dst, $tag", 0x07>; |
| 148 | def CATCH_I64 : I<(outs I64:$dst), (ins i32imm:$tag), |
| 149 | [(set I64:$dst, (int_wasm_catch imm:$tag))], |
| 150 | "i64.catch \t$dst, $tag", 0x07>; |
| 151 | def CATCH_ALL : I<(outs), (ins), [], "catch_all", 0x05>; |
| 152 | } |
Heejin Ahn | ac62b05 | 2017-06-30 00:43:15 +0000 | [diff] [blame] | 153 | |
Heejin Ahn | 5ef4d5f | 2018-05-31 22:25:54 +0000 | [diff] [blame^] | 154 | // Pseudo instructions: cleanupret / catchret |
| 155 | // They are not return instructions in wasm, but setting 'isReturn' to true as |
| 156 | // in X86 is necessary for computing funclet membership. |
| 157 | let isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1, |
| 158 | isCodeGenOnly = 1, isReturn = 1 in { |
| 159 | def CLEANUPRET : I<(outs), (ins), [(cleanupret)], "", 0>; |
| 160 | def CATCHRET : I<(outs), (ins bb_op:$dst, bb_op:$from), |
| 161 | [(catchret bb:$dst, bb:$from)], "", 0>; |
| 162 | } |
| 163 | |
| 164 | } // Defs = [ARGUMENTS] |