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 __i386__
8
9// _chkstk (_alloca) routine - probe stack between %esp and (%esp-%eax) in 4k increments,
10// then decrement %esp by %eax.  Preserves all registers except %esp 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) // _chkstk and _alloca are the same function
17DEFINE_COMPILERRT_FUNCTION(_chkstk)
18        push   %ecx
19        cmp    $0x1000,%eax
20        lea    8(%esp),%ecx     // esp before calling this routine -> ecx
21        jb     1f
222:
23        sub    $0x1000,%ecx
24        test   %ecx,(%ecx)
25        sub    $0x1000,%eax
26        cmp    $0x1000,%eax
27        ja     2b
281:
29        sub    %eax,%ecx
30        test   %ecx,(%ecx)
31
32        lea    4(%esp),%eax     // load pointer to the return address into eax
33        mov    %ecx,%esp        // install the new top of stack pointer into esp
34        mov    -4(%eax),%ecx    // restore ecx
35        push   (%eax)           // push return address onto the stack
36        sub    %esp,%eax        // restore the original value in eax
37        ret
38END_COMPILERRT_FUNCTION(_chkstk)
39END_COMPILERRT_FUNCTION(_alloca)
40
41#endif // __i386__
42