AAPCS64 reserves any of X9-X15 for a compiler to choose to use for this purpose, and says not to use X16 or X18 like GCC (and the previous implementation) chose to use. The X18 register may need to get used by the kernel in some circumstances, as specified by the platform ABI, so it is generally an unwise choice. Simply choosing a different register fixes the problem of this being broken on any platform that actually follows the platform ABI (which is all of them except EABI, if I am reading this linux kernel bug correctly https://lkml2.uits.iu.edu/hypermail/linux/kernel/2001.2/01502.html). As a side benefit, also generate slightly better code and avoids needing the compiler-rt to be present. I did that by following the XCore implementation instead of PPC (although in hindsight, following the RISCV might have been slightly more readable). That X18 is wrong to use for this purpose has been known for many years (e.g. https://www.mail-archive.com/gcc@gcc.gnu.org/msg76934.html) and also known that fixing this to use one of the correct registers is not an ABI break, since this only appears inside of a translation unit. Some of the other temporary registers (e.g. X9) are already reserved inside llvm for internal use as a generic temporary register in the prologue before saving registers, while X15 was already used in rare cases as a scratch register in the prologue as well, so I felt that seemed the most logical choice to choose here.
56 lines
1005 B
C
56 lines
1005 B
C
// RUN: %clang_builtins %s %librt -o %t && %run %t
|
|
// REQUIRES: librt_has_trampoline_setup
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdint.h>
|
|
|
|
/*
|
|
* Tests nested functions
|
|
* The ppc compiler generates a call to __trampoline_setup
|
|
* The i386 and x86_64 compilers generate a call to ___enable_execute_stack
|
|
*/
|
|
|
|
/*
|
|
* Note that, nested functions are not ISO C and are not supported in Clang.
|
|
*/
|
|
|
|
#if !defined(__clang__)
|
|
|
|
typedef int (*nested_func_t)(int x);
|
|
|
|
nested_func_t proc;
|
|
|
|
int main() {
|
|
/* Some locals */
|
|
int c = 10;
|
|
int d = 7;
|
|
|
|
/* Define a nested function: */
|
|
int bar(int x) { return x*5 + c*d; };
|
|
|
|
/* Assign global to point to nested function
|
|
* (really points to trampoline). */
|
|
proc = bar;
|
|
|
|
/* Invoke nested function: */
|
|
c = 4;
|
|
if ( (*proc)(3) != 43 )
|
|
return 1;
|
|
d = 5;
|
|
if ( (*proc)(4) != 40 )
|
|
return 1;
|
|
|
|
/* Success. */
|
|
return 0;
|
|
}
|
|
|
|
#else
|
|
|
|
int main() {
|
|
printf("skipped\n");
|
|
return 0;
|
|
}
|
|
|
|
#endif
|