| 1 |
<!DOCTYPE html> |
| 2 |
<html><head><title>Appendix 2: Intgen</title> |
| 3 |
<link rel="stylesheet" type="text/css" href="nyquiststyle.css"> |
| 4 |
<link rel="icon" href="nyquist-icon.png" /> |
| 5 |
<link rel="shortcut icon" href="nyquist-icon.png" /> |
| 6 |
</head> |
| 7 |
<body bgcolor="ffffff"> |
| 8 |
<a href = "part17.html">Previous Section</a> | <a href = "part19.html">Next Section</a> | <a href = "title.html#toc">Table of Contents</a> | <a href = "indx.html">Index</a> | <a href = "title.html">Title Page</a> |
| 9 |
<hr> |
| 10 |
<a name = "216"><h2>Appendix 2: Intgen</h2></a><a name="index1460"></a>This documentation describes Intgen, a program for generating XLISP to C |
| 11 |
<p>interfaces. Intgen works by scanning <code>.h</code> files with special comments in |
| 12 |
them. Intgen builds stubs that implement XLISP SUBR's. When the SUBR is |
| 13 |
called, arguments are type-checked and passed to the C routine declared in |
| 14 |
the <code>.h</code> file. Results are converted into the appropriate XLISP type and |
| 15 |
returned to the calling XLISP function. Intgen lets you add C functions |
| 16 |
into the XLISP environment with very little effort.</p> |
| 17 |
<a name = "217"><h3>Overview</h3></a> |
| 18 |
<p>The interface generator will take as command-line input: |
| 19 |
<ul> |
| 20 |
<li> |
| 21 |
the name of the <code>.c</code> file to generate (do not include the <code>.c</code> extension; e.g. write |
| 22 |
<code>xlexten</code>, not <code>xlexten.c</code>);</li> |
| 23 |
<li>a list of <code>.h</code> files. |
| 24 |
</li></ul> |
| 25 |
Alternatively, the command line may specify a command file from which to read file names. The command file name should be preceded by "@", for example: |
| 26 |
</p> |
| 27 |
<pre> |
| 28 |
intgen @sndfns.cl |
| 29 |
</pre> |
| 30 |
|
| 31 |
<p> |
| 32 |
reads sndfns.cl to get the command-line input. Only one level of indirection is allowed.</p> |
| 33 |
|
| 34 |
<p>The output is: |
| 35 |
<ul> |
| 36 |
<li> |
| 37 |
a single <code>.c</code> file with one SUBR defined for each designated |
| 38 |
routine in a <code>.h</code> file. </li> |
| 39 |
<li>a <code>.h</code> file that declares each new C routine. E.g. if the <code>.c</code> file is named <code>xlexten.c</code>, this file will be named <code>xlextendefs.h</code>;</li> |
| 40 |
<li>a <code>.h</code> file that extends the SUBR table used by Xlisp. E.g. if the <code>.c</code> file is named <code>xlexten.c</code>, then this file is named <code>xlextenptrs.h</code>;</li> |
| 41 |
<li>a <code>.lsp</code> file with lisp initialization expressions copied from the |
| 42 |
<code>.h</code> |
| 43 |
files. This file is only generated if at least one initialization expression is encountered. |
| 44 |
</li></ul></p> |
| 45 |
|
| 46 |
<p>For example, the command line |
| 47 |
</p> |
| 48 |
<pre> |
| 49 |
intgen seint ~setypes.h access.h |
| 50 |
</pre> |
| 51 |
|
| 52 |
<p> |
| 53 |
generates the file <code>seint.c</code>, using declarations in <code>setypes.h</code> |
| 54 |
and <code>access.h</code>. Normally, the <code>.h</code> files are included by the |
| 55 |
generated file using <code>#include</code> commands. A <code>~</code> before a file |
| 56 |
means do not include the <code>.h</code> file. (This may be useful if you extend |
| 57 |
<code>xlisp.h</code>, which will be included anyway). Also generated will be |
| 58 |
<code>setintdefs.h</code> and <code>seintptrs.h</code>.</p> |
| 59 |
<a name = "218"><h4>Extending Xlisp</h4></a><a name="index1461"></a> |
| 60 |
<p>Any number of <code>.h</code> files may be named on the command line to Intgen, |
| 61 |
and Intgen will make a single <code>.c</code> file with interface routines for all |
| 62 |
of the <code>.h</code> files. On the other hand, it is not necessary to put all |
| 63 |
of the extensions to Xlisp into a single interface file. For example, you |
| 64 |
can run Intgen once to build interfaces to window manager routines, and |
| 65 |
again to build interfaces to a new data type. Both interfaces can be linked |
| 66 |
into Xlisp.</p> |
| 67 |
|
| 68 |
<p>To use the generated files, you must compile the <code>.c</code> files and link |
| 69 |
them with all of the standard Xlisp object files. In addition, you must |
| 70 |
edit the file <code>localdefs.h</code> to contain an <code>#include</code> for each |
| 71 |
<code>*defs.h</code> file, and edit the file <code>localptrs.h</code> to include each |
| 72 |
<code>*ptrs.h</code> file. For example, suppose you run Intgen to build |
| 73 |
<code>soundint.c</code>, <code>fugueint.c</code>, and <code>tableint.c</code>. You would then |
| 74 |
edit <code>localdefs.h</code> to contain the following: |
| 75 |
</p> |
| 76 |
<pre> |
| 77 |
#include "soundintdefs.h" |
| 78 |
#include "fugueintdefs.h" |
| 79 |
#include "tableintdefs.h" |
| 80 |
</pre> |
| 81 |
|
| 82 |
<p> |
| 83 |
and edit <code>localptrs.h</code> to contain: |
| 84 |
</p> |
| 85 |
<pre> |
| 86 |
#include "soundintptrs.h" |
| 87 |
#include "fugueintptrs.h" |
| 88 |
#include "tableintptrs.h" |
| 89 |
</pre> |
| 90 |
|
| 91 |
<p> |
| 92 |
These <code>localdefs.h</code> and <code>localptrs.h</code> files are in turn included |
| 93 |
by <code>xlftab.c</code> which is where Xlisp builds a table of SUBRs.</p> |
| 94 |
|
| 95 |
<p>To summarize, building an interface requires just a few simple steps: |
| 96 |
<ul> |
| 97 |
<li> |
| 98 |
Write C code to be called by Xlisp interface routines. This C code does the |
| 99 |
real work, and in most cases is completely independent of Xlisp.</li> |
| 100 |
<li>Add comments to <code>.h</code> files to tell Intgen which routines to build |
| 101 |
interfaces to, and to specify the types of the arguments.</li> |
| 102 |
<li>Run Intgen to build interface routines.</li> |
| 103 |
<li>Edit <code>localptrs.h</code> and <code>localdefs.h</code> to include generated |
| 104 |
<code>.h</code> files.</li> |
| 105 |
<li>Compile and link Xlisp, including the new C code. |
| 106 |
</li></ul></p> |
| 107 |
<a name = "219"><h3>Header file format</h3></a><a name="index1462"></a> |
| 108 |
<p>Each routine to be interfaced with Xlisp must be declared as |
| 109 |
follows: |
| 110 |
</p> |
| 111 |
<pre> |
| 112 |
<i>type-name</i> <i>routine-name</i>(); /* LISP: (<i>func-name</i> <i>type<sub>1</sub></i> <i>type<sub>2</sub></i> ...) */ |
| 113 |
</pre> |
| 114 |
|
| 115 |
<p> |
| 116 |
The comment may be on the line following the declaration, but the |
| 117 |
declaration and the comment must each be on no more than one line. |
| 118 |
The characters <code>LISP:</code> at the beginning of the comment mark routines |
| 119 |
to put in the interface. The comment also gives the |
| 120 |
type and number of arguments. The function, when accessed from lisp will |
| 121 |
be known as <i>func-name</i>, which need not bear any relationship to |
| 122 |
<i>routine-name</i>. By convention, underscores in the C <i>routine-name</i> |
| 123 |
should be converted to dashes in <i>func-name</i>, and <i>func-name</i> should be in |
| 124 |
all capitals. None of this is enforced or automated though.</p> |
| 125 |
|
| 126 |
<p>Legal type_names are: |
| 127 |
<dl> |
| 128 |
<dt> |
| 129 |
<code>LVAL</code></dt> |
| 130 |
<dd>returns an Xlisp datum.</p> |
| 131 |
<dt><code>atom_type</code></dt> |
| 132 |
<dd>equivalent to <code>LVAL</code>, but the result is expected to |
| 133 |
be an atom.<br><br> |
| 134 |
<dt><code>value_type</code></dt> |
| 135 |
<dd>a value as used in Dannenberg's score editor.<br><br> |
| 136 |
<dt><code>event_type</code></dt> |
| 137 |
<dd>an event as used in Dannenberg's score editor.<br><br> |
| 138 |
<dt><code>int</code></dt> |
| 139 |
<dd>interface will convert int to Xlisp <code>FIXNUM</code>.<br><br> |
| 140 |
<dt><code>boolean</code></dt> |
| 141 |
<dd>interface will convert int to <code> T</code> or <code>nil</code>.<br><br> |
| 142 |
<dt><code>float</code> or <code>double</code></dt> |
| 143 |
<dd>interface converts to <code>FLONUM</code>.<br><br> |
| 144 |
<dt><code>char *</code> or <code>string</code> or <code>string_type</code></dt> |
| 145 |
<dd>interface converts to <code>STRING</code>. The result string will be copied into the XLISP heap.<br><br> |
| 146 |
<dt> void</dt> |
| 147 |
<dd>interface will return <code>nil</code>. |
| 148 |
</dd></dl> |
| 149 |
<p>It is easy to extend this list. Any unrecognized type will |
| 150 |
be coerced to an <code>int</code> and then returned as a <code>FIXNUM</code>, and a warning will be |
| 151 |
issued.</p> |
| 152 |
|
| 153 |
<p>The “<code>*</code>” after char must be followed by <i>routine-name</i> with |
| 154 |
no intervening space.</p> |
| 155 |
|
| 156 |
<p>Parameter types may be any of the following: |
| 157 |
<dl> |
| 158 |
<dt> |
| 159 |
<code>FIXNUM</code></dt> |
| 160 |
<dd>C routine expects an int.</p> |
| 161 |
<dt><code>FLONUM</code> or <code>FLOAT</code></dt> |
| 162 |
<dd>C routine expects a <code>double</code>.<br><br> |
| 163 |
<dt><code>STRING</code></dt> |
| 164 |
<dd>C routine expects <code>char *</code>, the string is not copied.<br><br> |
| 165 |
<dt><code>VALUE</code></dt> |
| 166 |
<dd>C routine expects a <code>value_type</code>. (Not applicable to Fugue.)<br><br> |
| 167 |
<dt><code>EVENT</code></dt> |
| 168 |
<dd>C routine expects an <code>event_type</code>. (Not applicable to Fugue.)<br><br> |
| 169 |
<dt><code>ANY</code></dt> |
| 170 |
<dd>C routine expects <code>LVAL</code>.<br><br> |
| 171 |
<dt><code>ATOM</code></dt> |
| 172 |
<dd>C routine expects <code>LVAL</code> which is a lisp atom.<br><br> |
| 173 |
<dt><code>FILE</code></dt> |
| 174 |
<dd>C routine expects <code>FILE *</code>.<br><br> |
| 175 |
<dt><code>SOUND</code></dt> |
| 176 |
<dd>C routine expects a <code>SoundPtr</code>. |
| 177 |
</dd></dl> |
| 178 |
Any of these may be followed by “<code>*</code>”: <code>FIXNUM*</code>, <code>FLONUM*</code>, <code>STRING*</code>, <code>ANY*</code>, <code>FILE*</code>, |
| 179 |
indicating C routine expects <code>int *</code>, <code>double *</code>, <code>char **</code>, <code>LVAL *</code>, or <code>FILE **</code> . |
| 180 |
This is basically a mechanism for returning more than one value, <i>not</i> |
| 181 |
a mechanism for clobbering XLisp values. In this spirit, the interface |
| 182 |
copies the value (an <code>int</code>, <code>double</code>, <code>char *</code>, <code>LVAL</code>, or <code>FILE *</code>) to a local variable |
| 183 |
and passes the address of that variable to the C routine. On return, |
| 184 |
a list of resulting “<code>*</code>” parameters is constructed and bound to the |
| 185 |
global XLisp symbol <code>*RSLT*<a name="index1463"></a></code>. (Strings are copied.) If the C routine is void, then the result list is also returned by the corresponding XLisp function. |
| 186 |
<p>Note 1: this does not support C routines like strcpy that modify strings, |
| 187 |
because the C routine gets a pointer to the string in the XLisp heap. |
| 188 |
However, you can always add an intermediate routine that allocates |
| 189 |
space and then calls <code>strcpy</code>, or whatever.</p> |
| 190 |
|
| 191 |
<p>Note 2: it follows that a new XLisp <code>STRING</code> will be created for each <code>STRING*</code> parameter.</p> |
| 192 |
|
| 193 |
<p>Note 3: putting results on a (global!) symbol seems a bit unstructured, but note that one could write a multiple-value binding macro that hides this ugliness from the user if desired. In practice, I find that pulling the extra result values from <code>*RSLT*</code> when needed is perfectly acceptable.</p> |
| 194 |
|
| 195 |
<p>For parameters that are result values only, the character “<code>^</code>” may |
| 196 |
be substituted for “<code>*</code>”. In this case, the parameter is <i>not</i> to be passed in the XLisp calling site. |
| 197 |
However, the address of an initialized |
| 198 |
local variable of the given type is passed to the corresponding |
| 199 |
C function, and the resulting value is passed back through <code>*RSLT*</code> as |
| 200 |
ordinary result parameter as described above. |
| 201 |
The local variables are initialized to zero or <code>NULL</code>.</p> |
| 202 |
<a name = "220"><h3>Using #define'd macros</h3></a><a name="index1464"></a> |
| 203 |
<p>If a comment of the form: |
| 204 |
</p> |
| 205 |
<pre> |
| 206 |
/* LISP: <i>type-name</i> (<i>routine-name-2</i> <i>type-1</i> <i>type-2</i> ...) */ |
| 207 |
</pre> |
| 208 |
|
| 209 |
<p> |
| 210 |
appears on a line by itself and there was a <code>#define</code> on the previous |
| 211 |
line, then the preceding <code>#define</code> is treated as a C routine, e.g. |
| 212 |
</p> |
| 213 |
<pre> |
| 214 |
#define leftshift(val, count) ((val) << (count)) |
| 215 |
/* LISP: int (LOGSHIFT INT INT) */ |
| 216 |
</pre> |
| 217 |
|
| 218 |
<p> |
| 219 |
will implement the LeLisp function <code>LOGSHIFT</code>.</p> |
| 220 |
|
| 221 |
<p>The <i>type-name</i> following “<code>LISP:</code>” should have no spaces, e.g. use <code>ANY*</code>, not |
| 222 |
<code>ANY *</code>.</p> |
| 223 |
<a name = "221"><h3>Lisp Include Files</h3></a><a name="index1465"></a> |
| 224 |
<p>Include files often define constants that we would like to have around |
| 225 |
in the Lisp world, but which are easier to initialize just by loading |
| 226 |
a text file. Therefore, a comment of the form: |
| 227 |
</p> |
| 228 |
<pre> |
| 229 |
/* LISP-SRC: (any lisp expression) */ |
| 230 |
</pre> |
| 231 |
|
| 232 |
<p> |
| 233 |
will cause Intgen to open a file <i>name</i><code>.lsp</code> and append |
| 234 |
</p> |
| 235 |
<pre> |
| 236 |
(any lisp expression) |
| 237 |
</pre> |
| 238 |
|
| 239 |
<p> |
| 240 |
to <i>name</i><code>.lsp</code>, where <i>name</i> is the interface name passed on the command line. If none of the include files examined have comments of |
| 241 |
this form, then no <i>name</i><code>.lsp</code> file is generated. |
| 242 |
<b><i>Note:</i></b> <i>the LISP-SRC comment must be on a new line.</i></p> |
| 243 |
<a name = "222"><h3>Example</h3></a> |
| 244 |
<p>This file was used for testing Intgen. It uses a trick (ok, it's a hack) to interface |
| 245 |
to a standard library macro (tolower). Since tolower is already |
| 246 |
defined, the macro ToLower is defined just to give Intgen a name |
| 247 |
to call. Two other routines, strlen and tough, are interfaced as |
| 248 |
well. |
| 249 |
</p> |
| 250 |
<pre> |
| 251 |
/* igtest.h -- test interface for intgen */ |
| 252 |
|
| 253 |
#define ToLower(c) tolower(c) |
| 254 |
/* LISP: int (TOLOWER FIXNUM) */ |
| 255 |
|
| 256 |
int strlen(); /* LISP: (STRLEN STRING) */ |
| 257 |
|
| 258 |
void tough(); |
| 259 |
/* LISP: (TOUGH FIXNUM* FLONUM* STRING ANY FIXNUM) */ |
| 260 |
</pre> |
| 261 |
|
| 262 |
<p></p> |
| 263 |
<a name = "223"><h3>More Details</h3></a> |
| 264 |
<p>Intgen has some compiler switches to enable/disable the use of certain types, including |
| 265 |
<code>VALUE</code> and <code>EVENT</code> types used by Dannenberg's score editing work, the <code>SOUND</code> type used by Fugue, and <code>DEXT</code> and <code>SEXT</code> types added for Dale Amon. |
| 266 |
Enabling all of these is not likely to cause problems, |
| 267 |
and the chances of an accidental use of these types getting through |
| 268 |
the compiler and linker seems very small. |
| 269 |
</p> |
| 270 |
<hr> |
| 271 |
<a href = "part17.html">Previous Section</a> | <a href = "part19.html">Next Section</a> | <a href = "title.html#toc">Table of Contents</a> | <a href = "indx.html">Index</a> | <a href = "title.html">Title Page</a> |
| 272 |
</body></html> |