fix distcheck hopefully, by cleaning the vm-i-*.i files
[bpt/guile.git] / libguile / objcodes.c
CommitLineData
8f5cfc81
KN
1/* Copyright (C) 2001 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41
13c47753
AW
42#if HAVE_CONFIG_H
43# include <config.h>
44#endif
45
8f5cfc81
KN
46#include <string.h>
47#include <fcntl.h>
48#include <unistd.h>
49#include <sys/mman.h>
50#include <sys/stat.h>
51#include <sys/types.h>
054599f1 52#include <assert.h>
8f5cfc81 53
83495480 54#include "vm-bootstrap.h"
8f5cfc81
KN
55#include "programs.h"
56#include "objcodes.h"
57
53e28ed9 58/* nb, the length of the header should be a multiple of 8 bytes */
8f5cfc81
KN
59#define OBJCODE_COOKIE "GOOF-0.5"
60
61\f
62/*
63 * Objcode type
64 */
65
f9e8c09d 66scm_t_bits scm_tc16_objcode;
8f5cfc81 67
8f5cfc81
KN
68static SCM
69make_objcode_by_mmap (int fd)
70#define FUNC_NAME "make_objcode_by_mmap"
71{
72 int ret;
73 char *addr;
74 struct stat st;
53e28ed9
AW
75 SCM sret = SCM_BOOL_F;
76 struct scm_objcode *data;
8f5cfc81
KN
77
78 ret = fstat (fd, &st);
0b5f0e49 79 if (ret < 0)
62082959 80 SCM_SYSERROR;
8f5cfc81 81
53e28ed9 82 if (st.st_size <= sizeof (struct scm_objcode) + strlen (OBJCODE_COOKIE))
0b5f0e49
LC
83 scm_misc_error (FUNC_NAME, "object file too small (~a bytes)",
84 SCM_LIST1 (SCM_I_MAKINUM (st.st_size)));
85
8f5cfc81 86 addr = mmap (0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
62082959
LC
87 if (addr == MAP_FAILED)
88 SCM_SYSERROR;
89
90 if (memcmp (addr, OBJCODE_COOKIE, strlen (OBJCODE_COOKIE)))
91 SCM_SYSERROR;
8f5cfc81 92
53e28ed9
AW
93 data = (struct scm_objcode*)(addr + strlen (OBJCODE_COOKIE));
94
1f1ec13b 95 if (data->len + data->metalen != (st.st_size - sizeof (*data) - strlen (OBJCODE_COOKIE)))
53e28ed9
AW
96 scm_misc_error (FUNC_NAME, "bad length header (~a, ~a)",
97 SCM_LIST2 (scm_from_size_t (st.st_size),
1f1ec13b 98 scm_from_uint32 (sizeof (*data) + data->len + data->metalen)));
53e28ed9
AW
99
100 SCM_NEWSMOB3 (sret, scm_tc16_objcode, addr + strlen (OBJCODE_COOKIE),
101 SCM_PACK (SCM_BOOL_F), fd);
102 SCM_SET_SMOB_FLAGS (sret, SCM_F_OBJCODE_IS_MMAP);
103
104 /* FIXME: we leak ourselves and the file descriptor. but then again so does
105 dlopen(). */
106 return scm_permanent_object (sret);
8f5cfc81
KN
107}
108#undef FUNC_NAME
109
53e28ed9
AW
110SCM
111scm_c_make_objcode_slice (SCM parent, scm_t_uint8 *ptr)
112#define FUNC_NAME "make-objcode-slice"
8f5cfc81 113{
53e28ed9
AW
114 struct scm_objcode *data, *parent_data;
115 SCM ret;
116
117 SCM_VALIDATE_OBJCODE (1, parent);
118 parent_data = SCM_OBJCODE_DATA (parent);
119
120 if (ptr < parent_data->base
1f1ec13b 121 || ptr >= (parent_data->base + parent_data->len + parent_data->metalen
53e28ed9 122 - sizeof (struct scm_objcode)))
1f1ec13b 123 scm_misc_error (FUNC_NAME, "offset out of bounds (~a vs ~a + ~a + ~a)",
17dd267a
AW
124 SCM_LIST4 (scm_from_ulong ((unsigned long)ptr),
125 scm_from_ulong ((unsigned long)parent_data->base),
1f1ec13b
AW
126 scm_from_uint32 (parent_data->len),
127 scm_from_uint32 (parent_data->metalen)));
53e28ed9
AW
128
129 data = (struct scm_objcode*)ptr;
1f1ec13b 130 if (data->base + data->len + data->metalen > parent_data->base + parent_data->len + parent_data->metalen)
53e28ed9
AW
131 abort ();
132
133 SCM_NEWSMOB2 (ret, scm_tc16_objcode, data, parent);
134 SCM_SET_SMOB_FLAGS (ret, SCM_F_OBJCODE_IS_SLICE);
135 return ret;
8f5cfc81
KN
136}
137#undef FUNC_NAME
138
53e28ed9
AW
139static SCM
140objcode_mark (SCM obj)
141{
142 return SCM_SMOB_OBJECT_2 (obj);
143}
144
8f5cfc81
KN
145\f
146/*
147 * Scheme interface
148 */
149
150SCM_DEFINE (scm_objcode_p, "objcode?", 1, 0, 0,
151 (SCM obj),
152 "")
153#define FUNC_NAME s_scm_objcode_p
154{
155 return SCM_BOOL (SCM_OBJCODE_P (obj));
156}
157#undef FUNC_NAME
158
1f1ec13b
AW
159SCM_DEFINE (scm_objcode_meta, "objcode-meta", 1, 0, 0,
160 (SCM objcode),
161 "")
162#define FUNC_NAME s_scm_objcode_meta
163{
164 SCM_VALIDATE_OBJCODE (1, objcode);
165
166 if (SCM_OBJCODE_META_LEN (objcode) == 0)
167 return SCM_BOOL_F;
168 else
169 return scm_c_make_objcode_slice (objcode, (SCM_OBJCODE_BASE (objcode)
170 + SCM_OBJCODE_LEN (objcode)));
171}
172#undef FUNC_NAME
173
53e28ed9
AW
174SCM_DEFINE (scm_bytecode_to_objcode, "bytecode->objcode", 1, 0, 0,
175 (SCM bytecode),
8f5cfc81
KN
176 "")
177#define FUNC_NAME s_scm_bytecode_to_objcode
178{
179 size_t size;
054599f1
LC
180 ssize_t increment;
181 scm_t_array_handle handle;
b6368dbb 182 const scm_t_uint8 *c_bytecode;
53e28ed9 183 struct scm_objcode *data;
8f5cfc81
KN
184 SCM objcode;
185
53e28ed9 186 if (scm_is_false (scm_u8vector_p (bytecode)))
054599f1 187 scm_wrong_type_arg (FUNC_NAME, 1, bytecode);
8f5cfc81 188
054599f1 189 c_bytecode = scm_u8vector_elements (bytecode, &handle, &size, &increment);
53e28ed9
AW
190 data = (struct scm_objcode*)c_bytecode;
191 SCM_NEWSMOB2 (objcode, scm_tc16_objcode, data, bytecode);
054599f1 192 scm_array_handle_release (&handle);
ac47d5f6
AW
193
194 SCM_ASSERT_RANGE (0, bytecode, size >= sizeof(struct scm_objcode));
195 if (data->len + data->metalen != (size - sizeof (*data)))
196 scm_misc_error (FUNC_NAME, "bad u8vector size (~a != ~a)",
197 SCM_LIST2 (scm_from_size_t (size),
198 scm_from_uint32 (sizeof (*data) + data->len + data->metalen)));
53e28ed9 199 assert (increment == 1);
53e28ed9
AW
200 SCM_SET_SMOB_FLAGS (objcode, SCM_F_OBJCODE_IS_U8VECTOR);
201
202 /* foolishly, we assume that as long as bytecode is around, that c_bytecode
203 will be of the same length; perhaps a bad assumption? */
d8eeb67c 204
8f5cfc81
KN
205 return objcode;
206}
207#undef FUNC_NAME
208
209SCM_DEFINE (scm_load_objcode, "load-objcode", 1, 0, 0,
210 (SCM file),
211 "")
212#define FUNC_NAME s_scm_load_objcode
213{
214 int fd;
2d80426a 215 char *c_file;
8f5cfc81
KN
216
217 SCM_VALIDATE_STRING (1, file);
218
2d80426a
LC
219 c_file = scm_to_locale_string (file);
220 fd = open (c_file, O_RDONLY);
221 free (c_file);
8f5cfc81
KN
222 if (fd < 0) SCM_SYSERROR;
223
224 return make_objcode_by_mmap (fd);
225}
226#undef FUNC_NAME
227
9bb8012d 228SCM_DEFINE (scm_objcode_to_bytecode, "objcode->bytecode", 1, 0, 0,
8f5cfc81
KN
229 (SCM objcode),
230 "")
9bb8012d 231#define FUNC_NAME s_scm_objcode_to_bytecode
8f5cfc81 232{
b6368dbb 233 scm_t_uint8 *u8vector;
53e28ed9 234 scm_t_uint32 len;
054599f1 235
8f5cfc81 236 SCM_VALIDATE_OBJCODE (1, objcode);
054599f1 237
1f1ec13b 238 len = sizeof(struct scm_objcode) + SCM_OBJCODE_TOTAL_LEN (objcode);
054599f1 239 /* FIXME: Is `gc_malloc' ok here? */
53e28ed9
AW
240 u8vector = scm_gc_malloc (len, "objcode-u8vector");
241 memcpy (u8vector, SCM_OBJCODE_DATA (objcode), len);
054599f1 242
53e28ed9 243 return scm_take_u8vector (u8vector, len);
8f5cfc81
KN
244}
245#undef FUNC_NAME
246
53e28ed9
AW
247SCM_DEFINE (scm_write_objcode, "write-objcode", 2, 0, 0,
248 (SCM objcode, SCM port),
8f5cfc81 249 "")
53e28ed9 250#define FUNC_NAME s_scm_write_objcode
8f5cfc81 251{
8f5cfc81 252 SCM_VALIDATE_OBJCODE (1, objcode);
53e28ed9
AW
253 SCM_VALIDATE_OUTPUT_PORT (2, port);
254
255 scm_c_write (port, OBJCODE_COOKIE, strlen (OBJCODE_COOKIE));
256 scm_c_write (port, SCM_OBJCODE_DATA (objcode),
1f1ec13b 257 sizeof (struct scm_objcode) + SCM_OBJCODE_TOTAL_LEN (objcode));
53e28ed9
AW
258
259 return SCM_UNSPECIFIED;
8f5cfc81
KN
260}
261#undef FUNC_NAME
262
263\f
264void
07e56b27 265scm_bootstrap_objcodes (void)
8f5cfc81
KN
266{
267 scm_tc16_objcode = scm_make_smob_type ("objcode", 0);
53e28ed9 268 scm_set_smob_mark (scm_tc16_objcode, objcode_mark);
07e56b27
AW
269}
270
8992a9e3
AW
271/* Before, we used __BYTE_ORDER, but that is not defined on all
272 systems. So punt and use automake, PDP endianness be damned. */
273#ifdef WORDS_BIGENDIAN
274#define SCM_BYTE_ORDER 4321
275#else
276#define SCM_BYTE_ORDER 1234
277#endif
278
07e56b27
AW
279void
280scm_init_objcodes (void)
281{
282 scm_bootstrap_vm ();
8f5cfc81
KN
283
284#ifndef SCM_MAGIC_SNARFER
aeeff258 285#include "libguile/objcodes.x"
8f5cfc81 286#endif
53e28ed9
AW
287
288 scm_c_define ("word-size", scm_from_size_t (sizeof(SCM)));
8992a9e3 289 scm_c_define ("byte-order", scm_from_uint16 (SCM_BYTE_ORDER));
8f5cfc81
KN
290}
291
292/*
293 Local Variables:
294 c-file-style: "gnu"
295 End:
296*/