reimplement srfi-4 vectors on top of bytevectors
[bpt/guile.git] / libguile / objcodes.c
1 /* Copyright (C) 2001, 2009, 2010 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
17 */
18
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <string.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <assert.h>
30 #include <alignof.h>
31
32 #include "_scm.h"
33 #include "vm-bootstrap.h"
34 #include "programs.h"
35 #include "objcodes.h"
36
37 /* SCM_OBJCODE_COOKIE is defined in _scm.h */
38 /* The length of the header must be a multiple of 8 bytes. */
39 verify (((sizeof (SCM_OBJCODE_COOKIE) - 1) & 7) == 0);
40
41 \f
42 /*
43 * Objcode type
44 */
45
46 scm_t_bits scm_tc16_objcode;
47
48 static SCM
49 make_objcode_by_mmap (int fd)
50 #define FUNC_NAME "make_objcode_by_mmap"
51 {
52 int ret;
53 char *addr;
54 struct stat st;
55 SCM sret = SCM_BOOL_F;
56 struct scm_objcode *data;
57
58 ret = fstat (fd, &st);
59 if (ret < 0)
60 SCM_SYSERROR;
61
62 if (st.st_size <= sizeof (struct scm_objcode) + strlen (SCM_OBJCODE_COOKIE))
63 scm_misc_error (FUNC_NAME, "object file too small (~a bytes)",
64 scm_list_1 (SCM_I_MAKINUM (st.st_size)));
65
66 addr = mmap (0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
67 if (addr == MAP_FAILED)
68 {
69 (void) close (fd);
70 SCM_SYSERROR;
71 }
72
73 if (memcmp (addr, SCM_OBJCODE_COOKIE, strlen (SCM_OBJCODE_COOKIE)))
74 {
75 SCM args = scm_list_1 (scm_from_locale_stringn
76 (addr, strlen (SCM_OBJCODE_COOKIE)));
77 (void) close (fd);
78 (void) munmap (addr, st.st_size);
79 scm_misc_error (FUNC_NAME, "bad header on object file: ~s", args);
80 }
81
82 data = (struct scm_objcode*)(addr + strlen (SCM_OBJCODE_COOKIE));
83
84 if (data->len + data->metalen != (st.st_size - sizeof (*data) - strlen (SCM_OBJCODE_COOKIE)))
85 {
86 (void) close (fd);
87 (void) munmap (addr, st.st_size);
88 scm_misc_error (FUNC_NAME, "bad length header (~a, ~a)",
89 scm_list_2 (scm_from_size_t (st.st_size),
90 scm_from_uint32 (sizeof (*data) + data->len
91 + data->metalen)));
92 }
93
94 SCM_NEWSMOB3 (sret, scm_tc16_objcode, addr + strlen (SCM_OBJCODE_COOKIE),
95 SCM_PACK (SCM_BOOL_F), fd);
96 SCM_SET_SMOB_FLAGS (sret, SCM_F_OBJCODE_IS_MMAP);
97
98 /* FIXME: we leak ourselves and the file descriptor. but then again so does
99 dlopen(). */
100 return scm_permanent_object (sret);
101 }
102 #undef FUNC_NAME
103
104 SCM
105 scm_c_make_objcode_slice (SCM parent, const scm_t_uint8 *ptr)
106 #define FUNC_NAME "make-objcode-slice"
107 {
108 const struct scm_objcode *data, *parent_data;
109 const scm_t_uint8 *parent_base;
110 SCM ret;
111
112 SCM_VALIDATE_OBJCODE (1, parent);
113 parent_data = SCM_OBJCODE_DATA (parent);
114 parent_base = SCM_C_OBJCODE_BASE (parent_data);
115
116 if (ptr < parent_base
117 || ptr >= (parent_base + parent_data->len + parent_data->metalen
118 - sizeof (struct scm_objcode)))
119 scm_misc_error (FUNC_NAME, "offset out of bounds (~a vs ~a + ~a + ~a)",
120 scm_list_4 (scm_from_ulong ((unsigned long) ptr),
121 scm_from_ulong ((unsigned long) parent_base),
122 scm_from_uint32 (parent_data->len),
123 scm_from_uint32 (parent_data->metalen)));
124
125 /* Make sure bytecode for the objcode-meta is suitable aligned. Failing to
126 do so leads to SIGBUS/SIGSEGV on some arches (e.g., SPARC). */
127 assert ((((scm_t_bits) ptr) &
128 (alignof_type (struct scm_objcode) - 1UL)) == 0);
129
130 data = (struct scm_objcode*) ptr;
131 assert (SCM_C_OBJCODE_BASE (data) + data->len + data->metalen
132 <= parent_base + parent_data->len + parent_data->metalen);
133
134 SCM_NEWSMOB2 (ret, scm_tc16_objcode, data, parent);
135 SCM_SET_SMOB_FLAGS (ret, SCM_F_OBJCODE_IS_SLICE);
136 return ret;
137 }
138 #undef FUNC_NAME
139
140 \f
141 /*
142 * Scheme interface
143 */
144
145 SCM_DEFINE (scm_objcode_p, "objcode?", 1, 0, 0,
146 (SCM obj),
147 "")
148 #define FUNC_NAME s_scm_objcode_p
149 {
150 return scm_from_bool (SCM_OBJCODE_P (obj));
151 }
152 #undef FUNC_NAME
153
154 SCM_DEFINE (scm_objcode_meta, "objcode-meta", 1, 0, 0,
155 (SCM objcode),
156 "")
157 #define FUNC_NAME s_scm_objcode_meta
158 {
159 SCM_VALIDATE_OBJCODE (1, objcode);
160
161 if (SCM_OBJCODE_META_LEN (objcode) == 0)
162 return SCM_BOOL_F;
163 else
164 return scm_c_make_objcode_slice (objcode, (SCM_OBJCODE_BASE (objcode)
165 + SCM_OBJCODE_LEN (objcode)));
166 }
167 #undef FUNC_NAME
168
169 SCM_DEFINE (scm_bytecode_to_objcode, "bytecode->objcode", 1, 0, 0,
170 (SCM bytecode),
171 "")
172 #define FUNC_NAME s_scm_bytecode_to_objcode
173 {
174 size_t size;
175 const scm_t_uint8 *c_bytecode;
176 struct scm_objcode *data;
177 SCM objcode;
178
179 if (!scm_is_bytevector (bytecode))
180 scm_wrong_type_arg (FUNC_NAME, 1, bytecode);
181
182 size = SCM_BYTEVECTOR_LENGTH (bytecode);
183 c_bytecode = (const scm_t_uint8*)SCM_BYTEVECTOR_CONTENTS (bytecode);
184
185 SCM_ASSERT_RANGE (0, bytecode, size >= sizeof(struct scm_objcode));
186 data = (struct scm_objcode*)c_bytecode;
187
188 if (data->len + data->metalen != (size - sizeof (*data)))
189 scm_misc_error (FUNC_NAME, "bad bytevector size (~a != ~a)",
190 scm_list_2 (scm_from_size_t (size),
191 scm_from_uint32 (sizeof (*data) + data->len + data->metalen)));
192
193 SCM_NEWSMOB2 (objcode, scm_tc16_objcode, data, bytecode);
194 SCM_SET_SMOB_FLAGS (objcode, SCM_F_OBJCODE_IS_BYTEVECTOR);
195
196 /* foolishly, we assume that as long as bytecode is around, that c_bytecode
197 will be of the same length; perhaps a bad assumption? */
198
199 return objcode;
200 }
201 #undef FUNC_NAME
202
203 SCM_DEFINE (scm_load_objcode, "load-objcode", 1, 0, 0,
204 (SCM file),
205 "")
206 #define FUNC_NAME s_scm_load_objcode
207 {
208 int fd;
209 char *c_file;
210
211 SCM_VALIDATE_STRING (1, file);
212
213 c_file = scm_to_locale_string (file);
214 fd = open (c_file, O_RDONLY);
215 free (c_file);
216 if (fd < 0) SCM_SYSERROR;
217
218 return make_objcode_by_mmap (fd);
219 }
220 #undef FUNC_NAME
221
222 SCM_DEFINE (scm_objcode_to_bytecode, "objcode->bytecode", 1, 0, 0,
223 (SCM objcode),
224 "")
225 #define FUNC_NAME s_scm_objcode_to_bytecode
226 {
227 scm_t_int8 *s8vector;
228 scm_t_uint32 len;
229
230 SCM_VALIDATE_OBJCODE (1, objcode);
231
232 len = sizeof(struct scm_objcode) + SCM_OBJCODE_TOTAL_LEN (objcode);
233
234 s8vector = scm_malloc (len);
235 memcpy (s8vector, SCM_OBJCODE_DATA (objcode), len);
236
237 return scm_c_take_bytevector (s8vector, len);
238 }
239 #undef FUNC_NAME
240
241 SCM_DEFINE (scm_write_objcode, "write-objcode", 2, 0, 0,
242 (SCM objcode, SCM port),
243 "")
244 #define FUNC_NAME s_scm_write_objcode
245 {
246 SCM_VALIDATE_OBJCODE (1, objcode);
247 SCM_VALIDATE_OUTPUT_PORT (2, port);
248
249 scm_c_write (port, SCM_OBJCODE_COOKIE, strlen (SCM_OBJCODE_COOKIE));
250 scm_c_write (port, SCM_OBJCODE_DATA (objcode),
251 sizeof (struct scm_objcode) + SCM_OBJCODE_TOTAL_LEN (objcode));
252
253 return SCM_UNSPECIFIED;
254 }
255 #undef FUNC_NAME
256
257 \f
258 void
259 scm_bootstrap_objcodes (void)
260 {
261 scm_tc16_objcode = scm_make_smob_type ("objcode", 0);
262 scm_c_register_extension ("libguile", "scm_init_objcodes",
263 (scm_t_extension_init_func)scm_init_objcodes, NULL);
264 }
265
266 /* Before, we used __BYTE_ORDER, but that is not defined on all
267 systems. So punt and use automake, PDP endianness be damned. */
268 #ifdef WORDS_BIGENDIAN
269 #define SCM_BYTE_ORDER 4321
270 #else
271 #define SCM_BYTE_ORDER 1234
272 #endif
273
274 void
275 scm_init_objcodes (void)
276 {
277 scm_bootstrap_vm ();
278
279 #ifndef SCM_MAGIC_SNARFER
280 #include "libguile/objcodes.x"
281 #endif
282
283 scm_c_define ("word-size", scm_from_size_t (sizeof(SCM)));
284 scm_c_define ("byte-order", scm_from_uint16 (SCM_BYTE_ORDER));
285 }
286
287 /*
288 Local Variables:
289 c-file-style: "gnu"
290 End:
291 */