DOC
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:6k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

  1.   Copyright 1993 Bill Triggs, <Bill.Triggs@inrialpes.fr>
  2.   Copyright 1995 Bruno Haible, <haible@clisp.cons.org>
  3.   This is free software distributed under the GNU General Public
  4.   Licence described in the file COPYING. Contact the author if 
  5.   you don't have this or can't live with it. There is ABSOLUTELY 
  6.   NO WARRANTY, explicit or implied, on this software.
  7. ----------------------------------------------------------------------
  8. AVCALL --- a foreign function interface to ANSI-C
  9. ----------------------------------------------------------------------
  10. This library allows arbitrary C functions to be called from embedded
  11. interpreters, debuggers, RPC calls, etc, by building up a C argument
  12. list incrementally from explicitly typed arguments. This considerably
  13. reduces the amount of boilerplate glue code required for such
  14. applications.
  15. The interface is like stdargs/varargs in reverse and is intended to be as
  16. portable as possible, however the details of function calling are highly
  17. machine-dependent so your mileage may vary. At the very least there are
  18. typically built-in limits on the size of the argument-list. The
  19. argument-pushing macros all return 0 for success, < 0 for error (eg,
  20. arg-list overflow).
  21. Installation instructions are in the Makefile.
  22. ----------------------------------------------------------------------
  23. DECLARE ALIST -> OPEN ALIST -> SET FLAGS -> PUSH ARGS -> CALL FUNCTION
  24. ----------------------------------------------------------------------
  25. 1) Declare the argument list structure:
  26. #include "avcall.h"
  27. {
  28.   av_alist alist;
  29. 2) Set any special flags. This is architecture and compiler dependent.
  30. Sometimes, compiler options must be flagged by #defines before the
  31. #include <avcall.h>. Usually, however, the `configure' script should
  32. have determined which #defines are needed and put them at the head of
  33. avcall.h.
  34. 3) Initialise the alist with the function address and return type. 
  35. There is a separate macro for each built-in C type (char, int, float, etc).
  36. Eg,
  37. av_start_int(alist,&func,&return_addr);
  38. or
  39. av_start_double(alist,&func,&return_addr);
  40. etc.
  41. Functions returning a structure or pointer take an extra type argument:
  42. Eg,
  43. av_start_struct(alist,&func,STRUCT_OR_UNION_TYPE,SPLITTABLE,&return_addr);
  44. or
  45. av_start_ptr(alist,&func,POINTER_TYPE,&return_addr);
  46. 4) Push the arguments one by one in order. There is a macro for each
  47. built-in C type, eg:
  48. av_int(alist,value);
  49. or
  50. av_double(alist,value);
  51. Structure and pointer arguments require an extra type argument:
  52. av_struct(alist,STRUCT_TYPE,value);
  53. or
  54. av_ptr(alist,POINTER_TYPE,value);
  55. 5) Call the function, set the return value, and tidy up:
  56. av_call(alist);
  57. ----------------------------------------------------------------------
  58. NOTES
  59. 1) Functions declared in K&R style (ie, without a typed arglist) must
  60. use default K&R expression promotions (char,short-->int; float-->double)
  61. whether they are compiled by a K&R or an ANSI compiler, because the 
  62. true arg types may not be known at the call point. Such functions
  63. back-convert their arguments to the declared types on function entry.
  64. The only way to pass a true char, short or float (eg, from K&R C to an 
  65. ANSI or varargs function) is by an explicit cast: foo((char)c,(float)f).
  66.  !! Hence, for args of functions declared in K&R style you should use
  67.  !! av_int() and av_double() instead of av_{char,short}() and av_float().
  68. If you use a K&R compiler, the avcall header files may detect this and 
  69. define av_float, etc, appropriately, but with an ANSI compiler there's 
  70. no way avcall can know how a function was declared, so you have to 
  71. correct the argument types yourself. Similarly, some K&R compilers (such 
  72. as Sun cc on the sparc) actually return a float as a double.
  73. 2) There are too many possible structure and pointer types to have a
  74. separate macro for each, so the pointer and structure macros take an
  75. explicit type argument which may be used (eg) to calculate the size of
  76. the structure. On most architectures this provides enough information
  77. for the compiler to make the proper call, but there will always be
  78. machines with odd alignment requirements or argument passing
  79. conventions, unusual reprentations for function, char, or void pointers,
  80. etc, for which this scheme will not suffice.  These machines may define
  81. additional av_start_TYPE and av_TYPE macros.
  82. 3) The current implementations are pretty flakey in places. I'm happy to 
  83. accept new ports and (properly tested) fixes and enhancements. In
  84. particular, many of the routines waste a lot of stack space and generally
  85. do hairy things with stack frames - a bit more assembly code would probably 
  86. help things along quite a bit, but I don't speak assembler at all well.
  87. 4) The macros required for all this are pretty grungy, but it does seem
  88. to be possible to port avcall to many machines. Some of the grunge is
  89. usually handled by a C or assembly level glue routine that actually
  90. pushes the arguments, calls the function and unpacks any return value.
  91. This is called __builtin_avcall(). A precompiled assembler version for
  92. people without gcc is also made available. The routine should ideally
  93. have flags for the passing conventions of other compilers.
  94. ----------------------------------------------------------------------
  95. ACKNOWLEDGEMENTS
  96. I was aware of two similar but rather more restricted foreign function 
  97. interfaces when the initial version of this library was written, although
  98. (I believe) all of the present code is my own: the C interface in the zelk
  99. extensions to Oliver Laumann's <net@cs.tu-berlin.de> Elk scheme interpreter
  100. by J.P.Lewis, NEC C&C Research, <zilla@ccrl.nj.nec.com> (for Sun4 and SGI); 
  101. and Roy Featherstone's <roy@robots.oxford.ac.uk> personal C interface 
  102. library for Sun3,4 and SGI. I also looked at the comments and some of the 
  103. code in the machine-dependent parts of the GCC and GDB distributions, and 
  104. put the GCC __asm__ extensions to good use. Thanks guys!
  105. This work was partly supported by EC-ESPRIT Basic Research Action SECOND.
  106. ----------------------------------------------------------------------