File: c-interface.texi

package info (click to toggle)
gcl27 2.7.1-8
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 26,808 kB
  • sloc: lisp: 211,856; ansic: 53,500; sh: 9,333; makefile: 642; tcl: 53; awk: 25
file content (547 lines) | stat: -rwxr-xr-x 11,378 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
@c  Copyright (c) 1994 William Schelter.

@node C Interface, System Definitions, GCL Specific, Top
@chapter C Interface

@menu
* Available Symbols::		
* External Shared Libraries::
@end menu

@node Available Symbols,  , C Interface, C Interface
@section Available Symbols

When GCL is built, those symbols in the system libraries which
are referenced by functions linked in in the list of objects
given in @file{unixport/makefile}, become available for reference
by GCL code.

On some systems it is possible with @code{faslink} to load @file{.o} files
which reference other libraries, but in general this practice is not
portable.


@node External Shared Libraries,  , C Interface, C Interface
@section External Shared Libraries

@deffn {Macro} DEFDLFUN
Package:SYSTEM

Syntax:
@example
(compile (DEFDLFUN @{RETURN NAME &optional LIBNAME) ARGS*))
@end example

GCL specific:  Produces an entry function to function NAME in external shared
library LIBNAME with the specified args/return signature.  This
function must be compiled to run.  When inlined, the function
call collapses to a single reference to a pointer which is
automatically updated to the location of the external function at
image startup.  The connection to the external library is persistent
across image saves and re-executions.  The RETURN and ARGS specifiers
are keywords from the following list corresponding to the accompanying
C programming types:

    :char :short :int :long :float :double

Unsigned versions available are:

    :uchar :ushort :uint

Complex float and complex double types can be access via:

    :fcomplex :dcomplex

Pointers to types available are

     :void* :char* :long* :float* :double*

Example usage:

@example

@verbatim


GCL (GNU Common Lisp)  2.7.0 Thu Oct 26 12:00:01 PM EDT 2023  CLtL1    git: Version_2_7_0pre38
Source License: LGPL(gcl,gmp), GPL(unexec,bfd,xgcl)
Binary License:  GPL due to GPL'ed components: (XGCL READLINE UNEXEC)
Modifications of this banner must retain notice of a compatible license
Dedicated to the memory of W. Schelter

Use (help) to get some basic information on how to use GCL.
Temporary directory for compiler files set to /tmp/

>(do-symbols (s :lib) (print s))

LIB:|libm|
LIB:|libc|
NIL

>(compile (si::defdlfun (:double "cblas_ddot" "libblas.so") :uint :double* :uint :double* :uint))

;; Compiling /tmp/gazonk_653784_0.lsp.
;; End of Pass 1.
;; End of Pass 2.
OPTIMIZE levels: Safety=0 (No runtime error checking), Space=0, Speed=3
;; Finished compiling /tmp/gazonk_653784_0.o.
;; Loading #P"/tmp/gazonk_653784_0.o"
;; start address for /tmp/gazonk_653784_0.o 0x2700860
;; Finished loading #P"/tmp/gazonk_653784_0.o"
#<function 0000000001a4a860>
NIL
NIL

>(do-symbols (s :lib) (print s))

LIB:|libblas|
LIB:|libm|
LIB:|libc|
NIL

>(do-symbols (s 'lib::|libblas|) (unless (find-symbol (symbol-name s) :user) (print s)))

|libblas|:|cblas_ddot|
NIL
NIL

>(setq a (make-array 3 :element-type 'long-float) b (make-array 3 :element-type 'long-float))

#(0.0 0.0 0.0)

>(setf (aref a 1) 1.2 (aref b 1) 2.3)

2.3

>(|libblas|:|cblas_ddot| 3 a 1 b 1)

2.76

>(compile (defun foo (a b) (declare ((vector long-float) a b)) (|libblas|:|cblas_ddot| (length a) a 1 b 1)))

;; Compiling /tmp/gazonk_653784_0.lsp.
;; End of Pass 1.
;; End of Pass 2.
OPTIMIZE levels: Safety=0 (No runtime error checking), Space=0, Speed=3
;; Finished compiling /tmp/gazonk_653784_0.o.
;; Loading #P"/tmp/gazonk_653784_0.o"
;; start address for /tmp/gazonk_653784_0.o 0x2715050
;; Finished loading #P"/tmp/gazonk_653784_0.o"
#<function 0000000001a62140>
NIL
NIL

>(compile (defun bar (a b) (declare (inline |libblas|:|cblas_ddot|) ((vector long-float) a b)) (|libblas|:|cblas_ddot| (length a) a 1 b 1)))

;; Compiling /tmp/gazonk_653784_0.lsp.
;; End of Pass 1.
;; End of Pass 2.
OPTIMIZE levels: Safety=0 (No runtime error checking), Space=0, Speed=3
;; Finished compiling /tmp/gazonk_653784_0.o.
;; Loading #P"/tmp/gazonk_653784_0.o"
;; start address for /tmp/gazonk_653784_0.o 0x2729570
;; Finished loading #P"/tmp/gazonk_653784_0.o"
#<function 0000000001a62740>
NIL
NIL

>(foo a b)

2.76

>(bar a b)

2.76

>(setq compiler::*disassemble-objdump* nil)

NIL

>(disassemble '|libblas|:|cblas_ddot|)

;; Compiling /tmp/gazonk_653784_0.lsp.
;; End of Pass 1.
;; End of Pass 2.
OPTIMIZE levels: Safety=0 (No runtime error checking), Space=0, Speed=3
;; Finished compiling /tmp/gazonk_653784_0.o.

#include "gazonk_653784_0.h"
void init_code(){do_init((void *)VV);}
/*	local entry for function libblas::cblas_ddot	*/

static object LI1__cblas_ddot___gazonk_653784_0(fixnum V6,object V7,fixnum V8,object V9,fixnum V10)
{	 VMB1 VMS1 VMV1
	if(!(((char)tp0(make_fixnum(V6)))==(1))){
	goto T8;
	}
	if(!((0)<=(V6))){
	goto T13;
	}
	if(!((V6)<=((fixnum)4294967295))){
	goto T11;
	}
	goto T12;

	goto T13;
T13:;
	goto T11;

	goto T12;
T12:;
	goto T7;

	goto T11;
T11:;
	goto T6;

	goto T8;
T8:;
	goto T6;

	goto T7;
T7:;
	goto T5;

	goto T6;
T6:;
	goto T3;

	goto T5;
T5:;
	goto T2;

	goto T3;
T3:;
	V11= CMPmake_fixnum(V6);
	V6= fixint((fcall.argd=4,/* SYSTEM::CHECK-TYPE-SYMBOL */(object )(*LnkLI2)(((object)VV[1]),(V11),((object)VV[2]),Cnil)));
	goto T2;
T2:;
	switch(tp6(V7)){
	case 428:
	goto T27;
T27:;
	case 492:
	goto T28;
T28:;
	goto T25;

	default:
	goto T29;
T29:;
	goto T24;

	goto T24;
	}
	goto T24;

	goto T25;
T25:;
	goto T23;

	goto T24;
T24:;
	goto T22;

	goto T23;
T23:;
	goto T21;

	goto T22;
T22:;
	goto T19;

	goto T21;
T21:;
	goto T18;

	goto T19;
T19:;
	V7= (fcall.argd=4,/* SYSTEM::CHECK-TYPE-SYMBOL */(object )(*LnkLI2)(((object)VV[3]),(V7),((object)VV[4]),Cnil));
	goto T18;
T18:;
	if(!(((char)tp0(make_fixnum(V8)))==(1))){
	goto T39;
	}
	if(!((0)<=(V8))){
	goto T44;
	}
	if(!((V8)<=((fixnum)4294967295))){
	goto T42;
	}
	goto T43;

	goto T44;
T44:;
	goto T42;

	goto T43;
T43:;
	goto T38;

	goto T42;
T42:;
	goto T37;

	goto T39;
T39:;
	goto T37;

	goto T38;
T38:;
	goto T36;

	goto T37;
T37:;
	goto T34;

	goto T36;
T36:;
	goto T33;

	goto T34;
T34:;
	V12= CMPmake_fixnum(V8);
	V8= fixint((fcall.argd=4,/* SYSTEM::CHECK-TYPE-SYMBOL */(object )(*LnkLI2)(((object)VV[5]),(V12),((object)VV[2]),Cnil)));
	goto T33;
T33:;
	switch(tp6(V9)){
	case 428:
	goto T58;
T58:;
	case 492:
	goto T59;
T59:;
	goto T56;

	default:
	goto T60;
T60:;
	goto T55;

	goto T55;
	}
	goto T55;

	goto T56;
T56:;
	goto T54;

	goto T55;
T55:;
	goto T53;

	goto T54;
T54:;
	goto T52;

	goto T53;
T53:;
	goto T50;

	goto T52;
T52:;
	goto T49;

	goto T50;
T50:;
	V9= (fcall.argd=4,/* SYSTEM::CHECK-TYPE-SYMBOL */(object )(*LnkLI2)(((object)VV[6]),(V9),((object)VV[4]),Cnil));
	goto T49;
T49:;
	if(!(((char)tp0(make_fixnum(V10)))==(1))){
	goto T70;
	}
	if(!((0)<=(V10))){
	goto T75;
	}
	if(!((V10)<=((fixnum)4294967295))){
	goto T73;
	}
	goto T74;

	goto T75;
T75:;
	goto T73;

	goto T74;
T74:;
	goto T69;

	goto T73;
T73:;
	goto T68;

	goto T70;
T70:;
	goto T68;

	goto T69;
T69:;
	goto T67;

	goto T68;
T68:;
	goto T65;

	goto T67;
T67:;
	goto T64;

	goto T65;
T65:;
	V13= CMPmake_fixnum(V10);
	V10= fixint((fcall.argd=4,/* SYSTEM::CHECK-TYPE-SYMBOL */(object )(*LnkLI2)(((object)VV[7]),(V13),((object)VV[2]),Cnil)));
	goto T64;
T64:;
	{object V14 = make_longfloat(((double(*)(uint,double*,uint,double*,uint))(dlcblas_ddot))((uint)V6,(double*)V7->v.v_self,(uint)V8,(double*)V9->v.v_self,(uint)V10));
	VMR1(V14);}
}
static object  LnkTLI2(object first,...){object V1;va_list ap;va_start(ap,first);V1=(object )call_proc_new(((object)VV[0]),0,262147,(void **)(void *)&LnkLI2,0,first,ap);va_end(ap);return V1;} /* SYSTEM::CHECK-TYPE-SYMBOL */
(9 (MAPC 'EVAL *COMPILER-COMPILE-DATA*))
static object LI1__cblas_ddot___gazonk_653784_0(fixnum V6,object V7,fixnum V8,object V9,fixnum V10)
;
static void *dlcblas_ddot;
#define VMB1  object  V13 ,V12 ,V11;
#define VMS1
#define VMV1
#define VMRV1(a_,b_) return((object )a_);
#define VMR1(a_) VMRV1(a_,0);
#define VM1 0
static void * VVi[9]={
#define Cdata VV[8]
(void *)(&dlcblas_ddot),
(void *)(LI1__cblas_ddot___gazonk_653784_0)
};
#define VV (VVi)
static object  LnkTLI2(object,...);
static object  (*LnkLI2)() = (object (*)()) LnkTLI2;
NIL

>(disassemble 'foo)

;; Compiling /tmp/gazonk_653784_0.lsp.
;; End of Pass 1.
;; End of Pass 2.
OPTIMIZE levels: Safety=0 (No runtime error checking), Space=0, Speed=3
;; Finished compiling /tmp/gazonk_653784_0.o.

#include "gazonk_653784_0.h"
void init_code(){do_init((void *)VV);}
/*	local entry for function COMMON-LISP-USER::FOO	*/

static object LI1__FOO___gazonk_653784_0(object V3,object V4)
{	 VMB1 VMS1 VMV1
	if(!(((char)((fixnum)((uchar*)((fixnum)V3))[(fixnum)2]&(fixnum)1))==(0))){
	goto T5;
	}
	goto T2;

	goto T5;
T5:;
	V5= ((fixnum)((uint*)((fixnum)V3))[(fixnum)4]&268435455);
	goto T1;

	goto T2;
T2:;
	V5= (((fixnum)((uint*)((fixnum)V3))[(fixnum)1]>>(fixnum)3)&268435455);
	goto T1;
T1:;
	{object V6 = (/* libblas::cblas_ddot */(object )(*LnkLI2)(V5,(V3),(fixnum)1,(V4),(fixnum)1));
	VMR1(V6);}
}
static object  LnkTLI2(object first,...){object V1;va_list ap;va_start(ap,first);V1=(object )call_proc_new(((object)VV[0]),0,5,(void **)(void *)&LnkLI2,1092,first,ap);va_end(ap);return V1;} /* libblas::cblas_ddot */
(2 (MAPC 'EVAL *COMPILER-COMPILE-DATA*))
static object LI1__FOO___gazonk_653784_0(object V3,object V4)
;
#define VMB1  fixnum  V5;
#define VMS1
#define VMV1
#define VMRV1(a_,b_) return((object )a_);
#define VMR1(a_) VMRV1(a_,0);
#define VM1 0
static void * VVi[2]={
#define Cdata VV[1]
(void *)(LI1__FOO___gazonk_653784_0)
};
#define VV (VVi)
static object  LnkTLI2(object,...);
static object  (*LnkLI2)() = (object (*)()) LnkTLI2;
NIL

>(disassemble 'bar)

;; Compiling /tmp/gazonk_653784_0.lsp.
;; End of Pass 1.
;; End of Pass 2.
OPTIMIZE levels: Safety=0 (No runtime error checking), Space=0, Speed=3
;; Finished compiling /tmp/gazonk_653784_0.o.

#include "gazonk_653784_0.h"
void init_code(){do_init((void *)VV);}
/*	local entry for function COMMON-LISP-USER::BAR	*/

static object LI1__BAR___gazonk_653784_0(object V3,object V4)
{	 VMB1 VMS1 VMV1
	{fixnum V5;
	if(!(((char)((fixnum)((uchar*)((fixnum)V3))[(fixnum)2]&(fixnum)1))==(0))){
	goto T5;
	}
	goto T2;

	goto T5;
T5:;
	V5= ((fixnum)((uint*)((fixnum)V3))[(fixnum)4]&268435455);
	goto T1;

	goto T2;
T2:;
	V5= (((fixnum)((uint*)((fixnum)V3))[(fixnum)1]>>(fixnum)3)&268435455);
	goto T1;
T1:;
	{object V6 = make_longfloat(((double(*)(uint,double*,uint,double*,uint))(dlcblas_ddot))((uint)V5,(double*)V3->v.v_self,(uint)1,(double*)V4->v.v_self,(uint)1));
	VMR1(V6);}}
}
(2 (MAPC 'EVAL *COMPILER-COMPILE-DATA*))
static object LI1__BAR___gazonk_653784_0(object V3,object V4)
;
static void *dlcblas_ddot;
#define VMB1
#define VMS1
#define VMV1
#define VMRV1(a_,b_) return((object )a_);
#define VMR1(a_) VMRV1(a_,0);
#define VM1 0
static void * VVi[2]={
#define Cdata VV[1]
(void *)(&dlcblas_ddot),
(void *)(LI1__BAR___gazonk_653784_0)
};
#define VV (VVi)
NIL

>(si::save-system "ff")
$ ./ff
GCL (GNU Common Lisp)  2.7.0 Thu Oct 26 12:00:01 PM EDT 2023  CLtL1    git: Version_2_7_0pre38
Source License: LGPL(gcl,gmp), GPL(unexec,bfd,xgcl)
Binary License:  GPL due to GPL'ed components: (XGCL READLINE UNEXEC)
Modifications of this banner must retain notice of a compatible license
Dedicated to the memory of W. Schelter

Use (help) to get some basic information on how to use GCL.
Temporary directory for compiler files set to /tmp/

>(foo a b)

2.76

>(bar a b)

2.76

>

@end verbatim

@end example

@end deffn