2015-08-01 01:53:38 +08:00
|
|
|
//===- WebAssemblyInstrControl.td-WebAssembly control-flow ------*- tablegen -*-
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief WebAssembly control-flow code-gen constructs.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-11-26 03:36:19 +08:00
|
|
|
let Defs = [ARGUMENTS] in {
|
|
|
|
|
2015-09-17 00:51:30 +08:00
|
|
|
let isBranch = 1, isTerminator = 1, hasCtrlDep = 1 in {
|
2015-12-05 11:03:35 +08:00
|
|
|
// The condition operand is a boolean value which WebAssembly represents as i32.
|
2016-02-09 05:50:13 +08:00
|
|
|
def BR_IF : I<(outs), (ins bb_op:$dst, I32:$cond),
|
2015-12-05 11:03:35 +08:00
|
|
|
[(brcond I32:$cond, bb:$dst)],
|
2016-10-25 04:21:49 +08:00
|
|
|
"br_if \t$dst, $cond", 0x0d>;
|
2015-12-05 11:03:35 +08:00
|
|
|
let isCodeGenOnly = 1 in
|
2016-10-06 05:24:08 +08:00
|
|
|
def BR_UNLESS : I<(outs), (ins bb_op:$dst, I32:$cond), []>;
|
2015-09-17 00:51:30 +08:00
|
|
|
let isBarrier = 1 in {
|
|
|
|
def BR : I<(outs), (ins bb_op:$dst),
|
2015-11-06 04:42:30 +08:00
|
|
|
[(br bb:$dst)],
|
2016-10-25 04:21:49 +08:00
|
|
|
"br \t$dst", 0x0c>;
|
2015-09-17 00:51:30 +08:00
|
|
|
} // isBarrier = 1
|
|
|
|
} // isBranch = 1, isTerminator = 1, hasCtrlDep = 1
|
|
|
|
|
2015-12-05 11:03:35 +08:00
|
|
|
} // Defs = [ARGUMENTS]
|
|
|
|
|
|
|
|
def : Pat<(brcond (i32 (setne I32:$cond, 0)), bb:$dst),
|
2016-02-09 05:50:13 +08:00
|
|
|
(BR_IF bb_op:$dst, I32:$cond)>;
|
2015-12-05 11:03:35 +08:00
|
|
|
def : Pat<(brcond (i32 (seteq I32:$cond, 0)), bb:$dst),
|
2016-02-09 05:50:13 +08:00
|
|
|
(BR_UNLESS bb_op:$dst, I32:$cond)>;
|
2015-12-05 11:03:35 +08:00
|
|
|
|
|
|
|
let Defs = [ARGUMENTS] in {
|
|
|
|
|
2015-09-17 00:51:30 +08:00
|
|
|
// TODO: SelectionDAG's lowering insists on using a pointer as the index for
|
2016-03-08 11:18:12 +08:00
|
|
|
// jump tables, so in practice we don't ever use BR_TABLE_I64 in wasm32 mode
|
2015-09-17 00:51:30 +08:00
|
|
|
// currently.
|
2016-01-12 09:45:12 +08:00
|
|
|
// Set TSFlags{0} to 1 to indicate that the variable_ops are immediates.
|
2016-01-13 04:30:51 +08:00
|
|
|
// Set TSFlags{1} to 1 to indicate that the immediates represent labels.
|
2015-09-17 00:51:30 +08:00
|
|
|
let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
|
2016-03-08 11:18:12 +08:00
|
|
|
def BR_TABLE_I32 : I<(outs), (ins I32:$index, variable_ops),
|
|
|
|
[(WebAssemblybr_table I32:$index)],
|
2016-10-25 04:21:49 +08:00
|
|
|
"br_table \t$index", 0x0e> {
|
2016-01-12 09:45:12 +08:00
|
|
|
let TSFlags{0} = 1;
|
2016-01-13 04:30:51 +08:00
|
|
|
let TSFlags{1} = 1;
|
2016-01-12 09:45:12 +08:00
|
|
|
}
|
2016-03-08 11:18:12 +08:00
|
|
|
def BR_TABLE_I64 : I<(outs), (ins I64:$index, variable_ops),
|
|
|
|
[(WebAssemblybr_table I64:$index)],
|
|
|
|
"br_table \t$index"> {
|
2016-01-12 09:45:12 +08:00
|
|
|
let TSFlags{0} = 1;
|
2016-01-13 04:30:51 +08:00
|
|
|
let TSFlags{1} = 1;
|
2016-01-12 09:45:12 +08:00
|
|
|
}
|
2015-09-17 00:51:30 +08:00
|
|
|
} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
|
|
|
|
|
2018-03-02 09:03:40 +08:00
|
|
|
// Placemarkers to indicate the start or end of a block or loop scope.
|
2017-06-30 08:43:15 +08:00
|
|
|
// These use/clobber VALUE_STACK to prevent them from being moved into the
|
|
|
|
// middle of an expression tree.
|
2016-10-04 06:43:53 +08:00
|
|
|
let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
|
2016-10-25 04:21:49 +08:00
|
|
|
def BLOCK : I<(outs), (ins Signature:$sig), [], "block \t$sig", 0x02>;
|
|
|
|
def LOOP : I<(outs), (ins Signature:$sig), [], "loop \t$sig", 0x03>;
|
2016-10-25 04:32:04 +08:00
|
|
|
|
2018-03-02 09:03:40 +08:00
|
|
|
// END_BLOCK, END_LOOP, and END_FUNCTION are represented with the same opcode in
|
|
|
|
// wasm.
|
2016-10-25 07:27:49 +08:00
|
|
|
def END_BLOCK : I<(outs), (ins), [], "end_block", 0x0b>;
|
|
|
|
def END_LOOP : I<(outs), (ins), [], "end_loop", 0x0b>;
|
2017-02-25 07:18:00 +08:00
|
|
|
let isTerminator = 1, isBarrier = 1 in
|
|
|
|
def END_FUNCTION : I<(outs), (ins), [], "end_function", 0x0b>;
|
2016-10-04 06:43:53 +08:00
|
|
|
} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
|
2015-09-17 00:51:30 +08:00
|
|
|
|
2015-08-01 12:48:44 +08:00
|
|
|
multiclass RETURN<WebAssemblyRegClass vt> {
|
2015-11-06 04:42:30 +08:00
|
|
|
def RETURN_#vt : I<(outs), (ins vt:$val), [(WebAssemblyreturn vt:$val)],
|
2016-10-25 07:27:49 +08:00
|
|
|
"return \t$val", 0x0f>;
|
2016-05-21 08:21:56 +08:00
|
|
|
// Equivalent to RETURN_#vt, for use at the end of a function when wasm
|
|
|
|
// semantics return by falling off the end of the block.
|
|
|
|
let isCodeGenOnly = 1 in
|
|
|
|
def FALLTHROUGH_RETURN_#vt : I<(outs), (ins vt:$val), []>;
|
2015-08-01 12:48:44 +08:00
|
|
|
}
|
2015-11-10 08:30:57 +08:00
|
|
|
|
2016-08-03 07:16:09 +08:00
|
|
|
multiclass SIMD_RETURN<ValueType vt> {
|
|
|
|
def RETURN_#vt : SIMD_I<(outs), (ins V128:$val),
|
|
|
|
[(WebAssemblyreturn (vt V128:$val))],
|
2016-10-25 07:27:49 +08:00
|
|
|
"return \t$val", 0x0f>;
|
2016-08-03 07:16:09 +08:00
|
|
|
// Equivalent to RETURN_#vt, for use at the end of a function when wasm
|
|
|
|
// semantics return by falling off the end of the block.
|
|
|
|
let isCodeGenOnly = 1 in
|
|
|
|
def FALLTHROUGH_RETURN_#vt : SIMD_I<(outs), (ins V128:$val), []>;
|
|
|
|
}
|
|
|
|
|
2015-11-10 08:30:57 +08:00
|
|
|
let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
|
2016-10-04 05:33:09 +08:00
|
|
|
|
2015-11-10 08:30:57 +08:00
|
|
|
let isReturn = 1 in {
|
2015-09-26 09:09:44 +08:00
|
|
|
defm : RETURN<I32>;
|
|
|
|
defm : RETURN<I64>;
|
|
|
|
defm : RETURN<F32>;
|
|
|
|
defm : RETURN<F64>;
|
2018-03-08 12:05:37 +08:00
|
|
|
defm : RETURN<EXCEPT_REF>;
|
2016-08-03 07:16:09 +08:00
|
|
|
defm : SIMD_RETURN<v16i8>;
|
|
|
|
defm : SIMD_RETURN<v8i16>;
|
|
|
|
defm : SIMD_RETURN<v4i32>;
|
|
|
|
defm : SIMD_RETURN<v4f32>;
|
|
|
|
|
2016-10-25 07:27:49 +08:00
|
|
|
def RETURN_VOID : I<(outs), (ins), [(WebAssemblyreturn)], "return", 0x0f>;
|
2016-05-21 08:21:56 +08:00
|
|
|
|
|
|
|
// This is to RETURN_VOID what FALLTHROUGH_RETURN_#vt is to RETURN_#vt.
|
|
|
|
let isCodeGenOnly = 1 in
|
|
|
|
def FALLTHROUGH_RETURN_VOID : I<(outs), (ins), []>;
|
2015-11-10 08:30:57 +08:00
|
|
|
} // isReturn = 1
|
2016-10-04 05:33:09 +08:00
|
|
|
|
2016-10-25 07:27:49 +08:00
|
|
|
def UNREACHABLE : I<(outs), (ins), [(trap)], "unreachable", 0x00>;
|
2018-03-02 09:03:40 +08:00
|
|
|
} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
|
2016-10-04 05:33:09 +08:00
|
|
|
|
2018-03-02 09:03:40 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Exception handling instructions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Throwing an exception: throw / rethrow
|
|
|
|
let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
|
|
|
|
def THROW_I32 : I<(outs), (ins i32imm:$tag, I32:$val),
|
|
|
|
[(int_wasm_throw imm:$tag, I32:$val)], "throw \t$tag, $val",
|
2017-06-30 08:43:15 +08:00
|
|
|
0x08>;
|
2018-03-02 09:03:40 +08:00
|
|
|
def THROW_I64 : I<(outs), (ins i32imm:$tag, I64:$val),
|
|
|
|
[(int_wasm_throw imm:$tag, I64:$val)], "throw \t$tag, $val",
|
2017-06-30 08:43:15 +08:00
|
|
|
0x08>;
|
|
|
|
def RETHROW : I<(outs), (ins i32imm:$rel_depth), [], "rethrow \t$rel_depth",
|
|
|
|
0x09>;
|
2015-11-10 08:30:57 +08:00
|
|
|
} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
|
2015-11-26 03:36:19 +08:00
|
|
|
|
2018-03-02 09:03:40 +08:00
|
|
|
// Region within which an exception is caught: try / end_try
|
|
|
|
let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
|
|
|
|
def TRY : I<(outs), (ins Signature:$sig), [], "try \t$sig", 0x06>;
|
|
|
|
def END_TRY : I<(outs), (ins), [], "end_try", 0x0b>;
|
|
|
|
} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
|
|
|
|
|
2015-11-26 03:36:19 +08:00
|
|
|
} // Defs = [ARGUMENTS]
|
2017-06-30 08:43:15 +08:00
|
|
|
|
|
|
|
// rethrow takes a relative depth as an argument, for which currently only 0 is
|
|
|
|
// possible for C++. Once other languages need depths other than 0, depths will
|
|
|
|
// be computed in CFGStackify.
|
|
|
|
def : Pat<(int_wasm_rethrow), (RETHROW 0)>;
|