1// This file is dual licensed under the MIT and the University of Illinois Open
2// Source Licenses. See LICENSE.TXT for details.
3
4#include "../assembly.h"
5
6#ifdef __i386__
7
8// _chkstk (_alloca) routine - probe stack between %esp and (%esp-%eax) in 4k increments,
9// then decrement %esp by %eax.  Preserves all registers except %esp and flags.
10// This routine is windows specific
11// http://msdn.microsoft.com/en-us/library/ms648426.aspx
12
13.text
14.balign 4
15DEFINE_COMPILERRT_FUNCTION(_alloca) // _chkstk and _alloca are the same function
16DEFINE_COMPILERRT_FUNCTION(__chkstk)
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(__chkstk)
38END_COMPILERRT_FUNCTION(_alloca)
39
40#endif // __i386__
41