58 lines · plain
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-exception4 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 specific12// http://msdn.microsoft.com/en-us/library/ms648426.aspx13 14// Clang on i386 mingw generates calls to "_alloca" (which gets decorated to15// "__alloca").16//17// GCC before 4.6 generated calls a symbol which after decoration is named18// "___chkstk", with three leading underscores. We provide that here as well.19//20// MSVC produces calls to the symbol "__chkstk", with two leading underscores.21// That one has the same signature as this one - but we don't provide that22// symbol here. (If we'd do that, we should do it in a separate object file23// to avoid potential symbol collisions - see24// commit 248aeac1ad2cf4f583490dd1312a5b448d2bb8cc for details.)25//26// GCC after 4.6 generates calls to "___chkstk_ms", which does not decrement27// %esp - that function is defined in chkstk.S.28 29.text30.balign 431DEFINE_COMPILERRT_FUNCTION(_alloca) // _chkstk and _alloca are the same function32// This gets decorated into "___chkstk"; GCC < 4.6 references this symbol.33DEFINE_COMPILERRT_FUNCTION(__chkstk)34 push %ecx35 cmp $0x1000,%eax36 lea 8(%esp),%ecx // esp before calling this routine -> ecx37 jb 1f382:39 sub $0x1000,%ecx40 test %ecx,(%ecx)41 sub $0x1000,%eax42 cmp $0x1000,%eax43 ja 2b441:45 sub %eax,%ecx46 test %ecx,(%ecx)47 48 lea 4(%esp),%eax // load pointer to the return address into eax49 mov %ecx,%esp // install the new top of stack pointer into esp50 mov -4(%eax),%ecx // restore ecx51 push (%eax) // push return address onto the stack52 sub %esp,%eax // restore the original value in eax53 ret54END_COMPILERRT_FUNCTION(__chkstk)55END_COMPILERRT_FUNCTION(_alloca)56 57#endif // __i386__58