15ca0bacaSJF Bastien//===- WebAssemblyInstrCall.td-WebAssembly Call codegen support -*- tablegen -*-
25ca0bacaSJF Bastien//
35ca0bacaSJF Bastien//                     The LLVM Compiler Infrastructure
45ca0bacaSJF Bastien//
55ca0bacaSJF Bastien// This file is distributed under the University of Illinois Open Source
65ca0bacaSJF Bastien// License. See LICENSE.TXT for details.
75ca0bacaSJF Bastien//
85ca0bacaSJF Bastien//===----------------------------------------------------------------------===//
95ca0bacaSJF Bastien///
105ca0bacaSJF Bastien/// \file
115ca0bacaSJF Bastien/// \brief WebAssembly Call operand code-gen constructs.
125ca0bacaSJF Bastien///
135ca0bacaSJF Bastien//===----------------------------------------------------------------------===//
145ca0bacaSJF Bastien
15e2a7a827SDan Gohman// TODO: addr64: These currently assume the callee address is 32-bit.
16e2a7a827SDan Gohman
17fb3e0594SDan Gohmanlet Defs = [ARGUMENTS] in {
18fb3e0594SDan Gohman
19905bef5cSDan Gohman// Call sequence markers. These have an immediate which represents the amount of
20905bef5cSDan Gohman// stack space to allocate or free, which is used for varargs lowering.
215a143063SDerek Schufflet Uses = [SP32, SP64], Defs = [SP32, SP64], isCodeGenOnly = 1 in {
22e2a7a827SDan Gohmandef ADJCALLSTACKDOWN : I<(outs), (ins i32imm:$amt),
23af111db8SJF Bastien                         [(WebAssemblycallseq_start timm:$amt)]>;
248bb5f292SDerek Schuffdef ADJCALLSTACKUP : I<(outs), (ins i32imm:$amt, i32imm:$amt2),
258bb5f292SDerek Schuff                       [(WebAssemblycallseq_end timm:$amt, timm:$amt2)]>;
26af111db8SJF Bastien} // isCodeGenOnly = 1
27af111db8SJF Bastien
28c7c04454SDan Gohmanmulticlass CALL<WebAssemblyRegClass vt, string prefix> {
29e2a7a827SDan Gohman  def CALL_#vt : I<(outs vt:$dst), (ins i32imm:$callee, variable_ops),
30e2a7a827SDan Gohman                   [(set vt:$dst, (WebAssemblycall1 (i32 imm:$callee)))],
313acb187dSDan Gohman                   !strconcat(prefix, "call\t$dst, $callee"),
323acb187dSDan Gohman                   0x10>;
336f69783fSDerek Schuff  let isCodeGenOnly = 1 in {
346f69783fSDerek Schuff    def PCALL_INDIRECT_#vt : I<(outs vt:$dst), (ins I32:$callee, variable_ops),
35cf4748f1SDan Gohman                              [(set vt:$dst, (WebAssemblycall1 I32:$callee))],
366f69783fSDerek Schuff                              "PSEUDO CALL INDIRECT\t$callee">;
376f69783fSDerek Schuff  } // isCodeGenOnly = 1
38*f50d964bSDan Gohman
39*f50d964bSDan Gohman  def CALL_INDIRECT_#vt : I<(outs vt:$dst), (ins i32imm:$flags, variable_ops),
406f69783fSDerek Schuff                            [],
413acb187dSDan Gohman                            !strconcat(prefix, "call_indirect\t$dst"),
423acb187dSDan Gohman                            0x11>;
43af111db8SJF Bastien}
4439bf39f3SDerek Schuff
4539bf39f3SDerek Schuffmulticlass SIMD_CALL<ValueType vt, string prefix> {
4639bf39f3SDerek Schuff  def CALL_#vt : SIMD_I<(outs V128:$dst), (ins i32imm:$callee, variable_ops),
4739bf39f3SDerek Schuff                         [(set (vt V128:$dst),
4839bf39f3SDerek Schuff                               (WebAssemblycall1 (i32 imm:$callee)))],
493acb187dSDan Gohman                         !strconcat(prefix, "call\t$dst, $callee"),
503acb187dSDan Gohman                         0x10>;
516f69783fSDerek Schuff  let isCodeGenOnly = 1 in {
526f69783fSDerek Schuff    def PCALL_INDIRECT_#vt : SIMD_I<(outs V128:$dst),
5339bf39f3SDerek Schuff                                    (ins I32:$callee, variable_ops),
5439bf39f3SDerek Schuff                                    [(set (vt V128:$dst),
5539bf39f3SDerek Schuff                                          (WebAssemblycall1 I32:$callee))],
566f69783fSDerek Schuff                                    "PSEUDO CALL INDIRECT\t$callee">;
576f69783fSDerek Schuff  } // isCodeGenOnly = 1
58*f50d964bSDan Gohman
596f69783fSDerek Schuff  def CALL_INDIRECT_#vt : SIMD_I<(outs V128:$dst),
60*f50d964bSDan Gohman                                  (ins i32imm:$flags, variable_ops),
616f69783fSDerek Schuff                                  [],
623acb187dSDan Gohman                                  !strconcat(prefix, "call_indirect\t$dst"),
633acb187dSDan Gohman                                  0x11>;
6439bf39f3SDerek Schuff}
6539bf39f3SDerek Schuff
66af111db8SJF Bastienlet Uses = [SP32, SP64], isCall = 1 in {
67c7c04454SDan Gohman  defm : CALL<I32, "i32.">;
68c7c04454SDan Gohman  defm : CALL<I64, "i64.">;
69c7c04454SDan Gohman  defm : CALL<F32, "f32.">;
70c7c04454SDan Gohman  defm : CALL<F64, "f64.">;
7139bf39f3SDerek Schuff  defm : SIMD_CALL<v16i8, "i8x16.">;
7239bf39f3SDerek Schuff  defm : SIMD_CALL<v8i16, "i16x8.">;
7339bf39f3SDerek Schuff  defm : SIMD_CALL<v4i32, "i32x4.">;
7439bf39f3SDerek Schuff  defm : SIMD_CALL<v4f32, "f32x4.">;
75f71abef7SDan Gohman
76e2a7a827SDan Gohman  def CALL_VOID : I<(outs), (ins i32imm:$callee, variable_ops),
77e2a7a827SDan Gohman                    [(WebAssemblycall0 (i32 imm:$callee))],
783acb187dSDan Gohman                    "call    \t$callee", 0x10>;
796f69783fSDerek Schuff  let isCodeGenOnly = 1 in {
806f69783fSDerek Schuff    def PCALL_INDIRECT_VOID : I<(outs), (ins I32:$callee, variable_ops),
81cf4748f1SDan Gohman                      [(WebAssemblycall0 I32:$callee)],
826f69783fSDerek Schuff                      "PSEUDO CALL INDIRECT\t$callee">;
836f69783fSDerek Schuff  } // isCodeGenOnly = 1
84*f50d964bSDan Gohman
85*f50d964bSDan Gohman  def CALL_INDIRECT_VOID : I<(outs), (ins i32imm:$flags, variable_ops),
866f69783fSDerek Schuff                             [],
873acb187dSDan Gohman                             "call_indirect\t", 0x11>;
88af111db8SJF Bastien} // Uses = [SP32,SP64], isCall = 1
89fb3e0594SDan Gohman
90fb3e0594SDan Gohman} // Defs = [ARGUMENTS]
91e2a7a827SDan Gohman
92e2a7a827SDan Gohman// Patterns for matching a direct call to a global address.
93e2a7a827SDan Gohmandef : Pat<(i32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
94e2a7a827SDan Gohman          (CALL_I32 tglobaladdr:$callee)>;
95e2a7a827SDan Gohmandef : Pat<(i64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
96e2a7a827SDan Gohman          (CALL_I64 tglobaladdr:$callee)>;
97e2a7a827SDan Gohmandef : Pat<(f32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
98e2a7a827SDan Gohman          (CALL_F32 tglobaladdr:$callee)>;
99e2a7a827SDan Gohmandef : Pat<(f64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
100e2a7a827SDan Gohman          (CALL_F64 tglobaladdr:$callee)>;
10139bf39f3SDerek Schuffdef : Pat<(v16i8 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
10239bf39f3SDerek Schuff          (CALL_v16i8 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
10339bf39f3SDerek Schuffdef : Pat<(v8i16 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
10439bf39f3SDerek Schuff          (CALL_v8i16 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
10539bf39f3SDerek Schuffdef : Pat<(v4i32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
10639bf39f3SDerek Schuff          (CALL_v4i32 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
10739bf39f3SDerek Schuffdef : Pat<(v4f32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
10839bf39f3SDerek Schuff          (CALL_v4f32 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
109e2a7a827SDan Gohmandef : Pat<(WebAssemblycall0 (WebAssemblywrapper tglobaladdr:$callee)),
110e2a7a827SDan Gohman          (CALL_VOID tglobaladdr:$callee)>;
111e2a7a827SDan Gohman
112e2a7a827SDan Gohman// Patterns for matching a direct call to an external symbol.
113e2a7a827SDan Gohmandef : Pat<(i32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
114e2a7a827SDan Gohman          (CALL_I32 texternalsym:$callee)>;
115e2a7a827SDan Gohmandef : Pat<(i64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
116e2a7a827SDan Gohman          (CALL_I64 texternalsym:$callee)>;
117e2a7a827SDan Gohmandef : Pat<(f32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
118e2a7a827SDan Gohman          (CALL_F32 texternalsym:$callee)>;
119e2a7a827SDan Gohmandef : Pat<(f64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
120e2a7a827SDan Gohman          (CALL_F64 texternalsym:$callee)>;
12139bf39f3SDerek Schuffdef : Pat<(v16i8 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
12239bf39f3SDerek Schuff          (CALL_v16i8 texternalsym:$callee)>, Requires<[HasSIMD128]>;
12339bf39f3SDerek Schuffdef : Pat<(v8i16 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
12439bf39f3SDerek Schuff          (CALL_v8i16 texternalsym:$callee)>, Requires<[HasSIMD128]>;
12539bf39f3SDerek Schuffdef : Pat<(v4i32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
12639bf39f3SDerek Schuff          (CALL_v4i32 texternalsym:$callee)>, Requires<[HasSIMD128]>;
12739bf39f3SDerek Schuffdef : Pat<(v4f32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
12839bf39f3SDerek Schuff          (CALL_v4f32 texternalsym:$callee)>, Requires<[HasSIMD128]>;
129e2a7a827SDan Gohmandef : Pat<(WebAssemblycall0 (WebAssemblywrapper texternalsym:$callee)),
130e2a7a827SDan Gohman          (CALL_VOID texternalsym:$callee)>;
131