3cf8cc286ceb8e8180ed4a6a3d1af1f983b9c8bc
[m6w6/ext-psi] / m4 / psi / psi_decl.m4
1 # psi_add_redir(name, symbol)
2 # Add a function redirection to $PSI_REDIRS.
3 psi_add_redir() {
4 cat >>$PSI_REDIRS <<EOF
5 {"$1", (psi_func_ptr) $2},
6 EOF
7 }
8
9 # psi_add_decl(decl, options)
10 # Add a pre-defined decl to $PSI_VA_DECLS/$PSI_DECLS.
11 psi_add_decl() {
12 case "$2" in
13 *functor*)
14 cat >>$PSI_FN_DECLS <<EOF
15 $1, {0},
16 EOF
17 ;;
18 *vararg*)
19 cat >>$PSI_VA_DECLS <<EOF
20 $1, {0},
21 EOF
22 ;;
23 *)
24 cat >>$PSI_DECLS <<EOF
25 $1, {0},
26 EOF
27 ;;
28 esac
29 }
30
31 dnl PSI_DECL_TYPE(type functor_name, args)
32 dnl Adds a pre-defined functor decl to $PSI_FN_DECLS.
33 AC_DEFUN(PSI_DECL_TYPE, [
34 PSI_DECL_ARGS($1, $2)
35 psi_add_decl "$psi_decl_args" functor
36 ])
37 dnl PSI_REDIR(name, custom symbol)
38 dnl Create a function redirection to an optional custom symbol.
39 AC_DEFUN(PSI_REDIR, [
40 psi_add_redir $1 ifelse([$2],[],[$1],[$2])
41 ])
42
43 dnl PSI_FUNC_LIBC_MAIN()
44 dnl Check for the platforms default stub in executables.
45 AC_DEFUN(PSI_FUNC_LIBC_MAIN, [
46 AC_REQUIRE([AC_PROG_AWK])
47 AC_CACHE_CHECK(for libc start main symbol, psi_cv_libc_main, [
48 psi_libc_main=
49 AC_TRY_LINK(PSI_INCLUDES, [(void)0;], [
50 psi_libc_main=`nm -g conftest$ac_exeext | $AWK -F ' *|@' '/^@<:@@<:@:space:@:>@@:>@+U / {print$[]3; exit}'`
51 ])
52 psi_cv_libc_main=$psi_libc_main
53 ])
54 ])
55
56 dnl PSI_DECL_ARGS(decl args)
57 dnl INTERNAL: build psi_decl_args
58 AC_DEFUN(PSI_DECL_ARGS, [
59 psi_decl_args=
60 PSI_DECL_ARG($1)
61 m4_case([$2],
62 [(void)], [],
63 [()], [],
64 [], [],
65 [m4_map_args_sep([PSI_DECL_ARG(m4_normalize(], [))], [], m4_bregexp([$2], [(\(.*\))], [\1]))])
66 ])
67
68 dnl PSI_DECL_ARG(decl arg)
69 dnl INTERNAL: build psi_decl_args
70 AC_DEFUN(PSI_DECL_ARG, [
71 m4_define([member_name], PSI_VAR_NAME($1))
72 m4_define([member_type], PSI_VAR_TYPE($1))
73
74 PSI_TYPE_INDIRECTION([$1],, pl, as)
75 if test -n "$psi_decl_args"; then
76 psi_decl_args="$psi_decl_args, "
77 fi
78 psi_decl_args="[$psi_decl_args{]PSI_TYPE_PAIR(member_type)[, \"]member_name[\",] $pl, $as[}]"
79 ])
80
81 dnl PSI_DECL(type func, args, flags, libs)
82 dnl Check for a function or macro declaration and a possible asm redirection.
83 dnl Adds a pre-defined (vararg) decl to $PSI_VA_DECLS/$PSI_DECLS.
84 dnl Calls PSI_MACRO if PSI_FUNC fails.
85 AC_DEFUN(PSI_DECL, [
86 AC_REQUIRE([PSI_FUNC_LIBC_MAIN])dnl
87
88 PSI_DECL_ARGS($1, $2)
89
90 psi_symbol="PSI_VAR_NAME($1)"
91 AC_CACHE_CHECK(for PSI_VAR_NAME($1), [psi_cv_fn_]PSI_VAR_NAME($1), [
92 psi_decl_check=
93 psi_symbol_redirect=
94 for lib in "" $4; do
95 decl_save_LIBS=$LIBS
96 LIBS=$lib
97 AC_TRY_LINK(PSI_INCLUDES, [
98 void (*fn)(long, long, long) = (void (*)(long,long,long)) $psi_symbol;
99 fn(1,2,3);
100 ], [
101 psi_symbol_redirect=`nm -g conftest$ac_exeext | $AWK -F ' *|@' '/^@<:@@<:@:space:@:>@@:>@+U '$psi_cv_libc_main'/ {next} /^@<:@@<:@:space:@:>@@:>@+U / {print$[]3; exit}'`
102 ])
103 LIBS=$decl_save_LIBS
104
105 if test -n "$psi_symbol_redirect"; then
106 if test -n "$lib"; then
107 psi_decl_check="$psi_symbol_redirect in $lib"
108 else
109 psi_decl_check="$psi_symbol_redirect"
110 fi
111 break
112 fi
113 done
114 [psi_cv_fn_]PSI_VAR_NAME($1)=$psi_decl_check
115 ])
116
117 psi_symbol_redirect=`AS_ECHO("$[psi_cv_fn_]PSI_VAR_NAME($1)") | $AWK -F " in " '{print [$]1}'`
118 psi_symbol_libflag=`AS_ECHO("$[psi_cv_fn_]PSI_VAR_NAME($1)") | $AWK -F " in " '{print [$]2}'`
119
120 if test -n "$psi_symbol_libflag"; then
121 if ! expr "X$LIBS" : "X.*\b$psi_symbol_libflag\b" >/dev/null; then
122 LIBS="$psi_symbol_libflag $LIBS"
123 fi
124 fi
125
126 case "$psi_symbol_redirect" in
127 "$psi_symbol"|"_$psi_symbol")
128 case "$PSI_EXTRA_REDIRS-$3" in
129 "true-")
130 PSI_MACRO($1, $2, [
131 psi_add_decl "$psi_decl_args"
132 ])
133 ;;
134 *)
135 if test -n "$psi_symbol_libflag"
136 then
137 PSI_REDIR($psi_symbol)
138 fi
139 psi_add_decl "$psi_decl_args" $3
140 ;;
141 esac
142 ;;
143 "")
144 ifelse([$3], vararg, [
145 AC_MSG_WARN(varargs macro support is not implemented)
146 ],[
147 PSI_MACRO($1, $2, [
148 psi_add_decl "$psi_decl_args"
149 ])
150 ])
151 ;;
152 *)
153 PSI_REDIR($psi_symbol)
154 psi_add_decl "$psi_decl_args" $3
155 ;;
156 esac
157 ])