177ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao/* ===-- trampoline_setup_test.c - Test __trampoline_setup -----------------===
277ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao *
377ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao *                     The LLVM Compiler Infrastructure
477ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao *
577ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao * This file is distributed under the University of Illinois Open Source
677ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao * License. See LICENSE.TXT for details.
777ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao *
877ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao * ===----------------------------------------------------------------------===
977ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao */
1077ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao
1177ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao
1277ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao#include <stdio.h>
1377ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao#include <string.h>
1477ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao#include <stdint.h>
1577ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao#include <sys/mman.h>
1677ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao
1777ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao/*
1877ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao * Tests nested functions
1977ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao * The ppc compiler generates a call to __trampoline_setup
2077ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao * The i386 and x86_64 compilers generate a call to ___enable_execute_stack
2177ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao */
2277ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao
2377ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao/*
2477ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao * Note that, nested functions are not ISO C and are not supported in Clang.
2577ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao */
2677ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao
2777ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao#if !defined(__clang__)
2877ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao
2977ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liaotypedef int (*nested_func_t)(int x);
3077ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao
3177ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liaonested_func_t proc;
3277ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao
3377ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liaoint main() {
3477ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao    /* Some locals */
3577ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao    int c = 10;
3677ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao    int d = 7;
3777ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao
3877ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao    /* Define a nested function: */
3977ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao    int bar(int x) { return x*5 + c*d; };
4077ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao
4177ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao    /* Assign global to point to nested function
4277ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao     * (really points to trampoline). */
4377ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao    proc = bar;
4477ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao
4577ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao    /* Invoke nested function: */
4677ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao    c = 4;
4777ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao    if ( (*proc)(3) != 43 )
4877ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao        return 1;
4977ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao    d = 5;
5077ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao    if ( (*proc)(4) != 40 )
5177ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao        return 1;
5277ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao
5377ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao    /* Success. */
5477ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao    return 0;
5577ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao}
5677ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao
5777ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao#else
5877ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao
5977ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liaoint main() {
6077ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao  return 0;
6177ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao}
6277ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao
6377ed6142daed1e068fbda64405d0de9845e40e1Shih-wei Liao#endif
64