~ubuntu-branches/ubuntu/edgy/swig1.3/edgy

« back to all changes in this revision

Viewing changes to Doc/Manual/Chicken.html

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2005-01-10 09:48:52 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20050110094852-axi555axhj1brbwq
Tags: 1.3.22-5ubuntu2
Build using python2.4 and pike7.6. Closes: #4146.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
 
8
8
<body bgcolor="#ffffff">
9
9
 
10
 
<a name="n1"></a><H1>24 SWIG and Chicken</H1>
 
10
<H1><a name="Chicken"></a>17 SWIG and Chicken</H1>
11
11
<!-- INDEX -->
12
12
<ul>
13
 
<li><a href="#n2">Preliminaries</a>
14
 
<ul>
15
 
<li><a href="#n3">Running SWIG in C mode</a>
16
 
<li><a href="#n4">Running SWIG in C++ mode</a>
17
 
</ul>
18
 
<li><a href="#n5">Code Generation</a>
19
 
<ul>
20
 
<li><a href="#n6">Naming Conventions</a>
21
 
<li><a href="#n7">Modules and Prefixes</a>
22
 
<li><a href="#n8">Constants and Variables</a>
23
 
<li><a href="#n9">Functions</a>
24
 
</ul>
25
 
<li><a href="#n10">TinyCLOS</a>
26
 
<li><a href="#n11">Compilation</a>
27
 
<li><a href="#n12">Linkage</a>
28
 
<ul>
29
 
<li><a href="#n13">Customized Interpreter</a>
30
 
</ul>
31
 
<li><a href="#n14">Typemaps</a>
32
 
<li><a href="#n15">Pointers</a>
33
 
<li><a href="#n16">Unsupported features</a>
 
13
<li><a href="#Chicken_nn2">Preliminaries</a>
 
14
<ul>
 
15
<li><a href="#Chicken_nn3">Running SWIG in C mode</a>
 
16
<li><a href="#Chicken_nn4">Running SWIG in C++ mode</a>
 
17
</ul>
 
18
<li><a href="#Chicken_nn5">Code Generation</a>
 
19
<ul>
 
20
<li><a href="#Chicken_nn6">Naming Conventions</a>
 
21
<li><a href="#Chicken_nn7">Modules and Prefixes</a>
 
22
<li><a href="#Chicken_nn8">Constants and Variables</a>
 
23
<li><a href="#Chicken_nn9">Functions</a>
 
24
</ul>
 
25
<li><a href="#Chicken_nn10">TinyCLOS</a>
 
26
<li><a href="#Chicken_nn11">Compilation</a>
 
27
<li><a href="#Chicken_nn12">Linkage</a>
 
28
<ul>
 
29
<li><a href="#Chicken_nn13">Shared library</a>
 
30
<li><a href="#Chicken_nn14">Static binary</a>
 
31
</ul>
 
32
<li><a href="#Chicken_nn15">Typemaps</a>
 
33
<li><a href="#Chicken_nn16">Pointers</a>
 
34
<li><a href="#Chicken_nn17">Unsupported features</a>
34
35
</ul>
35
36
<!-- INDEX -->
36
37
 
41
42
      Scheme-to-C compiler supporting most of the language features as
42
43
      defined in the <i>Revised^5 Report on Scheme</i>.  Its main
43
44
      attributes are that it
 
45
    </p>
44
46
      <ol>
45
47
        <li>generates portable C code</li>
46
48
        <li>includes a customizable interpreter</li>
48
50
        <li>supports full tail-recursion and first-class continuations</li>
49
51
      </ol>
50
52
 
 
53
    <p>
51
54
      When confronted with a large C library, CHICKEN users can use
52
55
      SWIG to generate CHICKEN wrappers for the C library.  However,
53
56
      the real advantages of using SWIG with CHICKEN are its
58
61
 
59
62
    </p>
60
63
 
61
 
<a name="n2"></a><H2>24.1 Preliminaries</H2>
 
64
<H2><a name="Chicken_nn2"></a>17.1 Preliminaries</H2>
62
65
 
63
66
 
64
67
    <p>
65
68
      CHICKEN support was introduced to SWIG in version 1.3.18.  SWIG
66
69
      relies on some recent additions to CHICKEN, which are only
67
70
      present in releases of CHICKEN with version number
68
 
      <strong>greater than or equal to <tt>1.0</tt></strong>.
 
71
      <strong>greater than or equal to <tt>1.40</tt></strong>.
69
72
 
70
 
      <br></br> CHICKEN can be downloaded from <a
71
 
      href="http://www.call-with-current-continuation.org/chicken.html">http://www.call-with-current-continuation.org/chicken.html</a>
 
73
      <br> CHICKEN can be downloaded from <a
 
74
      href="http://www.call-with-current-continuation.org/">http://www.call-with-current-continuation.org/</a>
72
75
 
73
76
      You may want to look at any of the examples in Examples/chicken/
74
77
      or Examples/GIFPlot/Chicken for the basic steps to run SWIG
79
82
 
80
83
    </p>
81
84
 
82
 
<a name="n3"></a><H3>24.1.1 Running SWIG in C mode</H3>
83
 
 
84
 
 
85
 
    <p>
 
85
<H3><a name="Chicken_nn3"></a>17.1.1 Running SWIG in C mode</H3>
 
86
 
 
87
 
 
88
      <p>
86
89
      To run SWIG CHICKEN in C mode, use
87
90
      the -chicken option.
 
91
      </p>
 
92
 
88
93
      <blockquote>
89
94
        <pre>% swig -chicken example.i</pre>
90
95
      </blockquote>
 
96
 
 
97
      <p>
91
98
      To allow the wrapper to take advantage of future CHICKEN code
92
99
      generation improvements, part of the wrapper is direct CHICKEN
93
100
      function calls (<tt>example_wrap.c</tt>) and part is CHICKEN
94
101
      Scheme (<tt>example.scm</tt>).  The basic Scheme code must
95
102
      be compiled to C using your system's CHICKEN compiler.
 
103
      </p>
 
104
 
96
105
      <blockquote>
97
106
        <pre>% chicken example.scm -output-file oexample.c</pre>
98
107
      </blockquote>
 
108
 
 
109
      <p>
99
110
      So for the C mode of SWIG CHICKEN, <tt>example_wrap.c</tt> and
100
111
      <tt>oexample.c</tt> are the files that must be compiled to
101
112
      object files and linked into your project.
102
 
    </p>
103
 
 
104
 
<a name="n4"></a><H3>24.1.2 Running SWIG in C++ mode</H3>
105
 
 
106
 
 
107
 
    <p>
 
113
      </p>
 
114
 
 
115
<H3><a name="Chicken_nn4"></a>17.1.2 Running SWIG in C++ mode</H3>
 
116
 
 
117
 
 
118
      <p>
108
119
      To run SWIG CHICKEN in C++ mode, use
109
120
      the -chicken -c++ option.
 
121
      </p>
 
122
 
110
123
      <blockquote>
111
124
        <pre>% swig -chicken -c++ example.i</pre>
112
125
      </blockquote>
 
126
 
 
127
      <p>
113
128
      This will generate <tt>example_wrap.cxx</tt>,
114
129
      <tt>example.scm</tt>, <tt>example-generic.scm</tt> and
115
130
      <tt>example-clos.scm</tt>.  The basic Scheme code must be
116
131
      compiled to C using your system's CHICKEN compiler.
 
132
      </p>
 
133
 
117
134
      <blockquote>
118
135
        <pre>% chicken example.scm -output-file oexample.c</pre>
119
136
      </blockquote>
 
137
 
 
138
      <p>
120
139
      So for the C++ mode of SWIG CHICKEN, <tt>example_wrap.cxx</tt>
121
140
      and <tt>oexample.c</tt> are the files that must be compiled to
122
141
      object files and linked into your project.
123
142
 
124
 
    </p>
125
 
 
126
 
<a name="n5"></a><H2>24.2 Code Generation</H2>
127
 
 
128
 
 
129
 
<a name="n6"></a><H3>24.2.1 Naming Conventions</H3>
 
143
      </p>
 
144
 
 
145
<H2><a name="Chicken_nn5"></a>17.2 Code Generation</H2>
 
146
 
 
147
 
 
148
<H3><a name="Chicken_nn6"></a>17.2.1 Naming Conventions</H3>
130
149
 
131
150
 
132
151
    <p>
133
152
      Given a C variable, function or constant declaration named
134
153
      <tt>Foo_Bar_to_Foo_Baz</tt>, the declaration will be available
135
154
      in CHICKEN as an identifier ending with
136
 
      <tt>foo-bar->foo-baz</tt>.  That is, an underscore is converted
137
 
      to a dash, '_to_' is converted to an arro, and all characters
138
 
      are sent to lowercase.
 
155
      <tt>Foo-Bar-&gt;Foo-Baz</tt>.  That is, an underscore is converted
 
156
      to a dash and '_to_' is converted to an arrow.
139
157
 
140
 
      <br></br>
 
158
      <br>
141
159
 
142
160
      Additionally, there is a <em>mixed</em> mode that can be
143
161
      specified with the <tt>-mixed</tt> option on the SWIG command
144
162
      line.  In this mode, the above rules apply with the addition
145
163
      that changes in case are indications to SWIG CHICKEN to place a
146
 
      dash in the CHICKEN identifier.  For example, a C declartaion
 
164
      dash in the CHICKEN identifier and the name is converted to lowercase.
 
165
      For example, a C declartaion
147
166
      named <tt>someDeclaration_xyz</tt> will be available as the
148
167
      CHICKEN identifier ending with <tt>some-declaration-xyz</tt>.
149
168
 
150
 
      <br></br>
 
169
      <br>
151
170
 
152
171
      You may control what the CHICKEN identifier will be by using the
153
172
      <tt>%rename</tt> SWIG directive in the SWIG interface file.
154
173
    </p>
155
174
 
156
 
<a name="n7"></a><H3>24.2.2 Modules and Prefixes</H3>
 
175
<H3><a name="Chicken_nn7"></a>17.2.2 Modules and Prefixes</H3>
157
176
 
158
177
 
159
178
    <p>
161
180
      (which has been deprecated); instead, it uses a prefix system.
162
181
      Specifying the module name as 'example' in SWIG CHICKEN can be
163
182
      done using either of:
 
183
    </p>
164
184
      <ul>
165
185
        <li>Placing <tt>%module example</tt> in the SWIG interface
166
186
          file.</li>
168
188
          line.</li>
169
189
      </ul>
170
190
 
 
191
    <p>
171
192
      CHICKEN will be able to access the module using the <tt>(declare
172
193
        (uses <i>modulename</i>))</tt> CHICKEN Scheme form.
173
194
 
174
 
      <br></br>
 
195
      <br>
175
196
 
176
197
      Normally, for a C declaration <tt>Foo_Bar</tt> with a module
177
198
      name of 'example', the corresponding CHICKEN identifier will be
178
 
      <tt>example:foo-bar</tt>.  <strong>The module name and a colon is
 
199
      <tt>example:Foo-Bar</tt>.  <strong>The module name and a colon is
179
200
      prefixed to the CHICKEN identifier</strong> (following normal
180
201
      naming conventions).
181
202
 
182
 
      <br></br>
 
203
      <br>
183
204
 
184
205
      You may explicitly override the prefix with the SWIG command
185
206
      line option <tt>-prefix <i>whateverprefix</i></tt>, or you may
187
208
 
188
209
    </p>
189
210
 
190
 
<a name="n8"></a><H3>24.2.3 Constants and Variables</H3>
 
211
<H3><a name="Chicken_nn8"></a>17.2.3 Constants and Variables</H3>
191
212
 
192
213
 
193
214
    <p>
194
215
      Constants may be created using any of the four constructs in
195
216
      the interface file:
 
217
    </p>
196
218
      <ol>
197
219
        <li><code>#define MYCONSTANT1 ...</code></li>
198
220
        <li><code>%constant int MYCONSTANT2 = ...</code></li>
200
222
        <li><code>enum { MYCONSTANT4  = ... };</code></li>
201
223
      </ol>
202
224
 
 
225
    <p>
203
226
      In all cases, the constants may be accessed from with CHICKEN
204
 
      using the form <tt>(myconstant1)</tt>; that is, the constants
 
227
      using the form <tt>(MYCONSTANT1)</tt>; that is, the constants
205
228
      may be accessed using the read-only parameter form.
206
229
    </p>
207
230
 
212
235
      use <tt>(my-variable)</tt>.
213
236
    </p>
214
237
 
215
 
<a name="n9"></a><H3>24.2.4 Functions</H3>
 
238
<H3><a name="Chicken_nn9"></a>17.2.4 Functions</H3>
216
239
 
217
240
 
218
241
    <p>
231
254
      parameters).
232
255
    </p>
233
256
 
234
 
<a name="n10"></a><H2>24.3 TinyCLOS</H2>
235
 
 
236
 
 
237
 
    <p>
 
257
<H2><a name="Chicken_nn10"></a>17.3 TinyCLOS</H2>
 
258
 
 
259
 
 
260
      <p>
238
261
      The author of TinyCLOS, Gregor Kiczales, describes TinyCLOS as:
 
262
      </p>
239
263
      <blockquote>
240
264
        Tiny CLOS is a Scheme implementation of a `kernelized' CLOS, with a
241
265
        metaobject protocol. The implementation is even simpler than
243
267
        weighing in at around 850 lines of code, including (some)
244
268
        comments and documentation.
245
269
      </blockquote>
 
270
 
 
271
      <p>
246
272
      Almost all good Scheme books describe how to use metaobjects and
247
273
      generic procedures to implement an object-oriented Scheme
248
274
      system.  Please consult a Scheme book if you are unfamiliar
249
275
      with the concept.
 
276
      </p>
250
277
 
251
 
      <br></br>
 
278
      <p>
252
279
 
253
280
      CHICKEN has a modified version of TinyCLOS, which SWIG CHICKEN
254
281
      uses in C++ mode.  SWIG CHICKEN generates a
258
285
      program needs, and <strong>then</strong> <tt>(include
259
286
      "xxx-clos")</tt> all the metaobject (class) macros your program
260
287
      needs.
 
288
      </p>
261
289
 
262
 
      <br></br>
 
290
      <p>
263
291
 
264
292
      SWIG CHICKEN will call the destructor for all TinyCLOS objects
265
293
      that are garbage-collected by CHICKEN.  It also allows access to
267
295
      of any TinyCLOS parameters.  It is best to learn the TinyCLOS
268
296
      system by running the <tt>Examples/chicken/class/</tt> example.
269
297
 
270
 
    </p>
 
298
      </p>
271
299
 
272
 
<a name="n11"></a><H2>24.4 Compilation</H2>
 
300
<H2><a name="Chicken_nn11"></a>17.4 Compilation</H2>
273
301
 
274
302
 
275
303
    <p>
283
311
      much simpler <tt>csc</tt> or <tt>csc.bat</tt>.
284
312
    </p>
285
313
 
286
 
<a name="n12"></a><H2>24.5 Linkage</H2>
 
314
<H2><a name="Chicken_nn12"></a>17.5 Linkage</H2>
287
315
 
288
316
 
289
317
    <p>
296
324
      <tt>-shared</tt> option if you want to create a dynamically
297
325
      loadable module.
298
326
    </p>
299
 
    <p>
300
 
      All the following examples assume that the module is named
301
 
      'example' and the following occurs when run:
302
 
      <blockquote>
303
 
        <pre>% chicken-config -home
304
 
CHICKEN_HOME=/usr/local/share/chicken</pre>
305
 
      </blockquote>
306
 
      Substitute <tt>/usr/local/share/chicken</tt> as appropriate for
307
 
      your platform.
308
 
    </p>
309
 
 
310
 
<a name="n13"></a><H3>24.5.1 Customized Interpreter</H3>
311
 
 
312
 
 
313
 
    <p>
314
 
      We will assume your files are in a directory
315
 
      <tt>/home/jonah/examples</tt>.  Make a file as follows:
316
 
      <pre>
317
 
;; precsi.scm
318
 
(declare (unit precsi))
319
 
(declare (uses example))
320
 
 
321
 
;; any other code you want run before the main interpreter is executed
322
 
      </pre>
323
 
 
324
 
      Run SWIG on your interface file as usual, create the 2 wrapper
325
 
      object files, and then either
326
 
 
327
 
      <blockquote>
328
 
        <pre>
329
 
% cd /usr/local/share/chicken
330
 
% chicken /usr/local/share/chicken/src/csi.scm -optimize-level 3 -quiet \
331
 
        -include-path /usr/local/share/chicken/src \
332
 
        -prologue /usr/local/share/chicken/src/build.scm \
333
 
        -prelude "(declare (uses posix precsi))" \
334
 
        -output-file /home/jonah/examples/csi-example.c
335
 
% cd /home/jonah/examples
336
 
% chicken precsi.scm -optimize-level 3 --explicit-use \
337
 
        -output-file precsi.c
338
 
% gcc precsi.c csi.c <i>wrapper_object_files</i> \
339
 
        `chicken-config -libs` `chicken-config -extra-libs` -o csi-example
340
 
        </pre>
341
 
        <pre>
342
 
      </blockquote>
343
 
      or
344
 
      <blockquote>
345
 
        <pre>% extend-csi precsi -output-file csi-example</pre>
346
 
      </blockquote>
347
 
 
348
 
 
349
 
    </p>
350
 
 
351
 
<a name="n14"></a><H2>24.6 Typemaps</H2>
 
327
 
 
328
<H3><a name="Chicken_nn13"></a>17.5.1 Shared library</H3>
 
329
 
 
330
 
 
331
    <p>
 
332
      The easiest way to use SWIG and CHICKEN is to use the csc compiler
 
333
      wrapper provided by CHICKEN.  Assume you have a SWIG interface file
 
334
      in example.i and the C functions being wrapped are in example_impl.c.
 
335
    </p>
 
336
 
 
337
   <blockquote>
 
338
   <pre>
 
339
   $ swig -chicken example.i
 
340
   $ csc -svk example.scm example_impl.c example_wrap.c
 
341
   $ csi example.so test_script.scm
 
342
   </pre>
 
343
   </blockquote>
 
344
 
 
345
    <p>
 
346
      You must be careful not to name the example_impl.c file example.c because
 
347
      when compiling example.scm, csc compiles that into example.c!
 
348
    </p>
 
349
 
 
350
<H3><a name="Chicken_nn14"></a>17.5.2 Static binary</H3>
 
351
 
 
352
 
 
353
   <p>Again, we can easily use csc to build a binary.</p>
 
354
 
 
355
   <blockquote>
 
356
   <pre>
 
357
   $ swig -chicken example.i
 
358
   $ csc -vk example.scm example_impl.c example_wrap.c test_script.scm -o example
 
359
   $ ./example
 
360
   </pre>
 
361
   </blockquote>
 
362
 
 
363
<H2><a name="Chicken_nn15"></a>17.6 Typemaps</H2>
352
364
 
353
365
 
354
366
    <p>
355
367
      The Chicken module handles all types via typemaps. This information is
356
368
      read from <code>Lib/chicken/typemaps.i</code> and
357
369
      <code>Lib/chicken/chicken.swg</code>.
358
 
 
359
 
      <br></br>
360
 
 
 
370
    </p>
 
371
 
 
372
    <p>
361
373
      Two Chicken-specific typemaps are supported:
362
374
      <code>clos_in</code> and <code>clos_out</code>.  They are for
363
375
      converting TinyCLOS to and from low-level CHICKEN SWIG.  Here is
364
376
      a quick example:
 
377
    </p>
365
378
 
366
379
      <blockquote>
367
380
        <pre>
378
391
 
379
392
      <blockquote>
380
393
        <pre>
381
 
/* TinyCLOS <--> Low-level CHICKEN */
 
394
/* TinyCLOS &lt;--&gt; Low-level CHICKEN */
382
395
 
383
396
%typemap("clos_in") SIMPLE_CLOS_OBJECT * "(slot-ref $input (quote this))"
384
397
%typemap("clos_out") SIMPLE_CLOS_OBJECT * "(make $class (quote this) $1)"
392
405
      are exposed in the SWIG interface file; it "fully knows" only
393
406
      those classes that are not forward declarations.
394
407
 
395
 
      <br></br>
 
408
      <br>
396
409
 
397
410
      A real-world example of the "fully knows" problem is found in
398
411
      the VTK visualization library.  All VTK classes are derived from
470
483
        </pre>
471
484
      </blockquote>
472
485
 
 
486
    <p>
473
487
    After SWIG processes <code>vtkObject.h</code> (from the
474
488
    <tt>%include "vtkObject.h"</tt> line), SWIG will have the complete
475
489
    definition of the <code>vtkObject</code> class because
488
502
 
489
503
    </p>
490
504
 
491
 
<a name="n15"></a><H2>24.7 Pointers</H2>
 
505
<H2><a name="Chicken_nn16"></a>17.7 Pointers</H2>
492
506
 
493
507
 
494
508
    <p>
495
509
      For pointer types, SWIG uses CHICKEN tagged pointers.
496
510
 
497
511
      A tagged pointer is an ordinary CHICKEN pointer with an
498
 
      extra slot for an arbitrary Scheme object.  With SWIG
499
 
      CHICKEN, this Scheme object is a reference to a type-info
 
512
      extra slot for a void *.  With SWIG
 
513
      CHICKEN, this void * is a pointer to a type-info
500
514
      structure.  So each pointer used as input or output from
501
515
      the SWIG-generated CHICKEN wrappers will have type
502
516
      information attached to it.  This will let the wrappers
506
520
    </p>
507
521
    <p>
508
522
      To construct a Scheme object from a C pointer, the wrapper code
509
 
      calls the function <code>swig_new_pointer_obj()</code>, passing
510
 
      stack allocation space of size SWIG_ALLOCSZ_POINTER (see
511
 
      definition in Lib/chicken/chickenrun.swg and also
512
 
      <code>C_alloc()</code> in the CHICKEN documentation) and a
513
 
      pointer to a struct representing the pointer type.
 
523
      calls the function 
 
524
      <code>SWIG_NewPointerObj(void *ptr, swig_type_info *type, int owner)</code>,
 
525
      The function that calls <code>SWIG_NewPointerObj</code> must have a variable declared
 
526
      <code>C_word *known_space = C_alloc(C_SIZEOF_SWIG_POINTER);</code>
 
527
      It is ok to call <code>SWIG_NewPointerObj</code> more than once, 
 
528
      just make sure known_space has enough space for all the created pointers.
514
529
    </p>
515
530
    <p>
516
531
      To get the pointer represented by a CHICKEN tagged pointer, the
517
 
      wrapper code calls the function <code>swig_convert_ptr()</code>,
 
532
      wrapper code calls the function 
 
533
      <code>SWIG_ConvertPtr(C_word s, void **result, swig_type_info *type, int flags)</code>,
518
534
      passing a pointer to a struct representing the expected pointer
519
 
      type.  If the Scheme object passed was not a tagged pointer
520
 
      representing a compatible pointer, a non-zero value is returned.
 
535
      type.
521
536
    </p>
522
 
    <p><strong>Warning:</strong> The Chicken typechecking code seems
523
 
    to be broken (Bug #782468), so that type errors may not be
524
 
    reported.
525
537
 
526
 
<a name="n16"></a><H2>24.8 Unsupported features</H2>
 
538
<H2><a name="Chicken_nn17"></a>17.8 Unsupported features</H2>
527
539
 
528
540
 
529
541
    <ul>
530
542
      <li>No exception handling.</li>
531
543
    </ul>
 
544
 
532
545
  </body>
533
 
</html>
 
 
b'\\ No newline at end of file'
 
546
</html>