~ubuntu-branches/debian/squeeze/ffcall/squeeze

« back to all changes in this revision

Viewing changes to avcall/avcall.html

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Egger
  • Date: 2010-06-26 15:29:30 UTC
  • mfrom: (5.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20100626152930-c09y01gk3szcnykn
Tags: 1.10+cvs20100619-2
Ship to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<HEAD>
 
2
<TITLE> AVCALL manual page </TITLE>
 
3
</HEAD>
 
4
<BODY>
 
5
<H1>AVCALL manual page</H1>
 
6
 
 
7
<UL>
 
8
<LI> <A HREF="#Name">Name</A>
 
9
<LI> <A HREF="#Synopsis">Synopsis</A>
 
10
<LI> <A HREF="#Description">Description</A>
 
11
<LI> <A HREF="#Notes">Notes</A>
 
12
<LI> <A HREF="#See also">See also</A>
 
13
<LI> <A HREF="#Bugs">Bugs</A>
 
14
<LI> <A HREF="#Non-Bugs">Non-Bugs</A>
 
15
<LI> <A HREF="#Porting AVCALL">Porting AVCALL</A>
 
16
<LI> <A HREF="#Author">Author</A>
 
17
<LI> <A HREF="#Acknowledgements">Acknowledgements</A>
 
18
</UL>
 
19
<P>
 
20
 
 
21
<HR>
 
22
 
 
23
<A NAME="Name">
 
24
<H2>Name</H2>
 
25
</A>
 
26
 
 
27
avcall  -  build a C argument list incrementally and call a
 
28
C function on it.
 
29
 
 
30
<A NAME="Synopsis">
 
31
<H2>Synopsis</H2>
 
32
</A>
 
33
 
 
34
<PRE>
 
35
<CODE>#include &lt;avcall.h&gt;</CODE>
 
36
<CODE>av_alist <VAR>alist</VAR>;</CODE>
 
37
<CODE>av_start_<VAR>type</VAR> (<VAR>alist</VAR>, &amp;<VAR>func</VAR>, [[<VAR>return_type</VAR>,] &amp;<VAR>return_value</VAR>]);</CODE>
 
38
<CODE>av_<VAR>type</VAR> (<VAR>alist</VAR>, [<VAR>arg_type</VAR>,] <VAR>value</VAR>);</CODE>
 
39
<CODE>av_call(<VAR>alist</VAR>);</CODE>
 
40
</PRE>
 
41
 
 
42
<A NAME="Description">
 
43
<H2>Description</H2>
 
44
</A>
 
45
 
 
46
This  set  of macros builds an argument list for a C function
 
47
and  calls  the  function  on  it.  It  significantly
 
48
reduces  the  amount  of `glue' code required for parsers,
 
49
debuggers, imbedded interpreters, C extensions to application
 
50
programs  and  other situations where collections of
 
51
functions need  to  be  called  on  lists  of  externally-
 
52
supplied arguments.
 
53
<P>
 
54
Function  calling  conventions differ considerably on different
 
55
machines and <CODE>avcall</CODE> attempts to provide some degree
 
56
of isolation from such architecture dependencies.
 
57
<P>
 
58
The  interface  is  like <A HREF="stdarg(3)"><CODE><B>stdarg</B></CODE></A>(3) in reverse. All of the
 
59
macros return 0 for success, < 0 for failure (e.g.,  argument
 
60
list overflow or type-not-supported).
 
61
<P>
 
62
<OL>
 
63
<LI> <CODE>#include  &lt;avcall.h&gt;</CODE>  and  declare  the argument list
 
64
structure <CODE>av_alist <VAR>alist</VAR>;</CODE>
 
65
<P>
 
66
<LI> Set  any special flags. This is architecture and compiler
 
67
dependent.
 
68
Compiler  options that affect passing conventions may need
 
69
to be flagged by <CODE>#define</CODE>s before the <CODE>#include &lt;avcall.h&gt;</CODE>
 
70
statement. However, the <SAMP>configure</SAMP> script should have
 
71
determined which <CODE>#define</CODE>s are needed and put them
 
72
at the top of <SAMP>avcall.h</SAMP>.
 
73
<P>
 
74
<LI> Initialize the alist with  the  function  address  and
 
75
return  value  pointer (if any). There is a separate macro
 
76
for each simple return type  ([u]char,  [u]short,  [u]int,
 
77
[u]long,  [u]longlong, float,  double, where `u' indicates
 
78
`unsigned'). The macros for functions returning structures
 
79
or pointers require an explicit type argument.
 
80
<P>
 
81
E.g.,
 
82
<PRE>
 
83
<CODE>av_start_int (<VAR>alist</VAR>, &amp;<VAR>func</VAR>, &amp;<VAR>int_return</VAR>);</CODE>
 
84
<CODE>av_start_double (<VAR>alist</VAR>, &amp;<VAR>func</VAR>, &amp;<VAR>double_return</VAR>);</CODE>
 
85
<CODE>av_start_void (<VAR>alist</VAR>, &amp;<VAR>func</VAR>);</CODE>
 
86
<CODE>av_start_struct (<VAR>alist</VAR>, &amp;<VAR>func</VAR>, <VAR>struct_type</VAR>, <VAR>splittable</VAR>, &amp;<VAR>struct_return</VAR>);</CODE>
 
87
<CODE>av_start_ptr (<VAR>alist</VAR>, &amp;<VAR>func</VAR>, <VAR>pointer_type</VAR>, &amp;<VAR>pointer_return</VAR>);</CODE>
 
88
</PRE>
 
89
The  <VAR>splittable</VAR> flag specifies whether the <VAR>struct_type</VAR> can
 
90
be returned in registers such that every struct field fits
 
91
entirely  in a single register. This needs to be specified
 
92
for structs of size <SAMP>2*sizeof(long)</SAMP>. For structs of size
 
93
&lt;= <SAMP>sizeof(long)</SAMP>,  splittable  is ignored and assumed to be 1.
 
94
For  structs  of  size  &gt; <SAMP>2*sizeof(long)</SAMP>,  splittable  is
 
95
ignored  and  assumed to be 0. There are some handy macros
 
96
for this:
 
97
<PRE>
 
98
<CODE>av_word_splittable_1 (<VAR>type1</VAR>)</CODE>
 
99
<CODE>av_word_splittable_2 (<VAR>type1</VAR>, <VAR>type2</VAR>)</CODE>
 
100
<CODE>av_word_splittable_3 (<VAR>type1</VAR>, <VAR>type2</VAR>, <VAR>type3</VAR>)</CODE>
 
101
<CODE>av_word_splittable_4 (<VAR>type1</VAR>, <VAR>type2</VAR>, <VAR>type3</VAR>, <VAR>type4</VAR>)</CODE>
 
102
</PRE>
 
103
For a struct with three slots
 
104
<PRE>
 
105
<CODE>struct { <VAR>type1 id1</VAR>; <VAR>type2 id2</VAR>; <VAR>type3 id3</VAR>; }</CODE>
 
106
</PRE>
 
107
you can specify <VAR>splittable</VAR> as
 
108
<CODE>av_word_splittable_3 (<VAR>type1</VAR>, <VAR>type2</VAR>, <VAR>type3</VAR>)</CODE>.
 
109
<P>
 
110
<LI> Push  the  arguments  on  to the list in order. Again
 
111
there is a macro for each simple built-in  type,  and  the
 
112
macros  for  structure  and  pointer  arguments require an
 
113
extra type argument:
 
114
<PRE>
 
115
<CODE>av_int (<VAR>alist</VAR>, <VAR>int_value</VAR>);</CODE>
 
116
<CODE>av_double (<VAR>alist</VAR>, <VAR>double_value</VAR>);</CODE>
 
117
<CODE>av_struct (<VAR>alist</VAR>, <VAR>struct_or_union_type</VAR>, <VAR>struct_value</VAR>);</CODE>
 
118
<CODE>av_ptr (<VAR>alist</VAR>, <VAR>pointer_type</VAR>, <VAR>pointer_value</VAR>);</CODE>
 
119
</PRE>
 
120
<LI> Call the function, set the return value, and tidy up:
 
121
<CODE>av_call (<VAR>alist</VAR>);</CODE>
 
122
</OL>
 
123
 
 
124
<A NAME="Notes">
 
125
<H2>Notes</H2>
 
126
</A>
 
127
 
 
128
<OL>
 
129
<LI> Functions whose first declaration is  in  Kernighan  &amp;
 
130
Ritchie  style  (i.e., without a typed argument list) MUST
 
131
use default K&amp;R C expression promotions (<CODE>char</CODE> and <CODE>short</CODE> to
 
132
<CODE>int</CODE>,  <CODE>float</CODE>  to <CODE>double</CODE>) whether they are compiled by a K&amp;R
 
133
or an ANSI compiler, because the true argument  types  may
 
134
not  be  known at the call point. Such functions typically
 
135
back-convert their arguments  to  the  declared  types  on
 
136
function  entry.  (In  fact,  the  only way to pass a true
 
137
<CODE>char</CODE>, <CODE>short</CODE> or <CODE>float</CODE> in K&amp;R C  is  by  an  explicit  cast:
 
138
<CODE>func((char)c,(float)f)</CODE>  ).   Similarly, some K&amp;R compilers
 
139
(such as Sun <SAMP>cc</SAMP> on the sparc) actually return a <CODE>float</CODE> as a
 
140
<CODE>double</CODE>.
 
141
<P>
 
142
Hence,  for  arguments  of functions declared in K&amp;R style
 
143
you  should  use  <CODE>av_int()</CODE>  and  </CODE>av_double()</CODE>  rather  than
 
144
<CODE>av_char()</CODE>,  <CODE>av_short()</CODE>  or  <CODE>av_float()</CODE>.   If you use a K&amp;R
 
145
compiler, the avcall header files may be  able  to  detect
 
146
this  and  define <CODE>av_float()</CODE>, etc, appropriately, but with
 
147
an ANSI compiler there is no way <B>avcall</B> can  know  how  a
 
148
function was declared, so you have to correct the argument
 
149
types yourself.
 
150
<P>
 
151
<LI> The explicit type arguments  of  the  <CODE>av_struct()</CODE>  and
 
152
<CODE>av_ptr()</CODE>  macros  are  typically  used  to calculate size,
 
153
alignment, and passing conventions.  This may not be  sufficient  for  some  machines  with  unusual  structure and
 
154
pointer handling: in this case additional  <CODE>av_start_<VAR>type</VAR>()</CODE>
 
155
and <CODE>av_<VAR>type</VAR>()</CODE> macros may be defined.
 
156
<P>
 
157
<LI> The macros <CODE>av_start_longlong()</CODE>,
 
158
<CODE>av_start_ulonglong()</CODE>, <CODE>av_longlong()</CODE>  and
 
159
<CODE>av_ulonglong()</CODE> work only if the C compiler has a working
 
160
<CODE>long long</CODE> 64-bit integer type.
 
161
<P>
 
162
<LI> The struct types used in <CODE>av_start_struct()</CODE> and
 
163
<CODE>av_struct()</CODE> must only contain (signed or unsigned) int,
 
164
long, long long or pointer fields. Struct types containing
 
165
(signed or unsigned) char, short, float, double or other
 
166
structs are not supported.
 
167
<P>
 
168
</OL>
 
169
 
 
170
<A NAME="See also">
 
171
<H2>See also</H2>
 
172
</A>
 
173
<A HREF="stdarg(3)"><CODE><B>stdarg</B></CODE></A>(3), <A HREF="varargs(3)"><CODE><B>varargs</B></CODE></A>(3).
 
174
 
 
175
<A NAME="Bugs">
 
176
<H2>Bugs</H2>
 
177
</A>
 
178
 
 
179
<UL>
 
180
<LI>
 
181
The  current  implementations have been tested on a selection
 
182
of common cases but there  are  probably  still  many
 
183
bugs.
 
184
<LI>
 
185
There  are  typically  built-in  limits on the size of the
 
186
argument-list, which may also  include  the  size  of  any
 
187
structure arguments.
 
188
<LI>
 
189
The decision whether a struct is to be returned in registers or in memory
 
190
considers only the struct's size and alignment. This is inaccurate: for
 
191
example, gcc on m68k-next returns
 
192
<CODE>struct { char a,b,c; }</CODE>
 
193
in registers and
 
194
<CODE>struct { char a[3]; }</CODE>
 
195
in memory, although both types have the same size and the same alignment.
 
196
</UL>
 
197
 
 
198
<A NAME="Non-Bugs">
 
199
<H2>Non-Bugs</H2>
 
200
</A>
 
201
 
 
202
All  information is passed in CPU registers and the stack.
 
203
The <CODE><B>avcall</B></CODE> package is therefore multithread-safe.
 
204
 
 
205
<A NAME="Porting AVCALL">
 
206
<H2>Porting AVCALL</H2>
 
207
</A>
 
208
 
 
209
Ports,  bug-fixes,  and  suggestions are most welcome. The
 
210
macros required for argument pushing  are  pretty  grungy,
 
211
but  it does seem to be possible to port avcall to a range
 
212
of machines. Ports to non-standard or non-32-bit  machines
 
213
are  especially  welcome  so we can sort the interface out
 
214
before it's too late.
 
215
<P>
 
216
Knowledge about argument passing conventions can be  found
 
217
in  the  gcc source, file <SAMP>gcc-2.6.3/config/<VAR>cpu</VAR>/<VAR>cpu</VAR>.h</SAMP>, section
 
218
<SAMP>"Stack layout; function entry, exit and calling."</SAMP>
 
219
<P>
 
220
Some of the grunge is usually handled by a C  or  assembly
 
221
level  glue  routine  that  actually pushes the arguments,
 
222
calls the function and unpacks  any  return  value. This is
 
223
called <CODE>__builtin_avcall()</CODE>. A precompiled assembler version for
 
224
people without gcc is also made available. The routine should ideally
 
225
have flags for the passing conventions of other compilers.
 
226
<P>
 
227
Many  of  the  current routines waste a lot of stack space
 
228
and generally do hairy things to stack frames - a bit more
 
229
assembly code would probably help things along quite a bit
 
230
here.
 
231
<P>
 
232
 
 
233
<A NAME="Author">
 
234
<H2>Author</H2>
 
235
</A>
 
236
 
 
237
Bill Triggs &lt;Bill.Triggs@inrialpes.fr&gt;, &lt;Bill.Triggs@imag.fr&gt;.
 
238
 
 
239
<A NAME="Acknowledgements">
 
240
<H2>Acknowledgements</H2>
 
241
</A>
 
242
 
 
243
Some initial ideas were stolen from the C interface to the
 
244
Zelk extensions to Oliver Laumann's Elk scheme interpreter
 
245
by J.P.Lewis, NEC  C&amp;C  Research,  &lt;zilla@ccrl.nj.nec.com&gt;
 
246
(for    Sun4    &amp;    SGI),    and    Roy    Featherstone's
 
247
&lt;roy@robots.oxford.ac.uk&gt; personal C interface library for
 
248
Sun3, Sun4  &amp;  SGI.   I  also looked at the machine-dependent
 
249
parts of the GCC and GDB distributions, and  put  the  gcc
 
250
<CODE>asm()</CODE> extensions to good use. Thanks guys!
 
251
<P>
 
252
This work was partly supported by EC-ESPRIT Basic Research
 
253
Action SECOND.
 
254
<P>
 
255
 
 
256
<HR>
 
257
 
 
258
<ADDRESS>AVCALL manual page<BR>
 
259
Bruno Haible &lt;bruno@clisp.org&gt;
 
260
</ADDRESS>
 
261
<P>
 
262
Last modified: 14 January 2001.
 
263
 
 
264
</BODY>
 
265