avcall.3
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:8k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

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