1// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2// See https://llvm.org/LICENSE.txt for license information.
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#include "../assembly.h"
6
7#ifdef __x86_64__
8
9// _chkstk (_alloca) routine - probe stack between %rsp and (%rsp-%rax) in 4k increments,
10// then decrement %rsp by %rax.  Preserves all registers except %rsp and flags.
11// This routine is windows specific
12// http://msdn.microsoft.com/en-us/library/ms648426.aspx
13
14.text
15.balign 4
16DEFINE_COMPILERRT_FUNCTION(__alloca)
17        mov    %rcx,%rax        // x64 _alloca is a normal function with parameter in rcx
18        // fallthrough
19DEFINE_COMPILERRT_FUNCTION(___chkstk)
20        push   %rcx
21        cmp    $0x1000,%rax
22        lea    16(%rsp),%rcx     // rsp before calling this routine -> rcx
23        jb     1f
242:
25        sub    $0x1000,%rcx
26        test   %rcx,(%rcx)
27        sub    $0x1000,%rax
28        cmp    $0x1000,%rax
29        ja     2b
301:
31        sub    %rax,%rcx
32        test   %rcx,(%rcx)
33
34        lea    8(%rsp),%rax     // load pointer to the return address into rax
35        mov    %rcx,%rsp        // install the new top of stack pointer into rsp
36        mov    -8(%rax),%rcx    // restore rcx
37        push   (%rax)           // push return address onto the stack
38        sub    %rsp,%rax        // restore the original value in rax
39        ret
40END_COMPILERRT_FUNCTION(___chkstk)
41END_COMPILERRT_FUNCTION(__alloca)
42
43#endif // __x86_64__
44