Zero-offset branches are backward branches; fix "br" backward branches
[bpt/guile.git] / libguile / gsubr.c
CommitLineData
27337b63 1/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2006, 2008, 2009, 2010, 2011, 2013 Free Software Foundation, Inc.
c0fa6561 2 *
73be1d9e 3 * This library is free software; you can redistribute it and/or
53befeb7
NJ
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.
c0fa6561 7 *
53befeb7
NJ
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
c0fa6561 12 *
73be1d9e
MV
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
53befeb7
NJ
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
73be1d9e 17 */
1bbd0b84 18
0f2d19dd 19\f
dbb605f5
LC
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
0f2d19dd
JB
23
24#include <stdio.h>
8321ed20
LC
25#include <stdarg.h>
26
a0599745 27#include "libguile/_scm.h"
a0599745 28#include "libguile/gsubr.h"
fd12a19a
AW
29#include "libguile/foreign.h"
30#include "libguile/instructions.h"
fd12a19a
AW
31#include "libguile/srfi-4.h"
32#include "libguile/programs.h"
22fc179a
HWN
33
34#include "libguile/private-options.h"
0f2d19dd
JB
35\f
36/*
37 * gsubr.c
38 * Provide `gsubrs' -- subrs taking a prescribed number of required, optional,
39 * and rest arguments.
40 */
41
fd12a19a
AW
42\f
43
44/* OK here goes nothing: we're going to define VM assembly trampolines for
4cbc95f1 45 invoking subrs. Ready? Right! */
fd12a19a
AW
46
47/* There's a maximum of 10 args, so the number of possible combinations is:
48 (REQ-OPT-REST)
49 for 0 args: 1 (000) (1 + 0)
50 for 1 arg: 3 (100, 010, 001) (2 + 1)
51 for 2 args: 5 (200, 110, 020, 101, 011) (3 + 2)
52 for 3 args: 7 (300, 210, 120, 030, 201, 111, 021) (4 + 3)
53 for N args: 2N+1
54
55 and the index at which N args starts:
56 for 0 args: 0
57 for 1 args: 1
58 for 2 args: 4
59 for 3 args: 9
60 for N args: N^2
61
62 One can prove this:
63
64 (1 + 3 + 5 + ... + (2N+1))
65 = ((2N+1)+1)/2 * (N+1)
66 = 2(N+1)/2 * (N+1)
67 = (N+1)^2
68
69 Thus the total sum is 11^2 = 121. Let's just generate all of them as
70 read-only data.
71*/
72
fd12a19a
AW
73/* A: req; B: opt; C: rest */
74#define A(nreq) \
095100bb
AW
75 SCM_PACK_OP_24 (assert_nargs_ee, nreq + 1), \
76 SCM_PACK_OP_24 (subr_call, 0), \
27337b63
AW
77 0, \
78 0
fd12a19a
AW
79
80#define B(nopt) \
095100bb
AW
81 SCM_PACK_OP_24 (assert_nargs_le, nopt + 1), \
82 SCM_PACK_OP_24 (alloc_frame, nopt + 1), \
83 SCM_PACK_OP_24 (subr_call, 0), \
27337b63 84 0
fd12a19a
AW
85
86#define C() \
095100bb
AW
87 SCM_PACK_OP_24 (bind_rest, 1), \
88 SCM_PACK_OP_24 (subr_call, 0), \
27337b63
AW
89 0, \
90 0
fd12a19a
AW
91
92#define AB(nreq, nopt) \
095100bb
AW
93 SCM_PACK_OP_24 (assert_nargs_ge, nreq + 1), \
94 SCM_PACK_OP_24 (assert_nargs_le, nreq + nopt + 1), \
95 SCM_PACK_OP_24 (alloc_frame, nreq + nopt + 1), \
96 SCM_PACK_OP_24 (subr_call, 0)
fd12a19a
AW
97
98#define AC(nreq) \
095100bb
AW
99 SCM_PACK_OP_24 (assert_nargs_ge, nreq + 1), \
100 SCM_PACK_OP_24 (bind_rest, nreq + 1), \
101 SCM_PACK_OP_24 (subr_call, 0), \
27337b63 102 0
fd12a19a
AW
103
104#define BC(nopt) \
095100bb
AW
105 SCM_PACK_OP_24 (bind_rest, nopt + 1), \
106 SCM_PACK_OP_24 (subr_call, 0), \
27337b63
AW
107 0, \
108 0
fd12a19a
AW
109
110#define ABC(nreq, nopt) \
095100bb
AW
111 SCM_PACK_OP_24 (assert_nargs_ge, nreq + 1), \
112 SCM_PACK_OP_24 (bind_rest, nreq + nopt + 1), \
113 SCM_PACK_OP_24 (subr_call, 0), \
27337b63
AW
114 0
115
fd12a19a
AW
116
117/*
118 (defun generate-bytecode (n)
119 "Generate bytecode for N arguments"
120 (interactive "p")
27337b63 121 (insert (format "/\* %d arguments *\/\n " n))
fd12a19a
AW
122 (let ((nreq n))
123 (while (<= 0 nreq)
124 (let ((nopt (- n nreq)))
125 (insert
126 (if (< 0 nreq)
127 (if (< 0 nopt)
27337b63
AW
128 (format " AB(%d,%d)," nreq nopt)
129 (format " A(%d)," nreq))
fd12a19a 130 (if (< 0 nopt)
27337b63
AW
131 (format " B(%d)," nopt)
132 (format " A(0),"))))
fd12a19a 133 (setq nreq (1- nreq))))
27337b63 134 (insert "\n ")
fd12a19a
AW
135 (setq nreq (1- n))
136 (while (<= 0 nreq)
137 (let ((nopt (- n nreq 1)))
138 (insert
139 (if (< 0 nreq)
140 (if (< 0 nopt)
27337b63
AW
141 (format " ABC(%d,%d)," nreq nopt)
142 (format " AC(%d)," nreq))
fd12a19a 143 (if (< 0 nopt)
27337b63
AW
144 (format " BC(%d)," nopt)
145 (format " C(),"))))
fd12a19a
AW
146 (setq nreq (1- nreq))))
147 (insert "\n\n ")))
148
149 (defun generate-bytecodes (n)
150 "Generate bytecodes for up to N arguments"
151 (interactive "p")
152 (let ((i 0))
153 (while (<= i n)
154 (generate-bytecode i)
155 (setq i (1+ i)))))
156*/
27337b63
AW
157static const scm_t_uint32 subr_stub_code[] = {
158 /* C-u 1 0 M-x generate-bytecodes RET */
fd12a19a 159 /* 0 arguments */
27337b63 160 A(0),
fd12a19a
AW
161
162 /* 1 arguments */
27337b63
AW
163 A(1), B(1),
164 C(),
fd12a19a
AW
165
166 /* 2 arguments */
27337b63
AW
167 A(2), AB(1,1), B(2),
168 AC(1), BC(1),
fd12a19a
AW
169
170 /* 3 arguments */
27337b63
AW
171 A(3), AB(2,1), AB(1,2), B(3),
172 AC(2), ABC(1,1), BC(2),
fd12a19a
AW
173
174 /* 4 arguments */
27337b63
AW
175 A(4), AB(3,1), AB(2,2), AB(1,3), B(4),
176 AC(3), ABC(2,1), ABC(1,2), BC(3),
fd12a19a
AW
177
178 /* 5 arguments */
27337b63
AW
179 A(5), AB(4,1), AB(3,2), AB(2,3), AB(1,4), B(5),
180 AC(4), ABC(3,1), ABC(2,2), ABC(1,3), BC(4),
fd12a19a
AW
181
182 /* 6 arguments */
27337b63
AW
183 A(6), AB(5,1), AB(4,2), AB(3,3), AB(2,4), AB(1,5), B(6),
184 AC(5), ABC(4,1), ABC(3,2), ABC(2,3), ABC(1,4), BC(5),
fd12a19a
AW
185
186 /* 7 arguments */
27337b63
AW
187 A(7), AB(6,1), AB(5,2), AB(4,3), AB(3,4), AB(2,5), AB(1,6), B(7),
188 AC(6), ABC(5,1), ABC(4,2), ABC(3,3), ABC(2,4), ABC(1,5), BC(6),
fd12a19a
AW
189
190 /* 8 arguments */
27337b63
AW
191 A(8), AB(7,1), AB(6,2), AB(5,3), AB(4,4), AB(3,5), AB(2,6), AB(1,7), B(8),
192 AC(7), ABC(6,1), ABC(5,2), ABC(4,3), ABC(3,4), ABC(2,5), ABC(1,6), BC(7),
fd12a19a
AW
193
194 /* 9 arguments */
27337b63
AW
195 A(9), AB(8,1), AB(7,2), AB(6,3), AB(5,4), AB(4,5), AB(3,6), AB(2,7), AB(1,8), B(9),
196 AC(8), ABC(7,1), ABC(6,2), ABC(5,3), ABC(4,4), ABC(3,5), ABC(2,6), ABC(1,7), BC(8),
fd12a19a
AW
197
198 /* 10 arguments */
27337b63
AW
199 A(10), AB(9,1), AB(8,2), AB(7,3), AB(6,4), AB(5,5), AB(4,6), AB(3,7), AB(2,8), AB(1,9), B(10),
200 AC(9), ABC(8,1), ABC(7,2), ABC(6,3), ABC(5,4), ABC(4,5), ABC(3,6), ABC(2,7), ABC(1,8), BC(9),
fd12a19a
AW
201};
202
27337b63
AW
203#undef A
204#undef B
205#undef C
206#undef AB
207#undef AC
208#undef BC
209#undef ABC
210
fd12a19a 211/* (nargs * nargs) + nopt + rest * (nargs + 1) */
27337b63
AW
212#define SUBR_STUB_CODE(nreq,nopt,rest) \
213 &subr_stub_code[((nreq + nopt + rest) * (nreq + nopt + rest) \
214 + nopt + rest * (nreq + nopt + rest + 1)) * 4]
fd12a19a 215
27337b63
AW
216static const scm_t_uint32*
217get_subr_stub_code (unsigned int nreq, unsigned int nopt, unsigned int rest)
fd12a19a
AW
218{
219 if (SCM_UNLIKELY (rest > 1 || nreq + nopt + rest > 10))
220 scm_out_of_range ("make-subr", scm_from_uint (nreq + nopt + rest));
221
27337b63 222 return SUBR_STUB_CODE (nreq, nopt, rest);
fd12a19a 223}
85db4a2c 224
9d78586f 225static SCM
27337b63
AW
226create_subr (int define, const char *name,
227 unsigned int nreq, unsigned int nopt, unsigned int rest,
228 SCM (*fcn) (), SCM *generic_loc)
0f2d19dd 229{
27337b63 230 SCM ret, sname;
fd12a19a 231 scm_t_bits flags;
27337b63 232 scm_t_bits nfree = generic_loc ? 3 : 2;
9d78586f 233
25d50a05 234 sname = scm_from_utf8_symbol (name);
fd12a19a 235
cc7005bc
AW
236 flags = SCM_F_PROGRAM_IS_PRIMITIVE;
237 flags |= generic_loc ? SCM_F_PROGRAM_IS_PRIMITIVE_GENERIC : 0;
b0ca878c 238
e0755cd1 239 ret = scm_words (scm_tc7_program | (nfree << 16) | flags, nfree + 2);
27337b63 240 SCM_SET_CELL_WORD_1 (ret, get_subr_stub_code (nreq, nopt, rest));
d798a895
AW
241 SCM_PROGRAM_FREE_VARIABLE_SET (ret, 0, scm_from_pointer (fcn, NULL));
242 SCM_PROGRAM_FREE_VARIABLE_SET (ret, 1, sname);
27337b63 243 if (generic_loc)
d798a895 244 SCM_PROGRAM_FREE_VARIABLE_SET (ret, 2,
27337b63 245 scm_from_pointer (generic_loc, NULL));
e20d7001
LC
246
247 if (define)
fd12a19a 248 scm_define (sname, ret);
e20d7001 249
fd12a19a 250 return ret;
0f2d19dd
JB
251}
252
74870c0d
AW
253/* Given a program that is a primitive, determine its minimum arity.
254 This is possible because each primitive's code is 4 32-bit words
255 long, and they are laid out contiguously in an ordered pattern. */
27337b63
AW
256int
257scm_i_primitive_arity (SCM prim, int *req, int *opt, int *rest)
258{
d798a895 259 const scm_t_uint32 *code = SCM_PROGRAM_CODE (prim);
27337b63
AW
260 unsigned idx, nargs, base, next;
261
262 if (code < subr_stub_code)
263 return 0;
264 if (code > subr_stub_code + (sizeof(subr_stub_code) / sizeof(scm_t_uint32)))
265 return 0;
266
267 idx = (code - subr_stub_code) / 4;
268
269 nargs = -1;
270 next = 0;
271 do
272 {
273 base = next;
274 nargs++;
275 next = (nargs + 1) * (nargs + 1);
276 }
277 while (idx >= next);
278
279 *rest = (next - idx) < (idx - base);
280 *req = *rest ? (next - 1) - idx : (base + nargs) - idx;
281 *opt = *rest ? idx - (next - nargs) : idx - base;
282
283 return 1;
284}
285
0e3a59f7 286scm_t_uintptr
27337b63
AW
287scm_i_primitive_call_ip (SCM subr)
288{
d798a895 289 const scm_t_uint32 *code = SCM_PROGRAM_CODE (subr);
27337b63
AW
290
291 /* A stub is 4 32-bit words long, or 16 bytes. The call will be one
292 instruction, in either the fourth, third, or second word. Return a
293 byte offset from the entry. */
0e3a59f7 294 return (scm_t_uintptr)(code + (code[3] ? 3 : code[2] ? 2 : 1));
27337b63
AW
295}
296
9de33deb 297SCM
9d78586f 298scm_c_make_gsubr (const char *name, int req, int opt, int rst, SCM (*fcn)())
9de33deb 299{
27337b63 300 return create_subr (0, name, req, opt, rst, fcn, NULL);
9d78586f
MV
301}
302
303SCM
304scm_c_define_gsubr (const char *name, int req, int opt, int rst, SCM (*fcn)())
305{
27337b63 306 return create_subr (1, name, req, opt, rst, fcn, NULL);
9de33deb
MD
307}
308
9d78586f
MV
309SCM
310scm_c_make_gsubr_with_generic (const char *name,
311 int req,
312 int opt,
313 int rst,
314 SCM (*fcn)(),
315 SCM *gf)
316{
27337b63 317 return create_subr (0, name, req, opt, rst, fcn, gf);
9d78586f
MV
318}
319
320SCM
321scm_c_define_gsubr_with_generic (const char *name,
322 int req,
323 int opt,
324 int rst,
325 SCM (*fcn)(),
326 SCM *gf)
327{
27337b63 328 return create_subr (1, name, req, opt, rst, fcn, gf);
9d78586f
MV
329}
330
0f2d19dd
JB
331void
332scm_init_gsubr()
0f2d19dd 333{
85db4a2c 334#include "libguile/gsubr.x"
0f2d19dd 335}
89e00824
ML
336
337/*
338 Local Variables:
339 c-file-style: "gnu"
340 End:
341*/