Edward O'Callaghan | fb0e926 | 2009-10-29 00:27:08 +0000 | [diff] [blame] | 1 | /* ===-- trampoline_setup_test.c - Test __trampoline_setup -----------------=== |
| 2 | * |
| 3 | * The LLVM Compiler Infrastructure |
| 4 | * |
Howard Hinnant | 9ad441f | 2010-11-16 22:13:33 +0000 | [diff] [blame] | 5 | * This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | * Source Licenses. See LICENSE.TXT for details. |
Edward O'Callaghan | fb0e926 | 2009-10-29 00:27:08 +0000 | [diff] [blame] | 7 | * |
| 8 | * ===----------------------------------------------------------------------=== |
| 9 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 10 | |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <string.h> |
| 14 | #include <stdint.h> |
| 15 | #include <sys/mman.h> |
| 16 | |
Edward O'Callaghan | fb0e926 | 2009-10-29 00:27:08 +0000 | [diff] [blame] | 17 | /* |
| 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 Dunbar | 26d53d0 | 2010-01-18 06:48:06 +0000 | [diff] [blame] | 27 | #if !defined(__clang__) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 28 | |
| 29 | typedef int (*nested_func_t)(int x); |
| 30 | |
| 31 | nested_func_t proc; |
| 32 | |
Daniel Dunbar | 26d53d0 | 2010-01-18 06:48:06 +0000 | [diff] [blame] | 33 | int main() { |
Edward O'Callaghan | fb0e926 | 2009-10-29 00:27:08 +0000 | [diff] [blame] | 34 | /* Some locals */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 35 | int c = 10; |
| 36 | int d = 7; |
| 37 | |
Edward O'Callaghan | fb0e926 | 2009-10-29 00:27:08 +0000 | [diff] [blame] | 38 | /* Define a nested function: */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 39 | int bar(int x) { return x*5 + c*d; }; |
| 40 | |
Edward O'Callaghan | fb0e926 | 2009-10-29 00:27:08 +0000 | [diff] [blame] | 41 | /* Assign global to point to nested function |
| 42 | * (really points to trampoline). */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 43 | proc = bar; |
| 44 | |
Edward O'Callaghan | fb0e926 | 2009-10-29 00:27:08 +0000 | [diff] [blame] | 45 | /* Invoke nested function: */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 46 | c = 4; |
| 47 | if ( (*proc)(3) != 43 ) |
| 48 | return 1; |
| 49 | d = 5; |
| 50 | if ( (*proc)(4) != 40 ) |
| 51 | return 1; |
| 52 | |
Edward O'Callaghan | fb0e926 | 2009-10-29 00:27:08 +0000 | [diff] [blame] | 53 | /* Success. */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 54 | return 0; |
| 55 | } |
Edward O'Callaghan | fb0e926 | 2009-10-29 00:27:08 +0000 | [diff] [blame] | 56 | |
Daniel Dunbar | 26d53d0 | 2010-01-18 06:48:06 +0000 | [diff] [blame] | 57 | #else |
| 58 | |
| 59 | int main() { |
Joerg Sonnenberger | 7482815 | 2011-05-29 21:43:29 +0000 | [diff] [blame] | 60 | printf("skipped\n"); |
| 61 | return 0; |
Daniel Dunbar | 26d53d0 | 2010-01-18 06:48:06 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | #endif |