1// WebAssemblyInstrInfo.td-Describe the WebAssembly Instructions-*- 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/// \brief WebAssembly Instruction definitions.
12///
13//===----------------------------------------------------------------------===//
14
15//===----------------------------------------------------------------------===//
16// WebAssembly Instruction Predicate Definitions.
17//===----------------------------------------------------------------------===//
18
19def HasAddr32 : Predicate<"!Subtarget->hasAddr64()">;
20def HasAddr64 : Predicate<"Subtarget->hasAddr64()">;
21def HasSIMD128 : Predicate<"Subtarget->hasSIMD128()">,
22                           AssemblerPredicate<"FeatureSIMD128", "simd128">;
23
24//===----------------------------------------------------------------------===//
25// WebAssembly-specific DAG Node Types.
26//===----------------------------------------------------------------------===//
27
28def SDT_WebAssemblyCallSeqStart : SDCallSeqStart<[SDTCisVT<0, iPTR>]>;
29def SDT_WebAssemblyCallSeqEnd :
30    SDCallSeqEnd<[SDTCisVT<0, iPTR>, SDTCisVT<1, iPTR>]>;
31def SDT_WebAssemblyCall0    : SDTypeProfile<0, -1, [SDTCisPtrTy<0>]>;
32def SDT_WebAssemblyCall1    : SDTypeProfile<1, -1, [SDTCisPtrTy<1>]>;
33def SDT_WebAssemblyTableswitch : SDTypeProfile<0, -1, [SDTCisPtrTy<0>]>;
34def SDT_WebAssemblyArgument : SDTypeProfile<1, 1, [SDTCisVT<1, i32>]>;
35def SDT_WebAssemblyReturn   : SDTypeProfile<0, -1, []>;
36def SDT_WebAssemblyWrapper  : SDTypeProfile<1, 1, [SDTCisSameAs<0, 1>,
37                                                   SDTCisPtrTy<0>]>;
38
39//===----------------------------------------------------------------------===//
40// WebAssembly-specific DAG Nodes.
41//===----------------------------------------------------------------------===//
42
43def WebAssemblycallseq_start :
44    SDNode<"ISD::CALLSEQ_START", SDT_WebAssemblyCallSeqStart,
45           [SDNPHasChain, SDNPOutGlue]>;
46def WebAssemblycallseq_end :
47    SDNode<"ISD::CALLSEQ_END", SDT_WebAssemblyCallSeqEnd,
48           [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
49def WebAssemblycall0 : SDNode<"WebAssemblyISD::CALL0",
50                              SDT_WebAssemblyCall0,
51                              [SDNPHasChain, SDNPVariadic]>;
52def WebAssemblycall1 : SDNode<"WebAssemblyISD::CALL1",
53                              SDT_WebAssemblyCall1,
54                              [SDNPHasChain, SDNPVariadic]>;
55def WebAssemblytableswitch : SDNode<"WebAssemblyISD::TABLESWITCH",
56                                    SDT_WebAssemblyTableswitch,
57                                    [SDNPHasChain, SDNPVariadic]>;
58def WebAssemblyargument : SDNode<"WebAssemblyISD::ARGUMENT",
59                                 SDT_WebAssemblyArgument>;
60def WebAssemblyreturn   : SDNode<"WebAssemblyISD::RETURN",
61                                 SDT_WebAssemblyReturn, [SDNPHasChain]>;
62def WebAssemblywrapper  : SDNode<"WebAssemblyISD::Wrapper",
63                                 SDT_WebAssemblyWrapper>;
64
65//===----------------------------------------------------------------------===//
66// WebAssembly-specific Operands.
67//===----------------------------------------------------------------------===//
68
69let OperandNamespace = "WebAssembly" in {
70
71let OperandType = "OPERAND_BASIC_BLOCK" in
72def bb_op : Operand<OtherVT>;
73
74let OperandType = "OPERAND_FPIMM" in {
75def f32imm_op : Operand<f32>;
76def f64imm_op : Operand<f64>;
77} // OperandType = "OPERAND_FPIMM"
78
79let OperandType = "OPERAND_P2ALIGN" in {
80def P2Align : Operand<i32> {
81  let PrintMethod = "printWebAssemblyP2AlignOperand";
82}
83} // OperandType = "OPERAND_P2ALIGN"
84
85} // OperandNamespace = "WebAssembly"
86
87//===----------------------------------------------------------------------===//
88// WebAssembly Instruction Format Definitions.
89//===----------------------------------------------------------------------===//
90
91include "WebAssemblyInstrFormats.td"
92
93//===----------------------------------------------------------------------===//
94// Additional instructions.
95//===----------------------------------------------------------------------===//
96
97multiclass ARGUMENT<WebAssemblyRegClass vt> {
98  let hasSideEffects = 1, Uses = [ARGUMENTS], isCodeGenOnly = 1 in
99  def ARGUMENT_#vt : I<(outs vt:$res), (ins i32imm:$argno),
100                       [(set vt:$res, (WebAssemblyargument timm:$argno))]>;
101}
102defm : ARGUMENT<I32>;
103defm : ARGUMENT<I64>;
104defm : ARGUMENT<F32>;
105defm : ARGUMENT<F64>;
106
107let Defs = [ARGUMENTS] in {
108
109// get_local and set_local are not generated by instruction selection; they
110// are implied by virtual register uses and defs in most contexts. However,
111// they are explicitly emitted for special purposes.
112multiclass LOCAL<WebAssemblyRegClass vt> {
113  def GET_LOCAL_#vt : I<(outs vt:$res), (ins i32imm:$regno), [],
114                        "get_local\t$res, $regno">;
115  // TODO: set_local returns its operand value
116  def SET_LOCAL_#vt : I<(outs), (ins i32imm:$regno, vt:$src), [],
117                        "set_local\t$regno, $src">;
118
119  // COPY_LOCAL is not an actual instruction in wasm, but since we allow
120  // get_local and set_local to be implicit, we can have a COPY_LOCAL which
121  // is actually a no-op because all the work is done in the implied
122  // get_local and set_local.
123  let isAsCheapAsAMove = 1 in
124  def COPY_LOCAL_#vt : I<(outs vt:$res), (ins vt:$src), [],
125                         "copy_local\t$res, $src">;
126}
127defm : LOCAL<I32>;
128defm : LOCAL<I64>;
129defm : LOCAL<F32>;
130defm : LOCAL<F64>;
131
132let isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1 in {
133def CONST_I32 : I<(outs I32:$res), (ins i32imm:$imm),
134                  [(set I32:$res, imm:$imm)],
135                  "i32.const\t$res, $imm">;
136def CONST_I64 : I<(outs I64:$res), (ins i64imm:$imm),
137                  [(set I64:$res, imm:$imm)],
138                  "i64.const\t$res, $imm">;
139def CONST_F32 : I<(outs F32:$res), (ins f32imm_op:$imm),
140                  [(set F32:$res, fpimm:$imm)],
141                  "f32.const\t$res, $imm">;
142def CONST_F64 : I<(outs F64:$res), (ins f64imm_op:$imm),
143                  [(set F64:$res, fpimm:$imm)],
144                  "f64.const\t$res, $imm">;
145} // isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1
146
147} // Defs = [ARGUMENTS]
148
149def : Pat<(i32 (WebAssemblywrapper tglobaladdr:$addr)),
150          (CONST_I32 tglobaladdr:$addr)>;
151def : Pat<(i32 (WebAssemblywrapper texternalsym:$addr)),
152          (CONST_I32 texternalsym:$addr)>;
153
154//===----------------------------------------------------------------------===//
155// Additional sets of instructions.
156//===----------------------------------------------------------------------===//
157
158include "WebAssemblyInstrMemory.td"
159include "WebAssemblyInstrCall.td"
160include "WebAssemblyInstrControl.td"
161include "WebAssemblyInstrInteger.td"
162include "WebAssemblyInstrConv.td"
163include "WebAssemblyInstrFloat.td"
164include "WebAssemblyInstrAtomics.td"
165include "WebAssemblyInstrSIMD.td"
166