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
17        push   %ecx
18        cmp    $0x1000,%eax
19        lea    8(%esp),%ecx     // esp before calling this routine -> ecx
20        jb     1f
212:
22        sub    $0x1000,%ecx
23        test   %ecx,(%ecx)
24        sub    $0x1000,%eax
25        cmp    $0x1000,%eax
26        ja     2b
271:
28        sub    %eax,%ecx
29        test   %ecx,(%ecx)
30
31        lea    4(%esp),%eax     // load pointer to the return address into eax
32        mov    %ecx,%esp        // install the new top of stack pointer into esp
33        mov    -4(%eax),%ecx    // restore ecx
34        push   (%eax)           // push return address onto the stack
35        sub    %esp,%eax        // restore the original value in eax
36        ret
37END_COMPILERRT_FUNCTION(_alloca)
38
39#endif // __i386__
40