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