blob: dc30fb6df60aa6e2cbbab8117f7289fa11f7df9e [file] [log] [blame]
Edward O'Callaghanfb0e9262009-10-29 00:27:08 +00001/* ===-- trampoline_setup_test.c - Test __trampoline_setup -----------------===
2 *
3 * The LLVM Compiler Infrastructure
4 *
Howard Hinnant9ad441f2010-11-16 22:13:33 +00005 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
Edward O'Callaghanfb0e9262009-10-29 00:27:08 +00007 *
8 * ===----------------------------------------------------------------------===
9 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000010
11
12#include <stdio.h>
13#include <string.h>
14#include <stdint.h>
15#include <sys/mman.h>
16
Edward O'Callaghanfb0e9262009-10-29 00:27:08 +000017/*
18 * Tests nested functions
19 * The ppc compiler generates a call to __trampoline_setup
20 * The i386 and x86_64 compilers generate a call to ___enable_execute_stack
21 */
22
23/*
24 * Note that, nested functions are not ISO C and are not supported in Clang.
25 */
26
Daniel Dunbar26d53d02010-01-18 06:48:06 +000027#if !defined(__clang__)
Daniel Dunbarb3a69012009-06-26 16:47:03 +000028
29typedef int (*nested_func_t)(int x);
30
31nested_func_t proc;
32
Daniel Dunbar26d53d02010-01-18 06:48:06 +000033int main() {
Edward O'Callaghanfb0e9262009-10-29 00:27:08 +000034 /* Some locals */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000035 int c = 10;
36 int d = 7;
37
Edward O'Callaghanfb0e9262009-10-29 00:27:08 +000038 /* Define a nested function: */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000039 int bar(int x) { return x*5 + c*d; };
40
Edward O'Callaghanfb0e9262009-10-29 00:27:08 +000041 /* Assign global to point to nested function
42 * (really points to trampoline). */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000043 proc = bar;
44
Edward O'Callaghanfb0e9262009-10-29 00:27:08 +000045 /* Invoke nested function: */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000046 c = 4;
47 if ( (*proc)(3) != 43 )
48 return 1;
49 d = 5;
50 if ( (*proc)(4) != 40 )
51 return 1;
52
Edward O'Callaghanfb0e9262009-10-29 00:27:08 +000053 /* Success. */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000054 return 0;
55}
Edward O'Callaghanfb0e9262009-10-29 00:27:08 +000056
Daniel Dunbar26d53d02010-01-18 06:48:06 +000057#else
58
59int main() {
Joerg Sonnenberger74828152011-05-29 21:43:29 +000060 printf("skipped\n");
61 return 0;
Daniel Dunbar26d53d02010-01-18 06:48:06 +000062}
63
64#endif