1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
/* Copyright (C) 2018 Free Software Foundation, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include "libguile/gsubr.h"
#include "boolean.h"
#include "alist.h"
#include "cache-internal.h"
#include "extensions.h"
#include "fluids.h"
#include "goops.h"
#include "keywords.h"
#include "libguile/modules.h"
#include "numbers.h"
#include "libguile/symbols.h"
#include "intrinsics.h"
struct scm_vm_intrinsics scm_vm_intrinsics;
SCM_DEFINE (scm_intrinsic_list, "intrinsic-list", 0, 0, 0,
(void),
"")
#define FUNC_NAME s_scm_intrinsic_list
{
SCM list = SCM_EOL;
#define ADD_INTRINSIC(type, id, name, ID) \
if (name) \
list = scm_acons (scm_from_latin1_symbol (name), \
scm_from_int (SCM_VM_INTRINSIC_##ID), \
list);
SCM_FOR_ALL_VM_INTRINSICS (ADD_INTRINSIC);
#undef ADD_INTRINSIC
return list;
}
#undef FUNC_NAME
static SCM
add_immediate (SCM a, scm_t_uint8 b)
{
return scm_sum (a, scm_from_uint8 (b));
}
static SCM
sub_immediate (SCM a, scm_t_uint8 b)
{
return scm_difference (a, scm_from_uint8 (b));
}
static void
string_set_x (SCM str, scm_t_uint64 idx, scm_t_uint64 ch)
{
str = scm_i_string_start_writing (str);
scm_i_string_set_x (str, idx, ch);
scm_i_string_stop_writing ();
}
static SCM
string_to_number (SCM str)
{
return scm_string_to_number (str, SCM_UNDEFINED /* radix = 10 */);
}
static scm_t_uint64
scm_to_uint64_truncate (SCM x)
{
if (SCM_I_INUMP (x))
return (scm_t_uint64) SCM_I_INUM (x);
else
return scm_to_uint64 (scm_logand (x, scm_from_uint64 ((scm_t_uint64) -1)));
}
static SCM
logsub (SCM x, SCM y)
{
if (SCM_I_INUMP (x) && SCM_I_INUMP (y))
{
scm_t_signed_bits a, b;
a = SCM_I_INUM (x);
b = SCM_I_INUM (y);
return SCM_I_MAKINUM (a & ~b);
}
return scm_logand (x, scm_lognot (y));
}
static void
wind (scm_i_thread *thread, SCM winder, SCM unwinder)
{
scm_dynstack_push_dynwind (&thread->dynstack, winder, unwinder);
}
static void
unwind (scm_i_thread *thread)
{
scm_dynstack_pop (&thread->dynstack);
}
static void
push_fluid (scm_i_thread *thread, SCM fluid, SCM value)
{
scm_dynstack_push_fluid (&thread->dynstack, fluid, value,
thread->dynamic_state);
}
static void
pop_fluid (scm_i_thread *thread)
{
scm_dynstack_unwind_fluid (&thread->dynstack, thread->dynamic_state);
}
static SCM
fluid_ref (scm_i_thread *thread, SCM fluid)
{
struct scm_cache_entry *entry;
/* If we find FLUID in the cache, then it is indeed a fluid. */
entry = scm_cache_lookup (&thread->dynamic_state->cache, fluid);
if (SCM_LIKELY (scm_is_eq (SCM_PACK (entry->key), fluid)
&& !SCM_UNBNDP (SCM_PACK (entry->value))))
return SCM_PACK (entry->value);
return scm_fluid_ref (fluid);
}
static void
fluid_set_x (scm_i_thread *thread, SCM fluid, SCM value)
{
struct scm_cache_entry *entry;
/* If we find FLUID in the cache, then it is indeed a fluid. */
entry = scm_cache_lookup (&thread->dynamic_state->cache, fluid);
if (SCM_LIKELY (scm_is_eq (SCM_PACK (entry->key), fluid)))
entry->value = SCM_UNPACK (value);
else
scm_fluid_set_x (fluid, value);
}
static void
push_dynamic_state (scm_i_thread *thread, SCM state)
{
scm_dynstack_push_dynamic_state (&thread->dynstack, state,
thread->dynamic_state);
}
static void
pop_dynamic_state (scm_i_thread *thread)
{
scm_dynstack_unwind_dynamic_state (&thread->dynstack,
thread->dynamic_state);
}
static SCM
lsh (SCM a, scm_t_uint64 b)
{
if (SCM_LIKELY (SCM_I_INUMP (a))
&& b < (scm_t_uint64) (SCM_I_FIXNUM_BIT - 1)
&& ((scm_t_bits)
(SCM_SRS (SCM_I_INUM (a), (SCM_I_FIXNUM_BIT-1 - b)) + 1)
<= 1))
{
scm_t_signed_bits nn = SCM_I_INUM (a);
return SCM_I_MAKINUM (nn < 0 ? -(-nn << b) : (nn << b));
}
else
return scm_ash (a, scm_from_uint64 (b));
}
static SCM
rsh (SCM a, scm_t_uint64 b)
{
if (SCM_LIKELY (SCM_I_INUMP (a)))
{
if (b > (scm_t_uint64) (SCM_I_FIXNUM_BIT - 1))
b = SCM_I_FIXNUM_BIT - 1;
return SCM_I_MAKINUM (SCM_SRS (SCM_I_INUM (a), b));
}
else
return scm_ash (a, scm_difference (SCM_INUM0, scm_from_uint64 (b)));
}
static SCM
lsh_immediate (SCM a, scm_t_uint8 b)
{
return lsh (a, b);
}
static SCM
rsh_immediate (SCM a, scm_t_uint8 b)
{
return rsh (a, b);
}
static enum scm_compare
less_p (SCM a, SCM b)
{
if (scm_is_true (scm_nan_p (a)) || scm_is_true (scm_nan_p (b)))
return SCM_F_COMPARE_INVALID;
else if (scm_is_true (scm_less_p (a, b)))
return SCM_F_COMPARE_LESS_THAN;
else
return SCM_F_COMPARE_NONE;
}
static int
numerically_equal_p (SCM a, SCM b)
{
return scm_is_true (scm_num_eq_p (a, b));
}
static SCM
resolve_module (SCM name, scm_t_uint8 public_p)
{
SCM mod;
if (!scm_module_system_booted_p)
return SCM_BOOL_F;
mod = scm_maybe_resolve_module (name);
if (scm_is_false (mod))
scm_misc_error (NULL, "Module named ~s does not exist",
scm_list_1 (name));
if (public_p)
{
mod = scm_module_public_interface (mod);
if (scm_is_false (mod))
scm_misc_error (NULL, "Module named ~s has no public interface",
scm_list_1 (name));
}
return mod;
}
static SCM
lookup (SCM module, SCM name)
{
/* If MODULE was captured before modules were booted, use the root
module. Not so nice, but hey... */
if (scm_is_false (module))
module = scm_the_root_module ();
return scm_module_variable (module, name);
}
void
scm_bootstrap_intrinsics (void)
{
scm_vm_intrinsics.add = scm_sum;
scm_vm_intrinsics.add_immediate = add_immediate;
scm_vm_intrinsics.sub = scm_difference;
scm_vm_intrinsics.sub_immediate = sub_immediate;
scm_vm_intrinsics.mul = scm_product;
scm_vm_intrinsics.div = scm_divide;
scm_vm_intrinsics.quo = scm_quotient;
scm_vm_intrinsics.rem = scm_remainder;
scm_vm_intrinsics.mod = scm_modulo;
scm_vm_intrinsics.logand = scm_logand;
scm_vm_intrinsics.logior = scm_logior;
scm_vm_intrinsics.logxor = scm_logxor;
scm_vm_intrinsics.string_set_x = string_set_x;
scm_vm_intrinsics.string_to_number = string_to_number;
scm_vm_intrinsics.string_to_symbol = scm_string_to_symbol;
scm_vm_intrinsics.symbol_to_keyword = scm_symbol_to_keyword;
scm_vm_intrinsics.class_of = scm_class_of;
scm_vm_intrinsics.scm_to_f64 = scm_to_double;
scm_vm_intrinsics.scm_to_u64 = scm_to_uint64;
scm_vm_intrinsics.scm_to_u64_truncate = scm_to_uint64_truncate;
scm_vm_intrinsics.scm_to_s64 = scm_to_int64;
scm_vm_intrinsics.u64_to_scm = scm_from_uint64;
scm_vm_intrinsics.s64_to_scm = scm_from_int64;
scm_vm_intrinsics.logsub = logsub;
scm_vm_intrinsics.wind = wind;
scm_vm_intrinsics.unwind = unwind;
scm_vm_intrinsics.push_fluid = push_fluid;
scm_vm_intrinsics.pop_fluid = pop_fluid;
scm_vm_intrinsics.fluid_ref = fluid_ref;
scm_vm_intrinsics.fluid_set_x = fluid_set_x;
scm_vm_intrinsics.push_dynamic_state = push_dynamic_state;
scm_vm_intrinsics.pop_dynamic_state = pop_dynamic_state;
scm_vm_intrinsics.lsh = lsh;
scm_vm_intrinsics.rsh = rsh;
scm_vm_intrinsics.lsh_immediate = lsh_immediate;
scm_vm_intrinsics.rsh_immediate = rsh_immediate;
scm_vm_intrinsics.heap_numbers_equal_p = scm_i_heap_numbers_equal_p;
scm_vm_intrinsics.less_p = less_p;
scm_vm_intrinsics.numerically_equal_p = numerically_equal_p;
scm_vm_intrinsics.resolve_module = resolve_module;
scm_vm_intrinsics.lookup = lookup;
scm_vm_intrinsics.define_x = scm_module_ensure_local_variable;
scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
"scm_init_intrinsics",
(scm_t_extension_init_func)scm_init_intrinsics,
NULL);
}
void
scm_init_intrinsics (void)
{
#ifndef SCM_MAGIC_SNARFER
#include "libguile/intrinsics.x"
#endif
}
|