Update README on using libraries in non-standard locations
[bpt/guile.git] / libguile / instructions.c
CommitLineData
8f5cfc81 1/* Copyright (C) 2001 Free Software Foundation, Inc.
17e90c5e 2 *
560b9c25
AW
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
17e90c5e 7 *
560b9c25
AW
8 * This library 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 GNU
11 * Lesser General Public License for more details.
17e90c5e 12 *
560b9c25
AW
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 02110-1301 USA
16 */
17e90c5e 17
13c47753
AW
18#if HAVE_CONFIG_H
19# include <config.h>
20#endif
21
17e90c5e 22#include <string.h>
560b9c25
AW
23
24#include "_scm.h"
83495480 25#include "vm-bootstrap.h"
17e90c5e
KN
26#include "instructions.h"
27
53e28ed9
AW
28struct scm_instruction {
29 enum scm_opcode opcode; /* opcode */
30 const char *name; /* instruction name */
31 signed char len; /* Instruction length. This may be -1 for
32 the loader (see the `VM_LOADER'
33 macro). */
34 signed char npop; /* The number of values popped. This may be
35 -1 for insns like `call' which can take
36 any number of arguments. */
37 char npush; /* the number of values pushed */
f775e51b 38 SCM symname; /* filled in later */
17e90c5e
KN
39};
40
53e28ed9
AW
41#define SCM_VALIDATE_LOOKUP_INSTRUCTION(pos, var, cvar) \
42 do { \
43 cvar = scm_lookup_instruction_by_name (var); \
44 SCM_ASSERT_TYPE (cvar, var, pos, FUNC_NAME, "INSTRUCTION_P"); \
45 } while (0)
17e90c5e 46
53e28ed9 47
f775e51b
AW
48static struct scm_instruction*
49fetch_instruction_table ()
50{
51 static struct scm_instruction *table = NULL;
52
53 if (SCM_UNLIKELY (!table))
54 {
55 size_t bytes = scm_op_last * sizeof(struct scm_instruction);
56 int i;
57 table = malloc (bytes);
58 memset (table, 0, bytes);
59#define VM_INSTRUCTION_TO_TABLE 1
aeeff258
AW
60#include <libguile/vm-expand.h>
61#include <libguile/vm-i-system.i>
62#include <libguile/vm-i-scheme.i>
63#include <libguile/vm-i-loader.i>
f775e51b
AW
64#undef VM_INSTRUCTION_TO_TABLE
65 for (i = 0; i < scm_op_last; i++)
66 {
67 table[i].opcode = i;
68 if (table[i].name)
69 table[i].symname = scm_from_locale_symbol (table[i].name);
70 else
71 table[i].symname = SCM_BOOL_F;
72 }
73 }
74 return table;
75}
76
53e28ed9
AW
77static struct scm_instruction *
78scm_lookup_instruction_by_name (SCM name)
17e90c5e 79{
f775e51b
AW
80 static SCM instructions_by_name = SCM_BOOL_F;
81 struct scm_instruction *table = fetch_instruction_table ();
82 SCM op;
83
84 if (SCM_UNLIKELY (SCM_FALSEP (instructions_by_name)))
85 {
86 int i;
87 instructions_by_name = scm_make_hash_table (SCM_I_MAKINUM (scm_op_last));
88 for (i = 0; i < scm_op_last; i++)
89 if (scm_is_true (table[i].symname))
90 scm_hashq_set_x (instructions_by_name, table[i].symname,
91 SCM_I_MAKINUM (i));
92 instructions_by_name = scm_permanent_object (instructions_by_name);
93 }
94
95 op = scm_hashq_ref (instructions_by_name, name, SCM_UNDEFINED);
96 if (SCM_I_INUMP (op))
97 return &table[SCM_I_INUM (op)];
98
99 return NULL;
17e90c5e
KN
100}
101
53e28ed9 102
17e90c5e
KN
103/* Scheme interface */
104
105SCM_DEFINE (scm_instruction_list, "instruction-list", 0, 0, 0,
106 (void),
107 "")
108#define FUNC_NAME s_scm_instruction_list
109{
110 SCM list = SCM_EOL;
111 struct scm_instruction *ip;
f775e51b
AW
112 for (ip = fetch_instruction_table (); ip->opcode != scm_op_last; ip++)
113 if (ip->name)
114 list = scm_cons (ip->symname, list);
17e90c5e
KN
115 return scm_reverse_x (list, SCM_EOL);
116}
117#undef FUNC_NAME
118
119SCM_DEFINE (scm_instruction_p, "instruction?", 1, 0, 0,
120 (SCM obj),
121 "")
122#define FUNC_NAME s_scm_instruction_p
123{
53e28ed9 124 return SCM_BOOL (scm_lookup_instruction_by_name (obj));
17e90c5e
KN
125}
126#undef FUNC_NAME
127
128SCM_DEFINE (scm_instruction_length, "instruction-length", 1, 0, 0,
129 (SCM inst),
130 "")
131#define FUNC_NAME s_scm_instruction_length
132{
53e28ed9
AW
133 struct scm_instruction *ip;
134 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst, ip);
135 return SCM_I_MAKINUM (ip->len);
46cd9a34
KN
136}
137#undef FUNC_NAME
138
139SCM_DEFINE (scm_instruction_pops, "instruction-pops", 1, 0, 0,
140 (SCM inst),
141 "")
142#define FUNC_NAME s_scm_instruction_pops
143{
53e28ed9
AW
144 struct scm_instruction *ip;
145 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst, ip);
146 return SCM_I_MAKINUM (ip->npop);
46cd9a34
KN
147}
148#undef FUNC_NAME
149
150SCM_DEFINE (scm_instruction_pushes, "instruction-pushes", 1, 0, 0,
151 (SCM inst),
152 "")
153#define FUNC_NAME s_scm_instruction_pushes
154{
53e28ed9
AW
155 struct scm_instruction *ip;
156 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst, ip);
157 return SCM_I_MAKINUM (ip->npush);
17e90c5e
KN
158}
159#undef FUNC_NAME
160
161SCM_DEFINE (scm_instruction_to_opcode, "instruction->opcode", 1, 0, 0,
162 (SCM inst),
163 "")
164#define FUNC_NAME s_scm_instruction_to_opcode
165{
53e28ed9
AW
166 struct scm_instruction *ip;
167 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst, ip);
168 return SCM_I_MAKINUM (ip->opcode);
17e90c5e
KN
169}
170#undef FUNC_NAME
171
172SCM_DEFINE (scm_opcode_to_instruction, "opcode->instruction", 1, 0, 0,
173 (SCM op),
174 "")
175#define FUNC_NAME s_scm_opcode_to_instruction
176{
53e28ed9 177 int opcode;
f775e51b 178 SCM ret = SCM_BOOL_F;
53e28ed9 179
62082959 180 SCM_MAKE_VALIDATE (1, op, I_INUMP);
53e28ed9
AW
181 opcode = SCM_I_INUM (op);
182
f775e51b
AW
183 if (opcode < scm_op_last)
184 ret = fetch_instruction_table ()[opcode].symname;
185
186 if (scm_is_false (ret))
187 scm_wrong_type_arg_msg (FUNC_NAME, 1, op, "INSTRUCTION_P");
53e28ed9 188
f775e51b 189 return ret;
17e90c5e
KN
190}
191#undef FUNC_NAME
192
07e56b27
AW
193void
194scm_bootstrap_instructions (void)
195{
60ae5ca2
AW
196 scm_c_register_extension ("libguile", "scm_init_instructions",
197 (scm_t_extension_init_func)scm_init_instructions,
198 NULL);
07e56b27
AW
199}
200
17e90c5e
KN
201void
202scm_init_instructions (void)
203{
07e56b27
AW
204 scm_bootstrap_vm ();
205
17e90c5e 206#ifndef SCM_MAGIC_SNARFER
aeeff258 207#include "libguile/instructions.x"
17e90c5e
KN
208#endif
209}
210
211/*
212 Local Variables:
213 c-file-style: "gnu"
214 End:
215*/