xref: /aosp_15_r20/external/compiler-rt/lib/builtins/i386/chkstk2.S (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot// This file is dual licensed under the MIT and the University of Illinois Open
2*7c3d14c8STreehugger Robot// Source Licenses. See LICENSE.TXT for details.
3*7c3d14c8STreehugger Robot
4*7c3d14c8STreehugger Robot#include "../assembly.h"
5*7c3d14c8STreehugger Robot
6*7c3d14c8STreehugger Robot#ifdef __i386__
7*7c3d14c8STreehugger Robot
8*7c3d14c8STreehugger Robot// _chkstk (_alloca) routine - probe stack between %esp and (%esp-%eax) in 4k increments,
9*7c3d14c8STreehugger Robot// then decrement %esp by %eax.  Preserves all registers except %esp and flags.
10*7c3d14c8STreehugger Robot// This routine is windows specific
11*7c3d14c8STreehugger Robot// http://msdn.microsoft.com/en-us/library/ms648426.aspx
12*7c3d14c8STreehugger Robot
13*7c3d14c8STreehugger Robot.text
14*7c3d14c8STreehugger Robot.balign 4
15*7c3d14c8STreehugger RobotDEFINE_COMPILERRT_FUNCTION(_alloca) // _chkstk and _alloca are the same function
16*7c3d14c8STreehugger RobotDEFINE_COMPILERRT_FUNCTION(__chkstk)
17*7c3d14c8STreehugger Robot        push   %ecx
18*7c3d14c8STreehugger Robot        cmp    $0x1000,%eax
19*7c3d14c8STreehugger Robot        lea    8(%esp),%ecx     // esp before calling this routine -> ecx
20*7c3d14c8STreehugger Robot        jb     1f
21*7c3d14c8STreehugger Robot2:
22*7c3d14c8STreehugger Robot        sub    $0x1000,%ecx
23*7c3d14c8STreehugger Robot        test   %ecx,(%ecx)
24*7c3d14c8STreehugger Robot        sub    $0x1000,%eax
25*7c3d14c8STreehugger Robot        cmp    $0x1000,%eax
26*7c3d14c8STreehugger Robot        ja     2b
27*7c3d14c8STreehugger Robot1:
28*7c3d14c8STreehugger Robot        sub    %eax,%ecx
29*7c3d14c8STreehugger Robot        test   %ecx,(%ecx)
30*7c3d14c8STreehugger Robot
31*7c3d14c8STreehugger Robot        lea    4(%esp),%eax     // load pointer to the return address into eax
32*7c3d14c8STreehugger Robot        mov    %ecx,%esp        // install the new top of stack pointer into esp
33*7c3d14c8STreehugger Robot        mov    -4(%eax),%ecx    // restore ecx
34*7c3d14c8STreehugger Robot        push   (%eax)           // push return address onto the stack
35*7c3d14c8STreehugger Robot        sub    %esp,%eax        // restore the original value in eax
36*7c3d14c8STreehugger Robot        ret
37*7c3d14c8STreehugger RobotEND_COMPILERRT_FUNCTION(__chkstk)
38*7c3d14c8STreehugger RobotEND_COMPILERRT_FUNCTION(_alloca)
39*7c3d14c8STreehugger Robot
40*7c3d14c8STreehugger Robot#endif // __i386__
41