Import unbound variable reports in the VM.
[bpt/guile.git] / libguile / foreign.h
CommitLineData
f353687c
LC
1#ifndef SCM_FOREIGN_H
2#define SCM_FOREIGN_H
3
e2c2a699
AW
4/* Copyright (C) 2010 Free Software Foundation, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 3 of
9 * the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301 USA
20 */
21
f353687c 22#include "libguile/__scm.h"
e2c2a699 23
52fd9639
AW
24/* A foreign value is some value that exists outside of Guile. It is represented
25 by a cell whose second word is a pointer. The first word has the
26 scm_tc7_foreign typecode and type of the aliased (pointed-to) value in its
27 lower 16 bits.
28
29 There are numeric types, like uint32 and float, and there is a "generic
30 pointer" type, void. Void pointers also have a length associated with them,
31 in the high bits of the first word of the SCM object, but since they really
32 are pointers out into the wild wooly world of C, perhaps we don't actually
33 know how much memory they take up. In that, most general case, the "len"
34 will be stored as 0.
35
36 The basic idea is that we can help the programmer to avoid cutting herself,
37 but we won't take away her knives.
38*/
e2c2a699
AW
39typedef enum
40 {
52fd9639 41 SCM_FOREIGN_TYPE_VOID, /* a pointer out into the wilderness */
e2c2a699
AW
42 SCM_FOREIGN_TYPE_FLOAT,
43 SCM_FOREIGN_TYPE_DOUBLE,
44 SCM_FOREIGN_TYPE_UINT8,
45 SCM_FOREIGN_TYPE_INT8,
46 SCM_FOREIGN_TYPE_UINT16,
47 SCM_FOREIGN_TYPE_INT16,
48 SCM_FOREIGN_TYPE_UINT32,
49 SCM_FOREIGN_TYPE_INT32,
50 SCM_FOREIGN_TYPE_UINT64,
51 SCM_FOREIGN_TYPE_INT64,
52fd9639 52 SCM_FOREIGN_TYPE_LAST = SCM_FOREIGN_TYPE_INT64
e2c2a699
AW
53 } scm_t_foreign_type;
54
55
56typedef void (*scm_t_foreign_finalizer) (void *);
57
58#define SCM_FOREIGN_P(x) \
59 (!SCM_IMP (x) && SCM_TYP7(x) == scm_tc7_foreign)
60#define SCM_VALIDATE_FOREIGN(pos, x) \
61 SCM_MAKE_VALIDATE (pos, x, FOREIGN_P)
62#define SCM_FOREIGN_TYPE(x) \
63 ((scm_t_foreign_type)((SCM_CELL_WORD_0 (x) >> 8)&0xff))
52fd9639
AW
64#define SCM_FOREIGN_POINTER(x, ctype) \
65 ((ctype*)SCM_CELL_WORD_1 (x))
66#define SCM_FOREIGN_VALUE_REF(x, ctype) \
67 (*SCM_FOREIGN_POINTER (x, ctype))
68#define SCM_FOREIGN_VALUE_SET(x, ctype, val) \
69 (*SCM_FOREIGN_POINTER (x, ctype) = (val))
70#define SCM_FOREIGN_HAS_FINALIZER(x) \
71 ((SCM_CELL_WORD_0 (x) >> 16) & 0x1)
72#define SCM_FOREIGN_LEN(x) \
73 ((size_t)(SCM_CELL_WORD_0 (x) >> 17))
e2c2a699
AW
74
75#define SCM_FOREIGN_TYPED_P(x, type) \
76 (SCM_FOREIGN_P (x) && SCM_FOREIGN_TYPE (x) == SCM_FOREIGN_TYPE_##type)
77#define SCM_VALIDATE_FOREIGN_TYPED(pos, x, type) \
78 do { \
79 SCM_ASSERT_TYPE (SCM_FOREIGN_TYPED_P (x, type), x, pos, FUNC_NAME, \
80 "FOREIGN_"#type"_P"); \
81 } while (0)
82
52fd9639
AW
83#define SCM_FOREIGN_VALUE_P(x) \
84 (SCM_FOREIGN_P (x) && SCM_FOREIGN_TYPE (x) != SCM_FOREIGN_TYPE_VOID)
85#define SCM_VALIDATE_FOREIGN_VALUE(pos, x) \
86 SCM_MAKE_VALIDATE (pos, x, FOREIGN_VALUE_P)
87
88SCM_API SCM scm_take_foreign_pointer (scm_t_foreign_type type, void *ptr,
89 size_t len,
90 scm_t_foreign_finalizer finalizer);
91
9a396cbd
AW
92SCM_API SCM scm_alignof (SCM type);
93SCM_API SCM scm_sizeof (SCM type);
52fd9639 94SCM_API SCM scm_foreign_type (SCM foreign);
20aafae2
AW
95SCM_API SCM scm_foreign_ref (SCM foreign);
96SCM_API SCM scm_foreign_set_x (SCM foreign, SCM val);
97SCM_API SCM scm_foreign_to_bytevector (SCM foreign, SCM type,
98 SCM offset, SCM len);
3435f3c0 99SCM_API SCM scm_foreign_set_finalizer_x (SCM foreign, SCM finalizer);
20aafae2 100SCM_API SCM scm_bytevector_to_foreign (SCM bv, SCM offset, SCM len);
e2c2a699
AW
101
102SCM_INTERNAL void scm_i_foreign_print (SCM foreign, SCM port,
103 scm_print_state *pstate);
d8b04f04
AW
104
105\f
106
107/* Foreign functions */
108
109/* The goal is to make it so that calling a foreign function doesn't cause any
110 heap allocation. That means we need native Scheme formats for all kinds of
111 arguments.
112
113 For "value" types like s64 or f32, we just use native Scheme value types.
114 (Note that in both these cases, allocation is possible / likely, as the
115 value might need to be boxed, but perhaps we won't worry about that. Hmm.)
116
117 For everything else, we use foreign pointers. This includes arrays, pointer
118 arguments and return vals, struct args and return vals, and out and in/out
119 arguments.
120 */
121
122SCM_API SCM scm_make_foreign_function (SCM return_type, SCM func_ptr,
123 SCM arg_types);
165a8643 124SCM_INTERNAL SCM scm_i_foreign_call (SCM foreign, const SCM *argv);
d8b04f04
AW
125
126\f
127
ab4779ff 128SCM_INTERNAL void scm_register_foreign (void);
e2c2a699
AW
129
130
131#endif /* SCM_FOREIGN_H */