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