chkstk.S 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. #include "../assembly.h"
  5. #ifdef __i386__
  6. // _chkstk (_alloca) routine - probe stack between %esp and (%esp-%eax) in 4k increments,
  7. // then decrement %esp by %eax. Preserves all registers except %esp and flags.
  8. // This routine is windows specific
  9. // http://msdn.microsoft.com/en-us/library/ms648426.aspx
  10. .text
  11. .balign 4
  12. DEFINE_COMPILERRT_FUNCTION(_alloca) // _chkstk and _alloca are the same function
  13. push %ecx
  14. cmp $0x1000,%eax
  15. lea 8(%esp),%ecx // esp before calling this routine -> ecx
  16. jb 1f
  17. 2:
  18. sub $0x1000,%ecx
  19. test %ecx,(%ecx)
  20. sub $0x1000,%eax
  21. cmp $0x1000,%eax
  22. ja 2b
  23. 1:
  24. sub %eax,%ecx
  25. test %ecx,(%ecx)
  26. lea 4(%esp),%eax // load pointer to the return address into eax
  27. mov %ecx,%esp // install the new top of stack pointer into esp
  28. mov -4(%eax),%ecx // restore ecx
  29. push (%eax) // push return address onto the stack
  30. sub %esp,%eax // restore the original value in eax
  31. ret
  32. END_COMPILERRT_FUNCTION(_alloca)
  33. #endif // __i386__