Implementing-new-library-functions.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <html lang="en">
  2. <head>
  3. <title>Implementing new library functions - avram - a virtual machine code interpreter</title>
  4. <meta http-equiv="Content-Type" content="text/html">
  5. <meta name="description" content="avram - a virtual machine code interpreter">
  6. <meta name="generator" content="makeinfo 4.13">
  7. <link title="Top" rel="start" href="index.html#Top">
  8. <link rel="up" href="External-Library-Maintenance.html#External-Library-Maintenance" title="External Library Maintenance">
  9. <link rel="prev" href="Calling-existing-library-functions.html#Calling-existing-library-functions" title="Calling existing library functions">
  10. <link rel="next" href="Working-around-library-misfeatures.html#Working-around-library-misfeatures" title="Working around library misfeatures">
  11. <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
  12. <meta http-equiv="Content-Style-Type" content="text/css">
  13. <style type="text/css"><!--
  14. pre.display { font-family:inherit }
  15. pre.format { font-family:inherit }
  16. pre.smalldisplay { font-family:inherit; font-size:smaller }
  17. pre.smallformat { font-family:inherit; font-size:smaller }
  18. pre.smallexample { font-size:smaller }
  19. pre.smalllisp { font-size:smaller }
  20. span.sc { font-variant:small-caps }
  21. span.roman { font-family:serif; font-weight:normal; }
  22. span.sansserif { font-family:sans-serif; font-weight:normal; }
  23. --></style>
  24. </head>
  25. <body>
  26. <div class="node">
  27. <a name="Implementing-new-library-functions"></a>
  28. <p>
  29. Next:&nbsp;<a rel="next" accesskey="n" href="Working-around-library-misfeatures.html#Working-around-library-misfeatures">Working around library misfeatures</a>,
  30. Previous:&nbsp;<a rel="previous" accesskey="p" href="Calling-existing-library-functions.html#Calling-existing-library-functions">Calling existing library functions</a>,
  31. Up:&nbsp;<a rel="up" accesskey="u" href="External-Library-Maintenance.html#External-Library-Maintenance">External Library Maintenance</a>
  32. <hr>
  33. </div>
  34. <h4 class="subsection">3.9.2 Implementing new library functions</h4>
  35. <p>Adding more external libraries to <code>avram</code> is currently a manual
  36. procedure requiring the attention of a developer conversant with C.
  37. To support a new library called <code>foobar</code>,
  38. these steps need to be followed at a minimum.
  39. <ul>
  40. <li>Create a new file called <samp><span class="file">foobar.h</span></samp> under the <samp><span class="file">avm/</span></samp>
  41. directory in the main source tree whose name doesn't clash with any
  42. <a name="index-header-file-659"></a><a name="index-library-interface-header-file-660"></a>existing file names and preferably doesn't induce any proper prefixes
  43. among them. This file should contain at least these function
  44. declarations.
  45. <pre class="example">
  46. extern list avm_foobar_call (list function_name,list argument,
  47. int *fault);
  48. extern list avm_have_foobar_call (list function_name,int *fault);
  49. extern void avm_initialize_foobar ();
  50. extern void avm_count_foobar ();
  51. </pre>
  52. <p>There should also be the usual preprocessor directives for
  53. <samp><span class="file">include</span></samp> files. The naming convention shown should be followed in
  54. anticipation of automated support for these operations in the future.
  55. <li>Add <samp><span class="file">foobar.h</span></samp> to the list of other header files in
  56. <samp><span class="file">avm/Makefile.am</span></samp>.
  57. <li>Create a new file called <samp><span class="file">foobar.c</span></samp> under the <samp><span class="file">src/</span></samp>
  58. directory whose name doesn't clash with any existing file names to
  59. <a name="index-library-interfac-source-file-661"></a>store most of the library interface code. It can start out with
  60. stubs for the functions declared in <samp><span class="file">foobar.h</span></samp>.
  61. <li>Add <samp><span class="file">foobar.c</span></samp> to the list of other source files in
  62. <samp><span class="file">src/Makefile.am</span></samp>
  63. <li>Execute the following command in the main <samp><span class="file">avram-x.x.x</span></samp>
  64. source directory where the file <samp><span class="file">configure.in</span></samp> is found.
  65. <pre class="example">
  66. aclocal \
  67. &amp;&amp; automake --gnu --add-missing \
  68. &amp;&amp; autoconf
  69. </pre>
  70. <p>This command requires having <code>automake</code> and
  71. <a name="index-automake-662"></a><a name="index-autoconf-663"></a><code>autoconf</code> installed on your system.
  72. <li>Make the following changes to <samp><span class="file">libfuns.c</span></samp>.
  73. <ul>
  74. <li>Add the line <code>#include&lt;avm/foobar.h&gt;</code> after the
  75. <a name="index-include-directives-664"></a>other <code>include</code> directives.
  76. <li>Add the string <code>"foobar"</code> to the end of the array of
  77. <code>libnames</code> in <code>avm_initialize_libfuns</code>.
  78. <li>Add a call to <code>avm_initialize_foobar</code> to the body.
  79. <li>Add a call to <code>avm_count_foobar</code> to the body of
  80. <code>avm_count_libfuns</code>.
  81. <li>Add a case of the form
  82. <pre class="example"> case nn:
  83. return avm_foobar_call(function_name,argument,fault);
  84. </pre>
  85. <p>after the last case in <code>avm_library_call</code>, being
  86. careful not to change the order, and using the same
  87. name as above in the file <samp><span class="file">foobar.h</span></samp>.
  88. <li>Add a case of the form
  89. <pre class="example"> case nn:
  90. looked_up = avm_have_foobar_call(function_name,fault);
  91. break;
  92. </pre>
  93. <p>after the last case in <code>avm_have_library_call</code>, being
  94. careful not to change the order, and using the same name
  95. as above in the file <samp><span class="file">foobar.h</span></samp>.
  96. </ul>
  97. <li>Edit <samp><span class="file">foobar.c</span></samp> and <samp><span class="file">foobar.h</span></samp> to suit,
  98. periodically compiling and testing by executing <code>make</code>.
  99. <li>Package and install at will.
  100. </ul>
  101. <p>The functions shown above have the obvious interpretations, namely
  102. that <code>avm_foobar_call</code> evaluates a library function from the
  103. <code>foobar</code> library, and <code>avm_have_foobar_call</code> tests for a
  104. function's availability. The latter should interpret wild cards as
  105. explained in <a href="Calling-existing-library-functions.html#Calling-existing-library-functions">Calling existing library functions</a>, but should
  106. return only a list of strings for the matching function names rather
  107. than a list of pairs of strings, as the library name is redundant.
  108. The remaining functions are for static initialization and reclamation.
  109. <p>These functions should consist mainly of boilerplate code similar to
  110. the corresponding functions in any of the other library source files,
  111. which should be consulted as examples. The real work would be done by
  112. other functions called by them. These should be statically declared
  113. within the <samp><span class="file">.c</span></samp> source file and normally not listed in the
  114. <samp><span class="file">.h</span></samp> header file unless there is some reason to think they may be
  115. of more general use. Any externally visible functions should have
  116. names beginning with <code>avm_</code> to avoid name clashes.
  117. <p>Some helpful hints are reported below for what they may be worth.
  118. <ul>
  119. <li>The reason for doing this is to leverage off other people's
  120. intelligence, so generally <code>foobar.c</code> should contain only glue
  121. code for library routines developed elsewhere with great skill rather
  122. than reinventing them in some home grown way.
  123. <li>The best numerical software is often written by Fortran
  124. <a name="index-Fortran-665"></a>programmers. Linking to a Fortran library is no problem on GNU systems
  125. provided that all variables are passed by reference and all arrays are
  126. converted to column order (<a href="Type-Conversions.html#Type-Conversions">Type Conversions</a>).
  127. <li>Most C++ programmers have yet to reach a comparable standard, but C++
  128. <a name="index-C_002b_002b-666"></a>libraries can also be linked by running <code>nm</code> on the static
  129. <a name="index-nm-utility-667"></a>library file to find out the real names of the functions and
  130. <a name="index-c_002b_002bfilt-utility-668"></a><code>c++filt</code> to find out which is which. However, there
  131. is no obvious workaround for the use of so called derived classes
  132. by C++ programmers to simulate passing functions as parameters.
  133. <li>Anything worth using can probably be found in the Debian
  134. <a name="index-Debian-669"></a>archive.
  135. <li>Not all libraries are sensible candidates for interfaces to
  136. <code>avram</code>. Typical design flaws are
  137. <ul>
  138. <li>irrepressible debugging messages written to <code>stderr</code> or
  139. <code>stdout</code> that are unfit for end user consumption
  140. <li>deliberately crashing the application if <code>malloc</code> fails
  141. <li>opaque data types with undocumented storage requirements
  142. <li>opaque data types that would be useful to store persistently
  143. but have platform specific binary representations
  144. <li>heavily state dependent
  145. <a name="index-state-dependence-670"></a>semantics
  146. <li>identifiers with clashing names
  147. <li>restrictive
  148. <a name="index-licensing-restrictions-671"></a>licenses
  149. </ul>
  150. <p>Some of these misfeatures have workarounds as explained next in
  151. <a href="Working-around-library-misfeatures.html#Working-around-library-misfeatures">Working around library misfeatures</a>, at least if there's
  152. nothing else wrong with the library.
  153. </ul>
  154. <p>Those who support <code>avram</code> are always prepared to assist in the
  155. dissemination of worthwhile contributed library modules under terms
  156. compatible with <a href="Copying.html#Copying">Copying</a>, and under separate copyrights if
  157. <a name="index-copyright-672"></a>preferred. Contributed modules can be integrated into the official
  158. source tree provided that they meet the following additional
  159. <a name="index-coding-standards-673"></a>guidelines to those above.
  160. <ul>
  161. <li>source code documentation and indentation according to GNU coding
  162. standards (<a href="http://www.gnu.org/prep/standards">http://www.gnu.org/prep/standards</a>)
  163. <li>sufficient stability for a semi-annual release cycle
  164. <li>no run-time or compile-time dependence on any non-free software,
  165. although dynamic loading and client/server interaction are acceptable
  166. <li>portable or at least unbreakable configuration by appropriate use of
  167. <a name="index-autoconf-674"></a><code>autoconf</code> macros and conditional defines
  168. <li>little or no state dependence at the level of the virtual code
  169. <a name="index-state-dependence-675"></a>interface (i.e., pure functions or something like them, except for
  170. <a name="index-random-number-generators-676"></a>random number generators and related applications)
  171. <li>adequate documentation for a section in <a href="External-Libraries.html#External-Libraries">External Libraries</a>
  172. </ul>
  173. </body></html>