Updated the assembly process so that `u8vectors' are used. Compilation works.
[bpt/guile.git] / src / objcodes.c
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
42 #include <string.h>
43 #include <fcntl.h>
44 #include <unistd.h>
45 #include <sys/mman.h>
46 #include <sys/stat.h>
47 #include <sys/types.h>
48 #include <assert.h>
49
50 #include "programs.h"
51 #include "objcodes.h"
52
53 #define OBJCODE_COOKIE "GOOF-0.5"
54
55 \f
56 /*
57 * Objcode type
58 */
59
60 scm_t_bits scm_tc16_objcode;
61
62 static SCM
63 make_objcode (size_t size)
64 #define FUNC_NAME "make_objcode"
65 {
66 struct scm_objcode *p = scm_gc_malloc (sizeof (struct scm_objcode),
67 "objcode");
68 p->size = size;
69 p->base = scm_gc_malloc (size, "objcode-base");
70 p->fd = -1;
71 SCM_RETURN_NEWSMOB (scm_tc16_objcode, p);
72 }
73 #undef FUNC_NAME
74
75 static SCM
76 make_objcode_by_mmap (int fd)
77 #define FUNC_NAME "make_objcode_by_mmap"
78 {
79 int ret;
80 char *addr;
81 struct stat st;
82 struct scm_objcode *p;
83
84 ret = fstat (fd, &st);
85 if (ret < 0) SCM_SYSERROR;
86
87 addr = mmap (0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
88 if (addr == MAP_FAILED) SCM_SYSERROR;
89
90 p = scm_gc_malloc (sizeof (struct scm_objcode), "objcode");
91 p->size = st.st_size;
92 p->base = addr;
93 p->fd = fd;
94 SCM_RETURN_NEWSMOB (scm_tc16_objcode, p);
95 }
96 #undef FUNC_NAME
97
98 static scm_sizet
99 objcode_free (SCM obj)
100 #define FUNC_NAME "objcode_free"
101 {
102 size_t size = sizeof (struct scm_objcode);
103 struct scm_objcode *p = SCM_OBJCODE_DATA (obj);
104
105 if (p->fd >= 0)
106 {
107 int rv;
108 rv = munmap (p->base, p->size);
109 if (rv < 0) SCM_SYSERROR;
110 rv = close (p->fd);
111 if (rv < 0) SCM_SYSERROR;
112 }
113 else
114 scm_gc_free (p->base, p->size, "objcode-base");
115
116 scm_gc_free (p, size, "objcode");
117
118 return 0;
119 }
120 #undef FUNC_NAME
121
122 \f
123 /*
124 * Scheme interface
125 */
126
127 SCM_DEFINE (scm_objcode_p, "objcode?", 1, 0, 0,
128 (SCM obj),
129 "")
130 #define FUNC_NAME s_scm_objcode_p
131 {
132 return SCM_BOOL (SCM_OBJCODE_P (obj));
133 }
134 #undef FUNC_NAME
135
136 SCM_DEFINE (scm_bytecode_to_objcode, "bytecode->objcode", 3, 0, 0,
137 (SCM bytecode, SCM nlocs, SCM nexts),
138 "")
139 #define FUNC_NAME s_scm_bytecode_to_objcode
140 {
141 size_t size;
142 ssize_t increment;
143 scm_t_array_handle handle;
144 char *base;
145 const char *c_bytecode;
146 SCM objcode;
147
148 if (scm_u8vector_p (bytecode) != SCM_BOOL_T)
149 scm_wrong_type_arg (FUNC_NAME, 1, bytecode);
150 SCM_VALIDATE_INUM (2, nlocs);
151 SCM_VALIDATE_INUM (3, nexts);
152
153 c_bytecode = scm_u8vector_elements (bytecode, &handle, &size, &increment);
154 assert (increment == 1);
155
156 objcode = make_objcode (size);
157 base = SCM_OBJCODE_BASE (objcode);
158
159 memcpy (base, OBJCODE_COOKIE, 8);
160 base[8] = scm_to_int (nlocs);
161 base[9] = scm_to_int (nexts);
162
163 memcpy (base + 10, c_bytecode, size - 10);
164
165 scm_array_handle_release (&handle);
166
167 return objcode;
168 }
169 #undef FUNC_NAME
170
171 SCM_DEFINE (scm_load_objcode, "load-objcode", 1, 0, 0,
172 (SCM file),
173 "")
174 #define FUNC_NAME s_scm_load_objcode
175 {
176 int fd;
177
178 SCM_VALIDATE_STRING (1, file);
179
180 fd = open (SCM_STRING_CHARS (file), O_RDONLY);
181 if (fd < 0) SCM_SYSERROR;
182
183 return make_objcode_by_mmap (fd);
184 }
185 #undef FUNC_NAME
186
187 SCM_DEFINE (scm_objcode_to_u8vector, "objcode->u8vector", 1, 0, 0,
188 (SCM objcode),
189 "")
190 #define FUNC_NAME s_scm_objcode_to_u8vector
191 {
192 char *u8vector;
193 size_t size;
194
195 SCM_VALIDATE_OBJCODE (1, objcode);
196
197 size = SCM_OBJCODE_SIZE (objcode);
198 /* FIXME: Is `gc_malloc' ok here? */
199 u8vector = scm_gc_malloc (size, "objcode-u8vector");
200 memcpy (u8vector, SCM_OBJCODE_BASE (objcode), size);
201
202 return scm_take_u8vector (u8vector, size);
203 }
204 #undef FUNC_NAME
205
206 SCM_DEFINE (scm_objcode_to_program, "objcode->program", 1, 0, 0,
207 (SCM objcode),
208 "")
209 #define FUNC_NAME s_scm_objcode_to_program
210 {
211 SCM prog;
212 size_t size;
213 char *base;
214 struct scm_program *p;
215
216 SCM_VALIDATE_OBJCODE (1, objcode);
217
218 base = SCM_OBJCODE_BASE (objcode);
219 size = SCM_OBJCODE_SIZE (objcode);
220 prog = scm_c_make_program (base + 10, size - 10, objcode);
221 p = SCM_PROGRAM_DATA (prog);
222 p->nlocs = base[8];
223 p->nexts = base[9];
224 return prog;
225 }
226 #undef FUNC_NAME
227
228 \f
229 void
230 scm_init_objcodes (void)
231 {
232 scm_tc16_objcode = scm_make_smob_type ("objcode", 0);
233 scm_set_smob_free (scm_tc16_objcode, objcode_free);
234
235 #ifndef SCM_MAGIC_SNARFER
236 #include "objcodes.x"
237 #endif
238 }
239
240 /*
241 Local Variables:
242 c-file-style: "gnu"
243 End:
244 */