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 {
22*d526b13eSSerge Pavlovdef ADJCALLSTACKDOWN : I<(outs), (ins i32imm:$amt, i32imm:$amt2),
23*d526b13eSSerge Pavlov                         [(WebAssemblycallseq_start timm:$amt, timm:$amt2)]>;
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> {
2900d734d8SDan Gohman  def CALL_#vt : I<(outs vt:$dst), (ins function32_op:$callee, variable_ops),
30e2a7a827SDan Gohman                   [(set vt:$dst, (WebAssemblycall1 (i32 imm:$callee)))],
313acb187dSDan Gohman                   !strconcat(prefix, "call\t$dst, $callee"),
323acb187dSDan Gohman                   0x10>;
33d934cb88SDan Gohman
346f69783fSDerek Schuff  let isCodeGenOnly = 1 in {
356f69783fSDerek Schuff    def PCALL_INDIRECT_#vt : I<(outs vt:$dst), (ins I32:$callee, variable_ops),
36cf4748f1SDan Gohman                              [(set vt:$dst, (WebAssemblycall1 I32:$callee))],
376f69783fSDerek Schuff                              "PSEUDO CALL INDIRECT\t$callee">;
386f69783fSDerek Schuff  } // isCodeGenOnly = 1
39f50d964bSDan Gohman
40d934cb88SDan Gohman  def CALL_INDIRECT_#vt : I<(outs vt:$dst),
41d934cb88SDan Gohman                            (ins TypeIndex:$type, i32imm:$flags, variable_ops),
426f69783fSDerek Schuff                            [],
433acb187dSDan Gohman                            !strconcat(prefix, "call_indirect\t$dst"),
443acb187dSDan Gohman                            0x11>;
45af111db8SJF Bastien}
4639bf39f3SDerek Schuff
4739bf39f3SDerek Schuffmulticlass SIMD_CALL<ValueType vt, string prefix> {
4800d734d8SDan Gohman  def CALL_#vt : SIMD_I<(outs V128:$dst), (ins function32_op:$callee, variable_ops),
4939bf39f3SDerek Schuff                         [(set (vt V128:$dst),
5039bf39f3SDerek Schuff                               (WebAssemblycall1 (i32 imm:$callee)))],
513acb187dSDan Gohman                         !strconcat(prefix, "call\t$dst, $callee"),
523acb187dSDan Gohman                         0x10>;
53d934cb88SDan Gohman
546f69783fSDerek Schuff  let isCodeGenOnly = 1 in {
556f69783fSDerek Schuff    def PCALL_INDIRECT_#vt : SIMD_I<(outs V128:$dst),
5639bf39f3SDerek Schuff                                    (ins I32:$callee, variable_ops),
5739bf39f3SDerek Schuff                                    [(set (vt V128:$dst),
5839bf39f3SDerek Schuff                                          (WebAssemblycall1 I32:$callee))],
596f69783fSDerek Schuff                                    "PSEUDO CALL INDIRECT\t$callee">;
606f69783fSDerek Schuff  } // isCodeGenOnly = 1
61f50d964bSDan Gohman
626f69783fSDerek Schuff  def CALL_INDIRECT_#vt : SIMD_I<(outs V128:$dst),
63d934cb88SDan Gohman                                  (ins TypeIndex:$type, i32imm:$flags,
64d934cb88SDan Gohman                                       variable_ops),
656f69783fSDerek Schuff                                  [],
663acb187dSDan Gohman                                  !strconcat(prefix, "call_indirect\t$dst"),
673acb187dSDan Gohman                                  0x11>;
6839bf39f3SDerek Schuff}
6939bf39f3SDerek Schuff
70af111db8SJF Bastienlet Uses = [SP32, SP64], isCall = 1 in {
71c7c04454SDan Gohman  defm : CALL<I32, "i32.">;
72c7c04454SDan Gohman  defm : CALL<I64, "i64.">;
73c7c04454SDan Gohman  defm : CALL<F32, "f32.">;
74c7c04454SDan Gohman  defm : CALL<F64, "f64.">;
7539bf39f3SDerek Schuff  defm : SIMD_CALL<v16i8, "i8x16.">;
7639bf39f3SDerek Schuff  defm : SIMD_CALL<v8i16, "i16x8.">;
7739bf39f3SDerek Schuff  defm : SIMD_CALL<v4i32, "i32x4.">;
7839bf39f3SDerek Schuff  defm : SIMD_CALL<v4f32, "f32x4.">;
79f71abef7SDan Gohman
8000d734d8SDan Gohman  def CALL_VOID : I<(outs), (ins function32_op:$callee, variable_ops),
81e2a7a827SDan Gohman                    [(WebAssemblycall0 (i32 imm:$callee))],
823acb187dSDan Gohman                    "call    \t$callee", 0x10>;
83d934cb88SDan Gohman
846f69783fSDerek Schuff  let isCodeGenOnly = 1 in {
856f69783fSDerek Schuff    def PCALL_INDIRECT_VOID : I<(outs), (ins I32:$callee, variable_ops),
86cf4748f1SDan Gohman                      [(WebAssemblycall0 I32:$callee)],
876f69783fSDerek Schuff                      "PSEUDO CALL INDIRECT\t$callee">;
886f69783fSDerek Schuff  } // isCodeGenOnly = 1
89f50d964bSDan Gohman
90d934cb88SDan Gohman  def CALL_INDIRECT_VOID : I<(outs),
91d934cb88SDan Gohman                             (ins TypeIndex:$type, i32imm:$flags, variable_ops),
926f69783fSDerek Schuff                             [],
933acb187dSDan Gohman                             "call_indirect\t", 0x11>;
94af111db8SJF Bastien} // Uses = [SP32,SP64], isCall = 1
95fb3e0594SDan Gohman
96fb3e0594SDan Gohman} // Defs = [ARGUMENTS]
97e2a7a827SDan Gohman
98e2a7a827SDan Gohman// Patterns for matching a direct call to a global address.
99e2a7a827SDan Gohmandef : Pat<(i32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
100e2a7a827SDan Gohman          (CALL_I32 tglobaladdr:$callee)>;
101e2a7a827SDan Gohmandef : Pat<(i64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
102e2a7a827SDan Gohman          (CALL_I64 tglobaladdr:$callee)>;
103e2a7a827SDan Gohmandef : Pat<(f32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
104e2a7a827SDan Gohman          (CALL_F32 tglobaladdr:$callee)>;
105e2a7a827SDan Gohmandef : Pat<(f64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
106e2a7a827SDan Gohman          (CALL_F64 tglobaladdr:$callee)>;
10739bf39f3SDerek Schuffdef : Pat<(v16i8 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
10839bf39f3SDerek Schuff          (CALL_v16i8 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
10939bf39f3SDerek Schuffdef : Pat<(v8i16 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
11039bf39f3SDerek Schuff          (CALL_v8i16 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
11139bf39f3SDerek Schuffdef : Pat<(v4i32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
11239bf39f3SDerek Schuff          (CALL_v4i32 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
11339bf39f3SDerek Schuffdef : Pat<(v4f32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
11439bf39f3SDerek Schuff          (CALL_v4f32 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
115e2a7a827SDan Gohmandef : Pat<(WebAssemblycall0 (WebAssemblywrapper tglobaladdr:$callee)),
116e2a7a827SDan Gohman          (CALL_VOID tglobaladdr:$callee)>;
117e2a7a827SDan Gohman
118e2a7a827SDan Gohman// Patterns for matching a direct call to an external symbol.
119e2a7a827SDan Gohmandef : Pat<(i32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
120e2a7a827SDan Gohman          (CALL_I32 texternalsym:$callee)>;
121e2a7a827SDan Gohmandef : Pat<(i64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
122e2a7a827SDan Gohman          (CALL_I64 texternalsym:$callee)>;
123e2a7a827SDan Gohmandef : Pat<(f32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
124e2a7a827SDan Gohman          (CALL_F32 texternalsym:$callee)>;
125e2a7a827SDan Gohmandef : Pat<(f64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
126e2a7a827SDan Gohman          (CALL_F64 texternalsym:$callee)>;
12739bf39f3SDerek Schuffdef : Pat<(v16i8 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
12839bf39f3SDerek Schuff          (CALL_v16i8 texternalsym:$callee)>, Requires<[HasSIMD128]>;
12939bf39f3SDerek Schuffdef : Pat<(v8i16 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
13039bf39f3SDerek Schuff          (CALL_v8i16 texternalsym:$callee)>, Requires<[HasSIMD128]>;
13139bf39f3SDerek Schuffdef : Pat<(v4i32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
13239bf39f3SDerek Schuff          (CALL_v4i32 texternalsym:$callee)>, Requires<[HasSIMD128]>;
13339bf39f3SDerek Schuffdef : Pat<(v4f32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
13439bf39f3SDerek Schuff          (CALL_v4f32 texternalsym:$callee)>, Requires<[HasSIMD128]>;
135e2a7a827SDan Gohmandef : Pat<(WebAssemblycall0 (WebAssemblywrapper texternalsym:$callee)),
136e2a7a827SDan Gohman          (CALL_VOID texternalsym:$callee)>;
137